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()
大家好,我想从我创建的表中提取用户的输入。但是,当我尝试运行代码时,它只会绘制出tkvar1或tkvar默认数据。我怎样才能解决这个问题?即使单击不同的选项,tkvar和tkvar1似乎也没有改变。
如果有人可以帮助我告诉我如何更新tkvar和tkvar1,我将非常感激。
答案 0 :(得分:1)
在用户有机会与UI交互之前,您正在很好地调用.get()
函数。您的按钮需要调用一个获取值的函数,然后调用plotgraph
。为此,您的变量需要保存为实例属性。
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)