2017-09-05 58 views
2

我目前正在使用GUI,我希望我的GUI在最後一幀保留5秒,然後從最後一幀返回到第一幀任何互動。如何更改tkinter中使用時間和沒有按鈕命令的幀

如果我使用after小部件方法,那麼定時器將從應用程序的開始處開始。 如果我把它放在方法中,並且如果我調用該方法,則會出現相同的行爲。另一方面,如果我通過一個按鈕調用該函數,它將按預期工作。

控制器

# The code for changing pages was derived from: 
    http://stackoverflow.com/questions/7546050/switch-between-two-frames-in-tkinter 
# License: http://creativecommons.org/licenses/by-sa/3.0/ 

import tkinter as tk 


LARGE_FONT= ("Verdana", 12) 


class SeaofBTCapp(tk.Tk): 

    def __init__(self, *args, **kwargs): 

     tk.Tk.__init__(self, *args, **kwargs) 
     container = tk.Frame(self) 

     container.pack(side="top", fill="both", expand = True) 

     container.grid_rowconfigure(0, weight=1) 
     container.grid_columnconfigure(0, weight=1) 

     self.frames = {} 

     for F in (StartPage, PageOne, PageTwo): 

      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() 

StartPage

class StartPage(tk.Frame): 

    def __init__(self, parent, controller): 
     tk.Frame.__init__(self,parent) 
     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: controller.show_frame(PageTwo)) 
     button2.pack() 

PageOne

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() 

PageTwo

class PageTwo(tk.Frame): 

    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     label = tk.Label(self, text="Page Two!!!", font=LARGE_FONT) 
     label.pack(pady=10,padx=10) 

     button1 = tk.Button(self, text="Back to Home", 
         command=lambda:self.start_reset()) 
     button1.pack() 

     button2 = tk.Button(self, text="Page One", 
         command=lambda: controller.show_frame(PageOne)) 
     button2.pack() 
     # self.after(5000,self.controller.show_frame(StartPage)) works only 
     # once and the time starts from the start of the GUI and after 5000ms it takes 
     # the gui to the start page no matter what 
    def start_reset(self): 
     self.after(5000,self.controller.show_frame(StartPage)) 


app = SeaofBTCapp() 
app.mainloop() 
+0

相關:[如何使用after方法定期調用?](https://stackoverflow.com/q/44085554/7051394) –

回答

3

有幾個地方,你可以把你的電話給after方法。 對我來說,如果將它放置在控制器類中,則更有意義,因爲它旨在管理組件。

一個直接的方式實現這一目標是完成SeaofBTCapp.show_frame方法:

def show_frame(self, cont): 
    frame = self.frames[cont] 
    frame.tkraise() 

    if cont == PageTwo: 
     self.after(5000, self.show_frame, StartPage) 

show_frame方法被調用,以show_frame調用使用StartPage作爲參數定。

+0

非常感謝您的回答。你的回答是非常好的描述,真的很有幫助。非常感謝 –