2012-08-15 40 views
1

目標:30分鐘的定時活動循環通過一系列交替緩慢和激烈的練習。窗口從30分鐘開始顯示倒數計時器。練習開始於30秒的「慢」活動 - 在計時器下方顯示「慢」字。然後繼續60秒的劇烈運動。然後是30秒的慢節奏,然後是60秒的不同的激烈運動,依此類推。總共有4種不同的激烈練習,每種練習都緩慢地交替進行。Python中的基於時鐘的方法TKinter窗口 - 請評價

我已經彙編了下面的代碼。我不相信這很直觀。我不喜歡繞着方法跳躍,我覺得有些方法可以用來提高效率。在實際的層面上,由於計時器和消息(即顯示的慢/鍛鍊#)開始變得不同步,這不能很好地工作。我研究了python博客,並且不斷聽到有關Tkinter不是多線程的內容。我猜測after(x,y)會因爲倒計時線程佔用資源而受到影響....不太確定。另外,我想在每種方法之後添加一個音調作爲「運動員」的音頻隊列,無論如何,先謝謝了,看看我的原始代碼。

import Tkinter as tk 

class ExampleApp(tk.Tk): 
    def __init__(self): 
     tk.Tk.__init__(self) 
     self.label = tk.Label(self, text="", width=10, font= ("Helvetica",72), background='yellow', fg = 'red') 
     self.label2 = tk.Label(self, text="", width=10, font=("Helvetica",72), background='yellow', fg = 'blue') 
     self.label.pack() 
     self.label2.pack() 
     self.remaining = 0 
     self.countdown(1800) # the 30 minutes countdown initialization 
     self.run = True 

    def countdown(self, remaining = None): 
     if self.remaining == 1799: # marker that starts the methods series 
      self.start() 
     if remaining is not None: 
      self.remaining = remaining 
     if self.remaining >= 1: 
      mins = int(self.remaining/60) 
      #rsecs = self.remaining - mins * 60 #could use this 
      rsecs = self.remaining % 60  #same results as t - mins * 60 
      self.remaining = self.remaining -1 
      self.after(1000, self.countdown) 
      self.label.configure(text ="%.2f" % (mins + rsecs/100.0)) 
     else: 
      self.label.configure(text="Stop") 

    def start(self): 
     app.after(0, self.slow1) 
     return 

    def slow1(self): 
     self.label2.configure(text="Slow") 
     app.after(30000, self.fast1) 
     return 

    def fast1(self): 
     self.label2.configure(text="Exercise 1") 
     app.after(61000, self.slow2) 
     return 

    def slow2(self): 
     self.label2.configure(text="Slow") 
     app.after(31000, self.fast2) 
     return 

    def fast2(self): 
     self.label2.configure(text="Exercise 2") 
     app.after(61000, self.slow3) 
     return 

    def slow3(self): 
     self.label2.configure(text="Slow") 
     app.after(30000, self.fast3) 
     return 

    def fast3(self): 
     self.label2.configure(text="Exercise 3") 
     app.after(60000, self.slow4) 
     return 

    def slow4(self): 
     self.label2.configure(text="Slow") 
     app.after(30000, self.fast4) 
     return 

    def fast4(self): 
     self.label2.configure(text="Exercise 4") 
     app.after(60000, self.slow1) 
     return 

if __name__ == "__main__": 
    app = ExampleApp() 
    app.title("Intense Workout") 
    app.geometry('550x550+200+200') 
    app.configure(background='yellow') 
    app.mainloop() 
+0

你說你有四個劇烈運動的時期,每個持續60秒。每個30秒的低強度運動開始。總共需要6分鐘,但你首先說你需要30分鐘的運動。額外的24分鐘來自哪裏? – 2012-08-15 12:36:29

回答

1

有很多方法可以解決這個問題,而且你不需要線程。這裏有一個想法:

從定義間隔的數據結構開始。例如:

intervals = [[30, "slow"], [60, "intense"], [30, "slow"], [60, "intense]] 

接下來,設置一個簡單的定時器函數,每秒調用一次。在每秒鐘它顯示數字和位於列表頭部的字符串。數字表示剩下的時間。在那個定時器功能中,從數字中減去一個。如果它變爲零,請從列表中刪除該元素。使用after後,此功能在一秒鐘後自動調用,當數據結構變空(即:只剩下一對並且數字降至零)時停止。

以下爲您提供有關如何實現此目的的想法。代碼可以做得更好,但重點是說明如何使用after來迭代數據結構,而不一定給出生產質量示例。

import Tkinter as tk 

class ExampleApp(tk.Tk): 
    def __init__(self): 
     tk.Tk.__init__(self) 
     self.label = tk.Label(self, text="", width=10, font= ("Helvetica",72), background='yellow', fg = 'red') 
     self.label2 = tk.Label(self, text="", width=10, font=("Helvetica",72), background='yellow', fg = 'blue') 
     self.label.pack() 
     self.label2.pack() 
     self.intervals = [[30, "Slow"], [60, "Exercise 1"], 
          [30, "Slow"], [60, "Exercise 2"], 
          [30, "Slow"], [60, "Exercise 3"], 
          [30, "Slow"], [60, "Exercise 4"], 
          ] 
     self.countdown() 

    def countdown(self): 
     (remaining, label) = self.intervals[0] 
     self.label.configure(text=remaining) 
     self.label2.configure(text=label) 
     remaining -= 1 
     if remaining < 0: 
      self.intervals.pop(0) 
     else: 
      self.intervals[0][0] = remaining 
     if len(self.intervals) > 0: 
      self.after(1000, self.countdown) 
     else: 
      self.label.configure(text="Done!") 

if __name__ == "__main__": 
    app = ExampleApp() 
    app.title("Intense Workout") 
    app.geometry('550x550+200+200') 
    app.configure(background='yellow') 
    app.mainloop() 
+0

真棒!這種方法我也可以做更多。 – 2012-08-16 02:06:36

+0

在回答你的問題時:「你說你有四個強烈的鍛鍊階段.....額外的24分鐘從哪裏來?」它將通過這6分鐘系列循環5次。 – 2012-08-16 13:11:07