简体   繁体   English

如何将变量分配给 eval / exec 值?

[英]How do i assign a variable to a eval / exec value?

is it possible to assign a variable to a exec / eval value?是否可以将变量分配给 exec / eval 值? this is what i tried这是我试过的

def execute ():
    run_wn = tk.Tk() 
    des1 = tk.Label(run_wn, text = "------------------------------").pack()
    run_value = exec(cmd.get("1.0", "end"))
    value = tk.Label(run_wn, text = run_value)
    des2 = tk.Label(run_wn, text = "------------------------------").pack()
    run_wn.mainloop
bt = tk.Button(wn, text = "run", command = execute).pack()

the cmd is a text box.. when i execute this code then it does not return anything in the run screen. cmd 是一个文本框。当我执行此代码时,它不会在运行屏幕中返回任何内容。 it prints it is there any way you can asign a value to a exec / eval value and and use it as text in a Label??它打印它有什么方法可以将值分配给 exec / eval 值并将其用作 Label 中的文本?

this is just a experimental project..这只是一个实验项目..

help will be very appreciated!!帮助将不胜感激!

thanks谢谢

exec takes two other positional arguments, dictionaries for globals and locals, so you could exec like this to capture results in a dict: exec需要另外两个位置 arguments,用于全局和本地的字典,因此您可以像这样执行以在 dict 中捕获结果:

In [1]: ns = {}
In [2]: exec("p = 1 + 2", globals(), ns)

In [3]: ns
Out[3]: {'p': 3}

Exec'ing untrusted code is dangerous.执行不受信任的代码是危险的。 Don't do it if you're mixing user input into the commands unless you really know what you're doing.如果您将用户输入混合到命令中,请不要这样做,除非您真的知道自己在做什么。

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

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