有没有使用参数将变量传递给函数的方法?

时间:2014-11-12 17:26:23

标签: python parameter-passing

我试图用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}}

0 个答案:

没有答案