Answered step by step
Verified Expert Solution
Question
1 Approved Answer
i have a python fucntion to plot some data into a tk interface, but my data does not display properly into my graphs, for example
i have a python fucntion to plot some data into a tk interface, but my data does not display properly into my graphs, for example when i select an item, and then it loads all the data, then i select a month such as november but some days for the previous month appear and then not all days for that month appear, i have added my code for the function and also my table from my database that includes some data points not all:
# Create a ttk Notebook for the Table of Contents
tocnotebook ttkNotebookwindow
tocnotebook.gridrow column columnspan padx pady sticky'nsew'
# Create frames for each tab
dailyframe ttkFrametocnotebook
weeklyframe ttkFrametocnotebook
# monthlyframe ttkFrametocnotebook
tocnotebook.adddailyframe, text'Daily Usage'
tocnotebook.addweeklyframe, text'Weekly Usage'
# tocnotebook.addmonthlyframe, text'Monthly Usage'
# Dropdown for month selection
months All Months'datetime mstrftimeB for m in range # List of months with 'All Months' as the first option
monthvar tkStringVarwindow
monthcombobox ttkComboboxwindow textvariablemonthvar, valuesmonths, state"readonly"
monthcombobox.current # Default to 'All Months'
monthcombobox.gridrow column padx pady stickyw
tocframes
"Daily Usage": dailyframe,
"Weekly Usage": weeklyframe,
# "Monthly Usage": monthlyframe
def drawgraphitemname, timeperiod, selectedmonth None:
global graphcanvas
if not itemname: # If no item is selected, do nothing
return
# Clear previous graph if it exists
if graphcanvas:
graphcanvas.gettkwidgetdestroy
# Connect to the database
db mysqlconnector.connect
host'localhost',
user'root',
password'peter',
database'shopinventory'
cursor dbcursor
# Modify the SQL query to filter data based on the selected month
monthcondition
if selectedmonth and selectedmonth 'All Months':
monthnumber datetime.strptimeselectedmonth, Bmonth
monthcondition AND MONTHchangedates monthnumber
if timeperiod "Daily Usage":
cursor.execute
SELECT DATEchangedate as date, SUMquantitychange as totalchange
FROM changeslog
WHERE itemname s monthcondition
GROUP BY DATEchangedate
ORDER BY DATEchangedate ASC
itemname,
elif timeperiod "Weekly Usage":
cursor.execute
SELECT YEARWEEKchangedate as week, SUMquantitychange as totalchange
FROM changeslog
WHERE itemname s
GROUP BY YEARWEEKchangedate
ORDER BY YEARWEEKchangedate ASC
itemname,
# Fetch the data
data cursor.fetchall
dates x for x in data
quantities x for x in data
fig, ax pltsubplotsfigsize # Adjust the size as needed
axplotdates quantities, markero # Add markers for each data point
# Adjust the plot margins
pltsubplotsadjustbottom # Increase the bottom margin to allow for more space
# Format the graph based on the timeperiod
if timeperiod "Daily Usage":
axxaxis.setmajorformattermdatesDateFormatterYmd
axxaxis.setmajorlocatormdatesDayLocatorinterval # Set interval to show every day
pltxticksrotation fontsize
elif timeperiod "Weekly Usage":
# Convert week numbers to the starting date of the week for labeling
startdates datetimestrptimefdateYWw for date in dates
axsetxticksrangelenstartdates # Set xticks to be the start dates of weeks
axsetxticklabelsdatestrftimeYmd for date in startdates rotation
# Embedding the figure in the Tkinter window
graphcanvas FigureCanvasTkAggfig mastertocframestimeperiod
graphcanvas.draw
graphwidget graphcanvas.gettkwidget
graphwidget.packsidetkTOP, filltkBOTH, expandTrue
# Close the database connection
cursor.close
dbclose
mysql SELECT
FROM changeslog
ORDER BY changedate ASC;
id itemname originalquantity newquantity changetime empid changedate quantitychange rando
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started