String from Object Python
本问题已经有最佳答案,请猛点这里访问。
我正在开发一个使用XML创建TkinterGUI的项目,需要知道如何将对象名转换为字符串。例如:
1 2 3 4 5 6 7 | # In the actual program, the value of widget variables is set by the values of XML attributes label_name ="mylabel" root = Tk() exec(label_name+" = Label("+str(root)+", text='Hello World')") |
使用
只是
1 | exec(label_name +" = Label(root, text='Hello World')") |
演示:
1 2 3 4 5 | In [31]: root = tkinter.Tk() In [32]: from tkinter import Label In [33]: exec("label_name = Label(root, text='Hello World')") In [34]: print(label_name.grid_size()) (0, 0) |