为什么这段代码不起作用
objects=[
('o','label', 0, 1),
('g','label', 0, 2),
('e','label', 0, 3),
('r','label', 1, 0),
('w','label', 2, 0),
('x','label', 3, 0),
]
for thing in objects:
name = thing[0]
type = thing[1]
name = tk.type()
我正在尝试使用列表来定义我的 tkinter 小部件,以保持代码整洁。第一项是名称,然后是类型。我不断收到此错误。
module has no attribute type
我知道我做错了什么,只是不知道如何解决。如何使用存储在变量中的值来创建小部件?
所以我希望将 name = tk.type()
解释为 o = tk.label()
感谢您的帮助。
最佳答案
它不起作用,因为列表中的第二列包含字符串,而不是类。也许您应该将实际的类存储在对象列表中:
import Tkinter as tk
...
objects=[
('o',tk.Label, 0, 1),
('g',tk.Label, 0, 2),
('e',tk.Label, 0, 3),
('r',tk.Label, 1, 0),
('w',tk.Label, 2, 0),
('x',tk.Label, 3, 0),
]
关于python - tkinter 中的变量替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18177986/