我决定从这个网站上找到的计时器构建,在我对它做任何事情以确保它看起来不错之前,我决定测试它,但是我设置显示的标签不起作用。请注意有些事情可能看起来不合逻辑,但是我只是在尝试各种事情来尝试让它发挥作用
代码:
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()