我想知道为什么myBtn = Button(text="btn", command=doSomething)
与myBtn = Button(text="btn", command=doSomething())
不同。你知道吗
当我创建一个按钮并添加命令doSomehting,myBtn = Button(text="btn", command=doSomething)
时,我得到了预期的行为-它做了一些事情。你知道吗
但是,当我创建按钮并添加命令doSomething()
,myBtn = Button(text="btn", command=doSomething())
时,它会得到意外的行为—它调用doSoemthing()
,单击按钮什么也不做。你知道吗
为什么会这样?
from tkinter import *
import datetime
import random
def printRandNum():
random.seed()
val = random.randint(1, 1000)
print(val)
top = Tk()
now = datetime.datetime.now()
top.title("Test Window")
top.geometry('800x600')
name = Label(top, text="Alex", fg="Orange", font="monospace 36 bold", anchor="w", width=50)
theDate = Button(top, text="Click here for the date", command=printRandNum)
theDate.pack(side=BOTTOM)
name.pack(side=LEFT)
top.mainloop()
在编写
doSomething, myBtn = Button(text="btn", command=doSomething
时,将doSomething
函数作为参数传递,这样Button
可以在单击时调用它。编写myBtn = Button(text="btn", command=doSomething())
时,将函数的返回值作为参数传递,这意味着Button
对象不能调用doSomething
函数,因为它只获取其返回值。你知道吗相关问题 更多 >
编程相关推荐