Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Function 在tkinter中调用函数_Function_User Interface_Python 3.x_Menu_Tkinter - Fatal编程技术网

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该命令的名称(或者更准确地说,是对该命令的引用)