【发布时间】:2015-07-20 22:33:11
【问题描述】:
同事们,
我正在设计一个带有两个按钮的 GUI,一个是显示图表,每小时温度。 我面临的问题是我无法创建一个使用 self.after 更新值的函数(update_graph)。
这部分创建第 1 页,我工作正常,直到我调用 update_graph
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Page One!!!", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button1 = tk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.pack()
button2 = tk.Button(self, text="Page Two",
command=lambda: controller.show_frame(PageTwo))
button2.pack()
canvas = Canvas(self, width=400, height=400, bg = 'white')
canvas.pack()
# create x and y axes
canvas.create_line(100,250,400,250, width=2)
canvas.create_line(100,250,100,50, width=2)
# creates divisions for each axle
for i in range(11):
x = 100 + (i * 30)
canvas.create_line(x,250,x,245, width=2)
canvas.create_text(x,254, text='%d'% (10*i), anchor=N)
for i in range(6):
y = 250 - (i * 40)
canvas.create_line(100,y,105,y, width=2)
canvas.create_text(96,y, text='%5.1f'% (50.*i), anchor=E)
self.update_graph()
def update_graph(self):
# here is canvas create line that causes a trouble
canvas.create_line(100,250,140,200, width=2)
self.after(100,self.update_graph)
Whith this code I get an error "canvas is not defined".
If I add self to canvas in update_graph, I get
self.canvas.create_line(100,250,140,200, width=2)
AttributeError: 'PageOne' object has no attribute 'canvas'
我在这里错过了什么?
【问题讨论】:
-
谢谢,我自己能够使用你的“部门”循环来获得灵感
标签: user-interface python-3.x tkinter tkinter-canvas