繁体   English   中英

Python3:使用 exec() 创建函数

[英]Python3: use exec() to create a function

我正在使用 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()

  • 函数不必按顺序命名。 您也可以将函数对象存储在循环变量中,并使用该循环变量来创建 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_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(),除非你完全知道它有什么安全问题,你知道该产品不会在另一台机器上使用,或者你真的别无选择。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM