简体   繁体   English

如何更改 Tkinter 框架子类的属性?

[英]How to change the attributes of a Tkinter Frame subclass?

Context语境

I am developing an app with Tkinter in Python 3.8 for some school students, that gets them to input some working out to a maths problem, then checks whether they are correct.我正在为一些学生在 Python 3.8 中使用 Tkinter 开发一个应用程序,让他们输入一些解决数学问题的方法,然后检查它们是否正确。

Approach方法

Each widget (or groups of widgets) are organised onto a ttk.Frame subclass, which is then instantiated in a MainWindow class that organises them using the.grid() geometry manager.每个小部件(或小部件组)都组织到一个 ttk.Frame 子类中,然后在 MainWindow class 中实例化,使用 .grid() 几何管理器组织它们。 For example:例如:

class Questions(ttk.Frame):
    def __init__(self, master):
        super().__init__(master)
        question_style = ttk.Style(self)
        question_style.configure("q.TLabel", padding=30)
        question = ttk.Label(self, text=f"{QA[q_number][0]}",
                             justify=tk.CENTER, style="q.TLabel")
        question.pack(fill=tk.BOTH)

The above Questions class is responsible for displaying the questions.以上问题class负责展示问题。 I have taken the same approach for pretty much every widget - navigation buttons, entry fields, etc. I took this approach because I thought it would afford me the greatest design flexibilities, as not only could I change the appearance of each widget, but also the frame on which they were placed (from my experience, school students can be very aesthetically picky).我对几乎每个小部件都采用了相同的方法——导航按钮、输入字段等。我采用这种方法是因为我认为它可以为我提供最大的设计灵活性,因为我不仅可以更改每个小部件的外观,而且还可以放置它们的框架(根据我的经验,学生在审美上可能非常挑剔)。

Problem问题

I cannot figure out how to change the attributes of the Frame on which the widgets are placed.我不知道如何更改放置小部件的 Frame 的属性。 For instance, how would I change the height and width of the frame?例如,我将如何更改框架的高度和宽度? I have tried the following, to no avail:我尝试了以下方法,但无济于事:

class Questions(ttk.Frame):
    def __init__(self, master):
        super().__init__(master)

        self.height = 50
        self.width = 50

        question_style = ttk.Style(self)
        question_style.configure("q.TLabel", padding=30)
        question = ttk.Label(self, text=f"{QA[q_number][0]}",
                             justify=tk.CENTER, style="q.TLabel")
        question.pack(fill=tk.BOTH)

I have also tried self.configure(height=50) but that didn't work either.我也试过self.configure(height=50)但这也没有用。

How can I change the frame on which these widgets are placed?如何更改放置这些小部件的框架? Is there a better way to tackle this?有没有更好的方法来解决这个问题? Should I abandon the Frame subclass approach?我应该放弃 Frame 子类方法吗?

Any help is greatly appreciated.任何帮助是极大的赞赏。 Thanks.谢谢。

ps I wasn't quite sure how to word this question so any advice on that is also appreciated. ps 我不太确定如何表达这个问题,因此对此问题的任何建议也值得赞赏。

I do not know Tkinter much, but have you tried by passing the parameters to the constructor of the superclass?我不太了解 Tkinter,但是您是否尝试过将参数传递给超类的构造函数?

super().__init__(self, width=50, height=50)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM