我正在使用tkinter创建一个应用程序,目前我制作了很多按钮,所以我需要用不同的命令绑定所有按钮,我想使用exec()
来创建函数。
strategy=None
exec("global commandbutton"+str(len(strategicpoint)+1)+"\ndef commandbutton"+str(len(strategicpoint)+1)+"():\n\tglobal strategy\n\tstrategy="+str(len(strategicpoint)))
commandline=eval('commandbutton'+str(len(strategicpoint)+1))
imgx=tk.Button(win,image=towert,command=commandline)
更清洁的解决方案
global commandbutton{...}
def commandbutton{...}():
global strategy
strategy={...}
我希望我的代码像上面一样运行并运行,但后来我调用命令并测试print(strategy)
,(我点击了按钮/调用了命令)它打印None
时我想要打印别的东西。
绝对没有必要在这里使用exec()
或eval()
。
exec
,或者仅使用lambda函数或functools.partial()
中的参数绑定。所以,如果你有一个递增的strategicpoint
值的循环,我只是这样做:
def set_strategy(point):
global strategy
strategy = point
buttons = []
for strategicpoint in range(1, number_of_points + 1):
imgx = tk.Button(win, image=towert, command=lambda point=strategicpoint: set_strategy(point))
buttons.append(imgx)
lambda point=...
部分将当前循环值绑定为point
创建的新函数对象的lambda
参数的默认值。当没有参数调用该函数时(如单击按钮时那样),则新函数使用当时分配给strategicpoint
的整数值来调用set_strategy(point)
。
您还可以使用闭包,外部函数中的局部变量,内部函数可以使用。每次调用外部函数时,都会创建外部函数内部的嵌套内部函数,因此它们与同一外部函数创建的其他函数对象是分开的:
def create_strategy_command(strategypoint):
def set_strategy():
global strategy
strategy = strategypoint
return set_strategy
然后在创建按钮时,使用:
imgx = tk.Button(win, image=towert, command=create_strategy_command(strategicpoint))
请注意,调用create_strategy_command()
函数会在此返回一个新函数,用作按钮命令。
免责声明:我没有测试过这个。
使用字典存储所有功能,如下所示:
option = "default"
def change_option(target):
global option
option = target
def f1():
print("foo")
def f2():
print("bar")
my_functions = {
"select1": f1,
"select2": f2
"default": None
}
imgx=tk.Button(win,image=towert,command=my_functions[option]) # None
swapper = tk.Button(win, image=towert, lambda: change_option("select2")) # button to change the command if you want it
imgx=tk.Button(win,image=towert,command=my_functions[option]) # print("bar")
change_option("select1")
imgx=tk.Button(win,image=towert,command=my_functions[option]) # print("foo")
你可以在不使用字典的情况下过关,但在我看来这很干净。不要使用exec()或eval(),除非你完全知道它有什么安全问题,你知道产品不会在另一台机器上使用,或者你真的没有其他选择。