Python计时器不按预期显示文本

时间:2013-03-31 05:51:23

标签: python python-2.7 timer

我决定使用本网站上的计时器构建,在我做任何事情以确保它看起来没问题之前,我决定测试它,但是我设置显示的标签不起作用。请注意,有些事情可能看似不合逻辑,但我只是尝试了各种各样的事情来尝试让它发挥作用

代码:

import Tkinter

class Timer(Tkinter.Tk):

    def __init__(self):

        Tkinter.Tk.__init__(self)

        '''Variable Setup'''
        self.hour = Tkinter.StringVar()
        self.minutes = Tkinter.StringVar()
        self.seconds = Tkinter.StringVar()

        '''Setting Up Time Inputes'''
        hourLabel = Tkinter.Label(self, text="Hours: ").pack()
        hourEntry = Tkinter.Entry(self, width=5,textvariable=self.hour).pack()

        minLabel = Tkinter.Label(self, text="Minutes: ").pack()
        minEntry = Tkinter.Entry(self, width=5,textvariable=self.minutes).pack()

        secsLabel = Tkinter.Label(self, text="Seconds: ").pack()
        secsEntry = Tkinter.Entry(self, width=5,textvariable=self.seconds).pack()

        self.timerLab = Tkinter.Label(self, text="", width=10).pack()

        startBut = Tkinter.Button(self, text="Start Timing", command=self.validation).pack()

    def validation(self):

        '''Simple Try to turn it to int value method to validate'''
        hours = self.hour.get()
        minutes = self.hour.get()
        seconds = self.seconds.get()

        try:
            hours = int(hours)
        except ValueError:
            print("Invalid Inputs")

        try:
            minutes = int(minutes)
        except ValueError:
            print("Invalid Inputs")

        try:
            seconds = int(seconds)
        except ValueError:
            print("Invalid Inputs")

        self.timer_init

    def timer_init(self):

        hours = self.hour.get()
        minutes = self.hour.get()
        seconds = self.seconds.get()

        if hours != 0:
            hoursToSeconds = int(hours)*60*60
        else:
            hoursToSeconds = 0
        if minutes != 0:
            minutesToSeconds = int(minutes)*60
        else:
            minutesToSeconds = 0

        self.totalTime = hoursToSeconds + minutesToSeconds + seconds
        self.remaining = 0
        self.countdown(10)


    def countdown(self, remaining = None):
        if remaining is not None:
            self.remaining = remaining

        if self.remaining <= 0:
            self.timerLab.configure(text="Time's Up")
        else:
            self.timerLab.configure(text="%d" % self.remaining)
            self.remaining = self.remaining - 1
            self.after(1000, self.countdown)


if __name__ == "__main__":
    app = Timer()
    app.mainloop()

1 个答案:

答案 0 :(得分:3)

你有很多错别字

  1. minutes = self.hour.get()应为minutes = self.minutes.get()
  2. self.timer_init - 遗忘的括号
  3. self.countdown(10) - 您可能想要self.countdown(self.totalTime)或类似的东西
  4. self.totalTime = hoursToSeconds + minutesToSeconds + seconds - 您应该将seconds转换为int
  5. 类型

    但你也有一个错误:

    self.timerLab = Tkinter.Label(self, text="", width=10).pack()
    

    pack()方法返回None。所以,你必须把它改成

    self.timerLab = Tkinter.Label(self, text="", width=10)
    self.timerLab.pack()