Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.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
Python3:使用exec()创建函数_Python_Python 3.x_Tkinter_Windows 7_Exec - Fatal编程技术网

Python3:使用exec()创建函数

Python3:使用exec()创建函数,python,python-3.x,tkinter,windows-7,exec,Python,Python 3.x,Tkinter,Windows 7,Exec,我正在使用tkinter创建一个应用程序,目前我制作了许多按钮,因此我需要使用不同的命令绑定所有按钮,我将使用exec()创建函数 策略=无 执行(“全局命令按钮”+str(len(strategicpoint)+1)+“\ndef commandbutton”+str(len(strategicpoint)+1)+”():\n\t全局策略\n\tstrategy=“+str(len(strategicpoint))) 命令行=eval('commandbutton'+str(len(strat

我正在使用tkinter创建一个应用程序,目前我制作了许多按钮,因此我需要使用不同的命令绑定所有按钮,我将使用
exec()
创建函数

策略=无
执行(“全局命令按钮”+str(len(strategicpoint)+1)+“\ndef commandbutton”+str(len(strategicpoint)+1)+”():\n\t全局策略\n\tstrategy=“+str(len(strategicpoint)))
命令行=eval('commandbutton'+str(len(strategicpoint)+1))
imgx=tk.按钮(win,image=towert,command=commandline)
对于更清洁的解决方案:

全局命令按钮{…}
def命令按钮{…}():
全球战略
策略={…}

我希望我的代码像上面那样运行,它也会运行,但后来我调用命令并测试到
print(strategy)
,(我单击了按钮/调用了命令)当我想打印其他内容时,它会打印
None

这里绝对不需要使用
exec()
eval()

  • 函数不必按顺序命名。也可以将函数对象存储在循环变量中,并使用该循环变量创建tkinter挂钩
  • 您可以使用绑定参数创建函数,而不使用
    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=…
部分将当前循环值绑定为
lambda
创建的新函数对象的
point
参数的默认值。如果在没有参数的情况下调用该函数(如单击按钮时所做的),则新函数将使用当时分配给
strategicpoint
的整数值来调用
set\u策略(点)

您还可以使用闭包,即外部函数中的局部变量,内部函数可以使用闭包。每次调用外部函数时,都会创建外部函数中的嵌套内部函数,因此它们与由同一外部函数创建的其他函数对象是分开的:

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\u strategy\u command()
函数将在此处返回一个新函数,用作按钮命令。

免责声明:我尚未测试此功能

使用字典存储所有函数,例如:

option=“默认”
def更改_选项(目标):
全局选项
选项=目标
def f1():
打印(“foo”)
def f2():
打印(“条”)
my_函数={
“选择1”:f1,
“选择2”:f2
“默认”:无
}
imgx=tk.按钮(win,image=towert,command=my_函数[选项])#无
swapper=tk.Button(win,image=towert,lambda:change_选项(“select2”))#按钮可根据需要更改命令
imgx=tk.按钮(win,image=towert,command=my_函数[选项])#打印(“条”)
更改选项(“选择1”)
imgx=tk.按钮(win,image=towert,command=my_函数[选项])#打印(“foo”)

你可能不用字典也能过得去,但在我看来这是相当干净的。永远不要使用exec()或eval(),除非您完全知道它有什么安全隐患,您知道该产品不会在其他机器上使用,或者您真的没有其他选择

这看起来是个糟糕的主意,糟糕的主意。不要这样做。我必须尝试一下,否则我将需要复制和粘贴许多使我的项目更大的函数:(哇,谢谢,伙计,它工作了!!!Lambda太棒了!虽然我不使用它:)[cuz我吸Lambda]直到你可以从函数返回函数为止。对于给定的场景来说,这是聪明和有用的。当我做下一个项目(sudoko)时,这会很有用。谢谢你的回答,但我的意思是我需要创建很多函数,上面的答案是有效的,如果我可以投票,我可能会投票给你,但我不能:)正如我所说,我在tkinter中做了很多按钮:)