尝试将计时器显示为按钮文本时出错

时间:2014-07-01 15:29:52

标签: python

当我运行此代码时,一切似乎工作正常,然后当我点击我的蓝色buff按钮,我期待我的时间倒计时开始显示按钮文本而不是更改为函数my_time在0x000000 ...等一些记忆位置我很难找到解决方案。如果不清楚我想要做的是将文本Blue Buff更改为倒计时。

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

2 个答案:

答案 0 :(得分:0)

看起来您正在设置self.Button1["command"] = self.my_time2 self.my_time2不是属性,而是功能。这就是为什么数字看起来都很糟糕,是因为它显示的是一个内存地址而不是你期望的str(self.my_time)。也许你打算调用 my_time2()

*编辑1

将第25行更改为

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

你在my_time中写的内容也会打印到stdout,而不是实际更新按钮(我认为你想要的是什么?),但是我会让你把它想象成设计师。

答案 1 :(得分: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()