-1
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.

mathyman
  • 19
  • 4
  • First you have to understand [Event-driven programming](https://stackoverflow.com/a/9343402/7414759). Furthermore, as it stands, you get `Syntax Error` and `Indentation Error`. – stovfl Mar 07 '20 at 16:54
  • that's not the full code just a part – mathyman Mar 07 '20 at 17:34
  • 1
    ***" not the full code just a part'***': Remove the not relevant part, per the [mcve] guidelines. Have you made any progress? – stovfl Mar 07 '20 at 17:37
  • here is the code so need tkvar and tkvar1 to change when selected: – mathyman Mar 07 '20 at 17:39
  • https://pastebin.com/nfjTi5sU – mathyman Mar 07 '20 at 17:40
  • We don't need more code, your problem is shown well. What did you not understand from the given link? – stovfl Mar 07 '20 at 17:41
  • i want to be able to draw a different graph based from the user's selction from the drop down list but it doesn't seem to work as the data in tkvar and tkvar1 is not changing – mathyman Mar 07 '20 at 17:43
  • It doesn't help repeating what you want to do. What did you not understand from the given link: [Event-driven programming](https://stackoverflow.com/a/9343402/7414759) – stovfl Mar 07 '20 at 17:45
  • I've not some across this terminology as I'm still a bit of a noob. – mathyman Mar 07 '20 at 17:47
  • So take the time an read up on this and try to understand. – stovfl Mar 07 '20 at 17:48

1 Answers1

1

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)
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685