Why put frames and widgets in classes?

前端 未结 1 516
隐瞒了意图╮
隐瞒了意图╮ 2021-01-07 14:37

As the question states, I can\'t seem to fully grasp the point of using classes with tkinter.

I have read through a decent number of different sites but I keep getti

相关标签:
1条回答
  • 2021-01-07 15:15

    Does every widget need to be in its own separate frame that's maybe part of an even bigger frame?

    That's a bit like asking if every part of a mathematical expression needs parenthesis. Strictly speaking, the answer is "no". However, using frames to organize groups of widgets is a tool designed to make writing and understanding the code easier, much like parenthesis in a complex math equation makes writing and understanding the equation easier.

    Can classes have methods that create and place a frame? In addition, can those same classes have methods than can create, modify, and place a widget within the previously made frame?

    Yes, and yes. Methods in a class don't have any limitations on what they can and cannot do. Methods of a class can do anything that a normal function can do.

    I also have some code that allows me to create, modify, and place a widget. Although I know it's not conventional, so I would greatly appreciate some input on this as well. Any suggestions on what you would do with this code to make it better?

    "Better" is highly subjective. What is better for 20 lines of code might not be better for 200, 2,000, or 20,000. What is better for a function used exactly twice might not be better for a function used hundreds or thousands of times (or visa versa).

    That being said, you're doing one thing that is very unconventional and which leads to making your code harder to understand: you're using self as a parameter for a function that is not a method of a class. self means something very specific to python programmers; using it outside of the context of a method is very confusing.

    You should do one of two things for the method layout:

    • Rename self to be widget or any other term other than self
    • Create a base class that defines layout, and then have your classes inherit from the base class. In that case, self is the proper first argument.

    This part of the answer refers to code which was added after I wrote my original answer.

    The base class I was referring to needs to be a separate class. For example:

    class Base():
        def layout(self):
            self.grid(row=0, column=0)
            self.config(bg="blue")
    
    class MyLabels(Base, tk.Label):
        def __init__(self, parent, text, **kwargs):
            tk.Label.__init__(self, parent, text=text)
            self.layout(self, **kwargs)
    
    class MyButtons(Base, tk.Button):
        def __init__(self, parent, text, command, **kwargs):
            tk.Button.__init__(self, parent, text=text, command=command)
            self.layout(self, **kwargs)
    

    This type of class is sometimes called a mixin because it's not designed to be instantiated as a standalone object. Rather, it "mixes in" some additional behavior to other classes. A mixin will typically have methods, but won't have its own __init__.

    0 讨论(0)
提交回复
热议问题