2016-04-10 12 views
0

しばらくするとウィンドウを切り替える必要がありますが、コードを動作させることができません。しばらくしてからWindows Tkinterを変更してください。

私のウィンドウは、ボタンをクリックしたときにのみ変化します。私はラムダを私のコマンドに入れようとしましたが、それでも動作しませんでした。

import tkinter as tk # python3 
#import Tkinter as tk # python 
import datetime 
TITLE_FONT = ("Helvetica", 18, "bold") 
time_start = datetime.datetime.now() 
delta_time = datetime.timedelta(seconds = 5) 
class SampleApp(tk.Tk): 

    def __init__(self, *args, **kwargs): 
     tk.Tk.__init__(self, *args, **kwargs) 

     # the container is where we'll stack a bunch of frames 
     # on top of each other, then the one we want visible 
     # will be raised above the others 
     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, TimePage): 
      page_name = F.__name__ 
      frame = F(container, self) 
      self.frames[page_name] = frame 

      # put all of the pages in the same location; 
      # the one on the top of the stacking order 
      # will be the one that is visible. 
      frame.grid(row=0, column=0, sticky="nsew") 

     self.show_frame("StartPage") 

    def show_frame(self, page_name): 
     '''Show a frame for the given page name''' 
     frame = self.frames[page_name] 
     frame.tkraise() 


class StartPage(tk.Frame): 
    def __init__(self, parent, controller): 

     tk.Frame.__init__(self, parent) 
     self.controller = controller 
     label = tk.Label(self, text="This is the start page", font=TITLE_FONT) 
     label.pack(side="top", fill="x", pady=10) 

     button1 = tk.Button(self, text="Go to Time Page", 
          command=lambda: controller.show_frame(self.start_Counting())) 
     button1.pack() 

    def start_Counting(self): 
     global time_start 
     time_start = datetime.datetime.now() 
     return 'TimePage' 

class TimePage(tk.Frame): 

    def __init__(self, parent, controller): 
     global delta_time 
     tk.Frame.__init__(self, parent) 
     self.controller = controller 
     label = tk.Label(self, text="This is Time Page", font=TITLE_FONT) 
     label.pack(side="top", fill="x", pady=10) 
     self.time_exit = tk.StringVar() 
     self.time_exit.set('%s' %datetime.datetime.now()) 
     label2 = tk.Label(self, text=self.time_exit, font=TITLE_FONT) 
     label2.pack(side="top", fill="x", pady=10) 
     button = tk.Button(self, text="Go to the start page", 
          command=lambda: controller.show_frame("StartPage")) 
     button.pack() 

    def update_Page(self): 
     global time_start, delta_time 
     #print('Executou o atualizar') 
     time_until = delta_time - (datetime.datetime.now() - time_start) 
     self.time_exit.set('%s' %time_until) 
     if time_until <= 0: 
      self.controller.show_frame('StartPage') # Go to the start_page after 5 seconds 
     self.after(1000, update_Page) 

if __name__ == "__main__": 
    app = SampleApp() 
    app.mainloop() 
+0

フォーマットが改善されましたが、この質問には正確なエラーの詳細が必要な場合があります。 [How to Ask](http://www.stackoverflow.com/help/how-to-ask)を参照してください。 – CodeMouse92

答えて

0

update_Pageに決して電話することはありません。最初のステップは、その関数を呼び出すことです。ただし、datetimeオブジェクトの計算を繰り返し行う必要はありません。あなたが必要とする秒数を計算し、afterを一度呼び出してください。

関連する問題