Skip to main content
added 233 characters in body
Source Link
Bryan Oakley
  • 384.7k
  • 53
  • 568
  • 726

No, it should not be self.parent.

In the class example you give, the class is itself a frame. It is designed this way to make the example self-contained. YouBy inheriting from Frame you can take all of the code in that class and put it anywhere in the GUI. You can think of the class and everything in it as a single custom widget. You could have multiple of these classes, and each one can be treated as a single GUI object.

To make that work, the class only ever puts widgets inside itself, not in its parent.

The entire purpose of using a sublcass of Frame is to act as a container for other widgets. If you don't plan on using it as a container for other widgets, there's no point in inheriting from Frame.

It is the equivalent of this, without classes:

import tkinter as tk

root = tk.Tk()
frame = tk.Frame(root)
frame.pack(...)
label = tk.Label(frame, text = "something", background = "something")
label.pack(...)

If you wanted the class to put widgets in the parent, you would define the class like the following. Notice that it inherits from object rather than Frame:

class Example(object):
    def __init__(self, parent):
        self.parent = parent
        ...
        label = tk.Label(parent, ...)

No, it should not be self.parent.

In the class example you give, the class is itself a frame. It is designed this way to make the example self-contained. You can take all of the code in that class and put it anywhere in the GUI. You can think of the class and everything in it as a single custom widget. You could have multiple of these classes, and each one can be treated as a single GUI object.

To make that work, the class only ever puts widgets inside itself, not in its parent.

It is the equivalent of this, without classes:

import tkinter as tk

root = tk.Tk()
frame = tk.Frame(root)
frame.pack(...)
label = tk.Label(frame, text = "something", background = "something")
label.pack(...)

If you wanted the class to put widgets in the parent, you would define the class like the following. Notice that it inherits from object rather than Frame:

class Example(object):
    def __init__(self, parent):
        self.parent = parent
        ...
        label = tk.Label(parent, ...)

No, it should not be self.parent.

In the class example you give, the class is itself a frame. It is designed this way to make the example self-contained. By inheriting from Frame you can take all of the code in that class and put it anywhere in the GUI. You can think of the class and everything in it as a single custom widget. You could have multiple of these classes, and each one can be treated as a single GUI object.

To make that work, the class only ever puts widgets inside itself, not in its parent.

The entire purpose of using a sublcass of Frame is to act as a container for other widgets. If you don't plan on using it as a container for other widgets, there's no point in inheriting from Frame.

It is the equivalent of this, without classes:

import tkinter as tk

root = tk.Tk()
frame = tk.Frame(root)
frame.pack(...)
label = tk.Label(frame, text = "something", background = "something")
label.pack(...)

If you wanted the class to put widgets in the parent, you would define the class like the following. Notice that it inherits from object rather than Frame:

class Example(object):
    def __init__(self, parent):
        self.parent = parent
        ...
        label = tk.Label(parent, ...)
Source Link
Bryan Oakley
  • 384.7k
  • 53
  • 568
  • 726

No, it should not be self.parent.

In the class example you give, the class is itself a frame. It is designed this way to make the example self-contained. You can take all of the code in that class and put it anywhere in the GUI. You can think of the class and everything in it as a single custom widget. You could have multiple of these classes, and each one can be treated as a single GUI object.

To make that work, the class only ever puts widgets inside itself, not in its parent.

It is the equivalent of this, without classes:

import tkinter as tk

root = tk.Tk()
frame = tk.Frame(root)
frame.pack(...)
label = tk.Label(frame, text = "something", background = "something")
label.pack(...)

If you wanted the class to put widgets in the parent, you would define the class like the following. Notice that it inherits from object rather than Frame:

class Example(object):
    def __init__(self, parent):
        self.parent = parent
        ...
        label = tk.Label(parent, ...)