Python 3.x 为什么函数在调用之前运行?

Python 3.x 为什么函数在调用之前运行?,python-3.x,tkinter,Python 3.x,Tkinter,我正在制作一个python脚本。这是我的python脚本: import tkinter def print_the_nothing_thing(): print ("I told you, nothing means nothing. For further assistance, please see:") print ("https://www.youtube.com/watch?v=3WNlWyFgLCg") print ("Type the url into

我正在制作一个python脚本。这是我的python脚本:

import tkinter

def print_the_nothing_thing():
    print ("I told you, nothing means nothing. For further assistance, please see:")
    print ("https://www.youtube.com/watch?v=3WNlWyFgLCg")
    print ("Type the url into the browser.")

#setup the window
window=tkinter.Tk()
menu=tkinter.Menu(window)
window.configure(menu=menu)
view=tkinter.Menu(menu)
file=tkinter.Menu(menu)
menu.add_cascade(label="File",menu=file)
menu.add_cascade(label="View",menu=view)
#here is my problem
view.add_command(label="Nothing here!",command=print_the_nothing_thing())
file.add_command(label="Nothing here!",command=print_the_nothing_thing())
helpm.add_command(label="Help",command=helpy())
window.mainloop()
我的问题是,当我定义函数时,它首先运行函数。关闭后,菜单命令将不起任何作用。

您需要从那些
添加命令行中删除
()
,如

view.add_command(label="Nothing here!",command=print_the_nothing_thing)
file.add_command(label="Nothing here!",command=print_the_nothing_thing)

您在那里调用这些函数,它们返回
None
。如果只使用函数的名称,则会得到一个函数对象。

您正在
add\u命令中调用函数:

view.add_command(label="Nothing here!",command=print_the_nothing_thing())
file.add_command(label="Nothing here!",command=print_the_nothing_thing())
helpm.add_command(label="Help",command=helpy())
相反,点
向函数名添加_命令
;不要执行它。 因此:


你有什么证据证明这是真的?它看起来像是
print\u\u nothing\u thing
被调用了两次,然后
helpy
被调用了一次吗?你对tkinter使用了什么教程/信息?我很确定您没有正确阅读文档,并在不应该的地方添加了括号。@ScottHunter是的,
print\u“无任何内容”是两次,
helpy
是一次。在单独的小文件中进行实验是一个很好的实践。坚持下去。除此之外,它还可以让您发布一个几乎最小的实际运行的文件,从而很容易发现问题。更多的初学者也应该这样做,
view.add_command(label="Nothing here!",command=print_the_nothing_thing)
file.add_command(label="Nothing here!",command=print_the_nothing_thing)
helpm.add_command(label="Help",command=helpy)