我决定使用本网站上的计时器构建,在我做任何事情以确保它看起来没问题之前,我决定测试它,但是我设置显示的标签不起作用。请注意,有些事情可能看似不合逻辑,但我只是尝试了各种各样的事情来尝试让它发挥作用
代码:
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()
答案 0 :(得分:3)
你有很多错别字
minutes = self.hour.get()
应为minutes = self.minutes.get()
self.timer_init
- 遗忘的括号self.countdown(10)
- 您可能想要self.countdown(self.totalTime)
或类似的东西self.totalTime = hoursToSeconds + minutesToSeconds + seconds
- 您应该将seconds
转换为int
但你也有一个错误:
self.timerLab = Tkinter.Label(self, text="", width=10).pack()
pack()
方法返回None
。所以,你必须把它改成
self.timerLab = Tkinter.Label(self, text="", width=10)
self.timerLab.pack()