Drop down menu choice not changing

JonathanChiu21 :
def Graphing():
class Root(Tk):
    def __init__(self):
        super(Root, self).__init__()
        label_title = tk.Label(self, text="Graph Page", font=12)
        label_title.pack(pady=10, padx=10)

        #variables to use
        tkvar = StringVar(self) #tkinter  for x axis
        tkvar1 = StringVar(self) #tkinter for yaxis
        OPTIONS = ['product_qty','product_price','product_sold','product_sellprice']
        OPTIONS1=  ['product_qty','product_price','product_sold','product_sellprice']
        tkvar.set(OPTIONS[3])
        tkvar1.set(OPTIONS1[0])




        #frames
        right_frame = tk.Frame(self)
        right_frame.pack(side=RIGHT)


        y_label = tk.Label(right_frame, text="x = ")
        y_label.grid(row=0, column=0)

        x_label = tk.Label(right_frame, text="y = ")
        x_label.grid(row=1, column=0)

        #equation_box = tk.Entry(right_frame)  # user equation entry
        #equation_box.grid(row=0, column=1)

        x = tk.OptionMenu(right_frame, tkvar, *OPTIONS)
        x.grid(row=0, column=1)# option menu

        y=tk.OptionMenu(right_frame, tkvar1, *OPTIONS1)
        y.grid(row=1, column=1)

        xaxis=tkvar.get()
        yaxis=tkvar1.get()


        draw_graph_button = tk.Button(right_frame, text="graph", height=2, width=15, bg="#b6bfcc",
                                      command=lambda: plotgraph(xaxis,yaxis))
        draw_graph_button.grid(row=2, column=1, pady=10)


        fig=Figure() #blank canvas
        a = fig.add_subplot(111) #adds the line
        a.plot([1, 2, 3, 4, 5, 6, 7, 8], [5, 6, 1, 3, 8, 9, 3, 5])


        #print(xtoplot)
        #print(ytoplot)



        #equation_box = tk.Entry(self)
        #equation_box.pack()
        #self.minsize(640, 400)
        #self.plotgraph()
        #self.matplotCanvas()

        #function that take in sql to plot
        def plotgraph(xaxis,yaxis):
            a.clear()
            #f = Figure(figsize=(5, 5), dpi=100)
            Database()
            mycursor = cursor.execute(f"SELECT {xaxis} FROM product")# select statement to get product_sold
            x = mycursor.fetchall()
            cursor.close()
            conn.close() #finds xaxis

            xtoplot = []
            for t in x:
                for x in t:
                    xtoplot.append(x)
            xtoplot = list(map(float, xtoplot))

            Database()
            mycursor = cursor.execute(f"SELECT {yaxis} FROM product") # select statemnt for y axis
            y = mycursor.fetchall()
            cursor.close()
            conn.close() #find y axis
            ytoplot = []
            for t in y:
                for y in t:
                    ytoplot.append(y)  # convert to list
            ytoplot = list(map(float, ytoplot))  # convert to float
            a.plot(xtoplot, ytoplot)
            canvas.draw()
            print(xtoplot)

        canvas = FigureCanvasTkAgg(fig, self)
        canvas.draw()
        canvas.get_tk_widget().pack(side=BOTTOM, fill=BOTH, expand=True)
        toolbar = NavigationToolbar2Tk(canvas, self)
        toolbar.update()
        canvas._tkcanvas.pack(side=TOP, fill=BOTH, expand=True)

        # a.plot([1, 2, 3, 4, 5, 6, 7, 8], [5, 6, 1, 3, 8, 9, 3, 5]) #data to plot




root = Root()
root.mainloop()

Hello all, I want to draw out user's input from a table that I've created. However when i tried running the code it only draws out the tkvar1 or tkvar default data. How can I fix this? It seems tkvar and tkvar1 don't change even when I click on a different option.

If someone can help tell me how I can make tkvar and tkvar1 update I will be most appreciative.

Bryan Oakley :

You are calling the .get() function well before the user has a chance to interact with the UI. Your button needs to call a function that gets the value and then calls plotgraph. To do this, your variables need to be saved as instance attributes.

class Root(Tk):
    def __init__(self):
        ...
        self.tkvar = StringVar(self) #tkinter  for x axis
        self.tkvar1 = StringVar(self) #tkinter for yaxis
        ...
        draw_graph_button = tk.Button(..., command=self.do_graph)
        ...

    def do_graph(self):
        xaxis = self.tkvar.get()
        yaxis = self.tkver1.get()
        plotgraph(xaxis,yaxis)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=32826&siteId=1