0

So I came across a Python3 tkinter GUI code snippet and it doesn't have anything like root = Tk() but IT RUNS! I read this and it is really helpful. But my question is, if the tk window and interpreter is initiated when I create my first widget, how can I add more widgets to the root without specifying it? aka. What should I do when I want to add more widgets to the same program / same window, since I don't have a variable like root to store the root window object?

By the way, there was a controller class like this:

class Controller(tk.Tk):
    def __init__ (self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        parentObj = tk.Frame(self)
        self.allFrames = {}
        ...

Does it mean that the parentObj frame is the windows / outmost layer of frame in this app? How do I understand this class definition here? What is tk.Tk.__init__(self, *args, **kwargs) here for?

Young
  • 739
  • 3
  • 9
  • 12
  • you add them to your widget instead of root? or you [get the parent](https://stackoverflow.com/questions/27161734/getting-the-parent-of-a-parent-widget-in-python-tkinter) – Patrick Artner Jan 07 '19 at 18:36
  • @PatrickArtner Getting the parent sounds cool here. Thanks. But I guess I need to understand the class defined here first. It appears to me the root window object (or frames in this particular example) is passed down to all other classes / functions through "self", but I'm not pretty sure about this. – Young Jan 07 '19 at 18:44

1 Answers1

1

Controller is a subclass of tk.Tk. Controller is identical to tk.Tk but with enhancements. Thus, doing something=Controller(...) serves the same purpose as something=tk.Tk().

What should I do when I want to add more widgets to the same program / same window,

Use self as the parent if inside the class, use the instance of the class if outside.

class Controller(tk.Tk):
    def __init__ (self, *args, **kwargs):
        ...
        self.some_widget = tk.Label(self, ...)

... and ...

root = Controller()
some_other_widget = tk.Label(root, ...)

Does it mean that the parentObj frame is the windows / outmost layer of frame in this app?

No. The outmost "layer" is the instance of Controller. That is the root window. parentObj lives inside that window.

What is tk.Tk.__init__(self, *args, **kwargs) here for?

This is just the standard python way for a subclass to initialize its parent class.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Thanks for your answer. What's the purpose of doing this? Why bother creating a subclass of Tk instead of using the parent class Tk? Is there a reason behind all of these that I didn't realize? – Young Jan 07 '19 at 21:01
  • Also, I just want to make sure, is parentObj the out-most layer of frame window in my example? – Young Jan 07 '19 at 21:05
  • @Young: what is the purpose? Personal preference. There's no significant advantage. The author probably thought it made the code easier to read. `parentObj` is the outtermost _frame_, but the outer-most widget is the instance of `Controller` itself. – Bryan Oakley Jan 07 '19 at 21:10
  • (I'm new to tkinter) I'm currently trying to add a scroll bar (or a new page, or anything that the user can jump back and forth), do I need to create a canvas on top of the parent frame or widget? I'm not very clear on how to achieve that. I tried several different ways but nothing has worked out yet. I would appreciate some more suggestions. – Young Jan 07 '19 at 21:22
  • @Young: there's no way that can be answered in a comment. Perhaps your first step should be to work through a python tutorial on how classes work, and then a tkinter tutorial. – Bryan Oakley Jan 07 '19 at 21:24