我试图用Python制作一个闹钟。我遇到的问题在于displayTime
功能。它应该倒计时警报响起的秒数。
我需要将aTime
变量传递给displayTime
函数。但是,如果我将aTime
作为displayTime
函数的参数,那么我就无法使用displayTime
作为timelabel.after(timelabel.after(1000, displayTime(aTime)))
的参数,因为它会使displayTime
成为displayTime
的参数alarm
的实例而不是参数。
由于aTime
嵌套在alarm()
函数中,为什么它不会从aTime
继承displayTime
?
另外,我还可以将from Tkinter import *
import time
def alarm() :
cHours = float(nHours.get())
cHours = cHours * 3600
cMinutes = float(nMinutes.get())
cMinutes = cMinutes * 60
aTime = cHours + cMinutes
print aTime
def displayTime():
timelabel = Label(alarmGUI, text = "%d seconds until it's time to wake up" % aTime)
timelabel.grid(row=9, column=0)
aTime -=1
timelabel.after(1000, displayTime)
displayTime()
alarmGUI = Tk()
alarmGUI.geometry('500x400')
alarmGUI.title("Alarm Clock")
nHours = IntVar()
nMinutes = IntVar()
label1 = Label(alarmGUI, text = "Enter the number of hours and number of minutes that you want to sleep").grid(row=1, column=1)
label2 = Label(alarmGUI, text = "Hours").grid(row = 3, column = 0)
label3 = Label(alarmGUI, text = "Minutes").grid(row = 5, column = 0)
hours = Entry(alarmGUI, textvariable = nHours).grid(row = 3, column = 1)
minutes = Entry(alarmGUI, textvariable = nMinutes).grid(row = 5, column = 1)
button1 = Button(alarmGUI, text = "Start", command = alarm).grid(row = 7, column = 0)
alarmGUI.mainloop()
带入{{1}}吗?除了使用参数之外还有其他方法吗?
以下是代码:
{{1}}