【发布时间】:2019-04-11 16:42:59
【问题描述】:
我正在尝试使用 Tkinter 制作 GUI,但我遇到了一个问题。我的 GUI 将有很多非常相似的按钮和很多选项(字体、宽度、高度、命令等),我宁愿写变量的名称,它存储重复选项而不是一遍又一遍地重复所有命令再次。
我什至不知道这是否可能。
我尝试将选项保存为变量中的字符串,然后将其传递给变量,但它会引发:AttributeError: 'str' object has no attribute 'tk'
这是我的按钮示例:
Num3 = Tk.Button(main, text="3", width = 2, height = 2, font = "Arial 16", command=lambda: nex("3"))
Num4 = Tk.Button(main, text="4", width = 2, height = 2, font = "Arial 16", command=lambda: nex("4"))
我希望它看起来像这样:
Var = 'main, width = 2, height = 2, font = "Arial 16",'
Num3 = Tk.Button(Var, text="3",command=lambda: nex("3"))
Num4 = Tk.Button(Var, text="4",command=lambda: nex("4"))
但它提出了AttributeError: 'str' object has no attribute 'tk'
谢谢大家的回答。
【问题讨论】:
-
不要使用以大写字母开头的变量名。见PEP 8 - Style Guide for Python Code。
标签: python python-3.x tkinter