0

I'm creating a program to plot values of product sales volumes of different products. But, when I run the program it automatically prints the default value of the option menu, and when i change the value and press show the graph, it doesn't retrieve the new value and thus doesn't update the graph.

global aggregateMonthly
aggregateSalesMonthly = cur.execute("""select SUM(HotDrink), SUM (ColdDrink),
SUM(Cake), SUM(Smoothie), SUM(DecafDrink),
strftime("%m-%Y", Date) as 'month-year' 
from TotalDaySales group by strftime("%m-%Y", Date);""").fetchall()

root = tkinter.Tk()
root.wm_title("Analytics")



def plotter(title, index1):
    valueArray = []
    valueArray2 = []
    for values in aggregateSalesMonthly:
        valueArray2.append(values[-1])
        valueArray.append(values[index1])
        print(values[index1])
    #print(valueArray)    


    plt.title(title)
    plt.xlabel('Months')
    plt.ylabel('Sales Volume')
    plt.xticks(rotation=30)
    f = Figure()
    f.add_subplot().plot(valueArray2, valueArray)

    canvas = FigureCanvasTkAgg(f, master=root)  # A tk.DrawingArea.
    canvas.draw()
    canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)


d = {
    'Hot Drinks': (plotter, ('Hot Drinks', 0)),
    'Cold Drinks': (plotter, ('Cold Drinks', 1)),
    'Cake': (plotter, ('Cake', 2)),
    'Smoothie': (plotter, ('Smoothie', 3)),
    'DecafDrink': (plotter, ('DecafDrink', 4)),
    }

def showGraph(value):
    foo, args = d[(value)]
    foo(*args)


cats1 = StringVar(root)
cats1.set("Hot Drinks") # default value
catgeory = OptionMenu(root, cats1, "Total", "Hot Drinks", "Cold Drinks", "Food", 
"None")
catgeory.pack(side=LEFT)
variableGetter = cats1.get()

button = tkinter.Button(root, text="Show Graph")
button.bind("<Button-1>", print(cats1.get()))
button.pack(side=LEFT)


root.mainloop()
stovfl
  • 14,998
  • 7
  • 24
  • 51
  • You only query the database once when your program starts. If you want the latest data to be fetched each time you change the value, your database query needs to be inside the function that is called when you change the value. – scotty3785 Oct 17 '19 at 08:51
  • I understand your logic. Would you care to provide an exemplar? Regards – TheDestroyer Oct 17 '19 at 11:05
  • 1
    @TheDestroyer: Read [Why is Button parameter “command” executed when declared?](https://stackoverflow.com/questions/5767228/why-is-button-parameter-command-executed-when-declared), this apply also to [`.bind(..., `](http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm) – stovfl Oct 17 '19 at 11:10
  • This was to test for the new updated value - which didn't work. Instead of "print((cats1.get()))" I had it as "showGraph(cats1.get())" - but it still didn't work. I want it executed when only clicked. – TheDestroyer Oct 17 '19 at 11:17
  • You cannot use `showGraph(cats1.get())` in the bind, for the reason @stovfl pointed to. – Bryan Oakley Oct 17 '19 at 14:24

0 Answers0