我试图用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'
谢谢大家的回答
把它们保存在字典里,像这样
看这个问题How to pass dictionary items as function arguments in python?
相关问题 更多 >
编程相关推荐