一次配置Tkinter应用程序所有按钮的理想方法?

问题描述 投票:1回答:1

这是我的第一个tkinter / gui项目。我设置了一个mvc模式,并为我的应用程序的每个窗口都有一个视图类。主窗口的示例:

class ViewMain(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master, height = 300, width = 500)
        self.pack(fill='both', expand=True)
        self.BUTTON_SIZE = {'relheight' : 0.3, 'relwidth' : 0.35}
        self._set_widgets()
        self._set_layout()

    def _set_widgets(self):
        self.button = tk.Button(self, text = 'Text')

    def _set_layout(self):
        self.button.place(relx = 0.1, rely = 0.15, **self.BUTTON_SIZE)

[目前,我有6个视图类,我想将按钮的凹凸更改为凹槽。因为我有30多个按钮,所以我一直在寻找一种不写以下内容的方法:

self.button = tk.Button(self, text = 'Text', relief = 'groove')

如您所见,我思路的一个缺陷是我已经在使用重复的方法来配置按钮的大小。但是,让我们忽略它。由于我对所有这些都还很陌生,因此我看到了三种实现方法:

  • 每次创建按钮时,将“ relief ='groove”添加为选项
  • 使用ttk.Button并配置样式。但是我必须为每个视图类都这样做,并且每次创建按钮时都要添加样式]
  • 为tk.Button编写包装,并改用它

最后一个选项使我想到了这一点:

class CustomButton(tk.Button):
def __init__(self, master, text):
    super().__init__(master, text = text, relief = 'groove')

哪个工作,但我不能停止思考是否有更好的方法来解决这个问题?

python tkinter configuration widget
1个回答
0
投票

我没有测试它,但是我的解决方法是这样的:

for widget in root.winfo_children():
    if isinstance(widget, Tkinter.Button):
        widget.configure(relief = 'groove')
© www.soinside.com 2019 - 2024. All rights reserved.