2014-07-01 34 views
0

當我運行此代碼一切似乎工作正常,然後當我點擊我的藍色按鈕,我期待我的時間倒計時開始顯示按鈕文本改爲函數my_time在0x000000 ...等一些內存位置im努力尋找解決方案。如果它不清楚我試圖做什麼是文本藍色魔法變爲倒計時。錯誤當試圖顯示一個計時器到按鈕文本

from Tkinter import * 
import os 
import time 

class Application(Frame): 
    @staticmethod 



def my_time(self): # creates a timer starting at 5 min , counts down to 0 then repeats 
min = 4 
sec = 59 
while sec <=60: 
    os.system('cls') 
    print min, "Minutes", sec, "Seconds" 
    time.sleep(1) 
     sec -= 1 
    if sec == 0: 
     min -= 1 
     sec = 59 
    elif min == 0: 
     min = 4 

    def my_time2(self):     # tries to display my_time method into the button font, doesnt seem to work. 
     self.Button1["text"] = str(self.my_time) 





    def createButtons(self):      # creats a button 
     self.Button1 = Button(self) 
     self.Button1["text"] = "Blue Buff" 
     self.Button1["fg"] = "Blue" 
     self.Button1["command"] = self.my_time2 # suppose to implement countdown in button text when click. 
          # currently problematic?     
     self.Button1.pack({"side": "left"}) 



    def __init__(self, master=None): 
     Frame.__init__(self, master) # initializes window 
     self.pack() 
     self.createButtons() 

root = Tk() 
app = Application(master=root) 
app.mainloop() 
+0

你'my_time'功能需要正確的縮進 – Shadow9043

回答

0

這應該做你需要的東西:

from Tkinter import * 
import os 
import time 

class Application(Frame): 

    def __init__(self, master=None): 
     Frame.__init__(self, master) # initializes window 
     self.Button1 = Button(self, text="Blue Buff", command=self.my_time) 
     self.Button1.pack() 
     self.pack() 

    def my_time(self): # creates a timer starting at 5 min , counts down to 0 then repeats 
     min = 4 
     sec = 59 

     while sec <=60: 
      os.system('cls') 
      print min, "Minutes", sec, "Seconds" 
      self.Button1.configure(text="{}:{}".format(min,sec)) 
      self.Button1.update() 
      time.sleep(1) 
      sec -= 1 
      if sec == 0: 
       min -= 1 
       sec = 59 
      elif min == 0: 
       min = 1 

root = Tk() 
app = Application(master=root) 
app.mainloop()# 

使用自己的代碼只是設置命令my_time和更新Button1的:

class Application(Frame): 

    def my_time(self): # creates a timer starting at 5 min , counts down to 0 then repeats 
     min = 4 
     sec = 59 
     while sec <=60: 
      self.Button1.configure(text="{}:{}".format(min,sec)) 
      self.Button1.update() 
      os.system('cls') 
      print min, "Minutes", sec, "Seconds" 
      time.sleep(1) 
      sec -= 1 
      if sec == 0: 
       min -= 1 
       sec = 59 
      elif min == 0: 
       min = 4 

    def my_time2(self):     # tries to display my_time method into the button font, doesnt seem to work. 
     self.Button1["text"] = str(self.my_time) 

    def createButtons(self):      # creats a button 
     self.Button1 = Button(self) 
     self.Button1["text"] = "Blue Buff" 
     self.Button1["fg"] = "Blue" 
     self.Button1["command"] = self.my_time # suppose to implement countdown in button text when click. 
          # currently problematic? 
     self.Button1.pack({"side": "left"}) 
    def __init__(self, master=None): 
     Frame.__init__(self, master) # initializes window 
     self.pack() 
     self.createButtons() 
0

看起來你正在設置self.Button1["command"] = self.my_time2self.my_time2不是一個屬性,而是一個功能。這就是爲什麼數字看起來都很奇怪,是因爲它顯示的是內存地址,而不是你期望的str(self.my_time)。也許你的意思是調用my_time2()

*編輯1

變更線25

self.Button1["text"] = str(self.my_time(self)) 

而且你寫在my_time什麼都會打印到標準輸出,而不是實際更新按鈕(是我認爲你想要的嗎?),但是我會讓你把它當作設計師。

+0

我明白了什麼你的說法很有道理。它會看起來它記錄雖然,我有點困惑 – Achilles

+0

self.Button1 [「command」] = self.my_time2() – The2ndSon

+0

self.Button1 [「command」] = self.my_time()不起作用 –

相關問題