我尝试通过Tkinter进行GUI。此代码如下 目标是每1秒显示消息“此页面xxx”。当我打开每一帧时
(对不起,我解释英语不好)
import Tkinter as tk
LARGE_FONT= ("Verdana", 12)
class Application(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self, bg='red')
container.pack(side="top", fill="both", expand = True)
self.val1 = "chain"
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
self.collection = (StartPage, PageOne, PageTwo)
for F in self.collection:
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
def printdata(self, data):
print data
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
self.master = controller
self.configure(background='green')
label = tk.Label(self, text="Start Page", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button = tk.Button(self, text="Visit Page 1", command=lambda: controller.show_frame(PageOne))
button.pack()
button2 = tk.Button(self, text="Visit Page 2", command=lambda: self.gopagetwo())
button2.pack()
def gopagetwo(self):
PageTwo.mymessage()
self.master.show_frame(PageTwo)
def mymessage(self):
print 'This is page initial.'
self.after(1000, self.mymessage)
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent, bg='yellow')
self.master = controller
self.label = tk.Label(self, text="Page One!!!", font=LARGE_FONT)
self.label.pack(pady=5,padx=10)
self.lblCounter = tk.Label(self, text="Counter", font=LARGE_FONT)
self.lblCounter.pack(pady=5,padx=10)
self.lblStartTime = tk.Label(self, text="time start", font=LARGE_FONT)
self.lblStartTime.pack(pady=5,padx=10)
self.lblStartNow = tk.Label(self, text="time now", font=LARGE_FONT)
self.lblStartNow.pack(pady=5,padx=10)
button1 = tk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.pack()
def mymessage(self):
print 'This is page one.'
self.after(1000, self.mymessage)
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent, bg='pink')
self.master = controller
label = tk.Label(self, text="Page Two!!!", font=LARGE_FONT)
label.pack(pady=5,padx=10)
self.lblCounter = tk.Label(self, text="Counter", font=LARGE_FONT)
self.lblCounter.pack(pady=5,padx=10)
self.lblStartTime = tk.Label(self, text="time start", font=LARGE_FONT)
self.lblStartTime.pack(pady=5,padx=10)
self.lblStartNow = tk.Label(self, text="time now", font=LARGE_FONT)
self.lblStartNow.pack(pady=5,padx=10)
button1 = tk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.pack()
def mymessage(self):
print 'This is page two.'
self.after(1000, self.myssage)
app = Application()
app.mainloop()
答案 0 :(得分:0)
您的代码中存在一些问题。
首先,在StartPage.gopagetwo
中,PageTwo.mymessage()
的调用不起作用,因为mymessage是一个实例方法,它需要一个PageTwo实例才能正常工作。
您可以尝试通过在frame.mymessage()
中添加对Application.show_frame
的调用来达到您想要的行为。但是,您会发现它不会为当前帧打印消息,而是针对每个访问过的帧(以及一个访问过的帧)打印消息。如果您按照此轨道进行操作,则必须将after的绑定存储在某处,并提供中止它的选项。
class StartPage(tk.Frame):
#...
def mymessage(self):
print 'This is page initial.'
self.after_id = self.after(1000, self.mymessage)
def end_mymessageloop(self):
self.after_cancel(self.after_id)
并调整show_frame
:
class Application(tk.Tk):
#...
def show_frame(self, cont):
if hasattr(self,"current_frame"):
self.current_frame.end_mymessageloop()
frame = self.frames[cont]
frame.tkraise()
frame.mymessage()
self.current_frame = frame
<小时/> 但是,您可能会发现只有一个后循环的实例更简单,它将print语句委托给您的帧(并在每个mymessage中删除循环)。
class Application(tk.Tk):
def __init__(self, *args, **kwargs):
#...
self.show_frame(StartPage)
self.status_message()
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
self.current_frame = frame
def status_message(self):
self.current_frame.mymessage()
self.after(1000, self.status_message)