0
我嘗試通過Tkinter GUI。該代碼如下, 目標是每1秒顯示消息「本頁xxx」。當我打開每個幀相同Tkinter多幀事件
- 當我打開一個頁面將顯示消息「這是一個網頁」每隔1秒
- 當我打開兩個頁面將顯示消息「這是兩頁的」每1第二(不顯示消息頁面之一)
(對不起我的所有解釋英語不太好)
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()
那麼問題是什麼?代碼不起作用,你會得到錯誤,如果是的話,哪些? – Marcin
是的,我有問題。當我按下開始頁面上的按鈕頁面1和頁面1時,立即顯示並且我要立即發送消息「這是第一頁面」,並在重新開始頁面返回時取消。 – user3814581
對不起,不瞭解你的代碼。它似乎不完整。什麼是'self.mastermessage'?它沒有被初始化,也沒有在你提供的代碼中定義。由於這個'PageTwo.mymessage()',當按下第二個按鈕時,代碼也會返回錯誤。 – Marcin