Function 在tkinter中调用函数
对于线路:Function 在tkinter中调用函数,function,user-interface,python-3.x,menu,tkinter,Function,User Interface,Python 3.x,Menu,Tkinter,对于线路: file_Menu.add_command(label = 'New', command = do_Nothing) 为什么函数名为command=do\u Nothing而不是command=do\u Nothing() 整个代码如下: # Tkinter GUI Menu from tkinter import * ### Functions ### # Do Nothing def do_Nothing(): print('I just did... noth
file_Menu.add_command(label = 'New', command = do_Nothing)
为什么函数名为command=do\u Nothing
而不是command=do\u Nothing()
整个代码如下:
# Tkinter GUI Menu
from tkinter import *
### Functions ###
# Do Nothing
def do_Nothing():
print('I just did... nothing')
### Create tkinter window ###
# Create Window
root = Tk()
#### Creating the Menu(s) ###
# Create the Menu Bar
menu_Bar = Menu(master = root)
# Create File Menu
file_Menu = Menu(master = menu_Bar)
### Displaying the Menu(s) ###
# Display Menu Bar
root.config(menu = menu_Bar)
# Display File Menu
menu_Bar.add_cascade(label = 'File', menu = file_Menu)
### File Menu Properties ####
# New
file_Menu.add_command(label = 'New', command = do_Nothing)
# Open
file_Menu.add_command(label = 'Open', command = do_Nothing)
# Exit
file_Menu.add_command(label = 'Exit', command = root.quit)
### Display tkinter window ###
root.mainloop()
Tkinter是普通的python,因此它遵循普通的python规则:如果使用函数名后跟括号,python将立即执行该函数并返回结果 因此,当您这样做时:
file_Menu.add_command(..., command = do_Nothing())
。。。这与您所做的完全相同:
nothing = do_Nothing()
file_Menu.add_command(..., command = nothing)
在绑定或
命令
回调中指定命令时,必须告诉tkinter该命令的名称(或者更准确地说,是对该命令的引用) Tkinter是普通的python,因此它遵循普通的python规则:如果使用函数名后跟括号,python将立即执行该函数并返回结果
因此,当您这样做时:
file_Menu.add_command(..., command = do_Nothing())
。。。这与您所做的完全相同:
nothing = do_Nothing()
file_Menu.add_command(..., command = nothing)
在绑定或命令
回调中指定命令时,必须告诉tkinter该命令的名称(或者更准确地说,是对该命令的引用)