Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: why must Tkinter class instantiation use a Frame?

If I want to create a Tkinter GUI simply with statements, I can do this:

from Tkinter import *
root = Tk()
root.title("Test Window")
tkFrame = Frame(root)
tkButton = Button(tkFrame)
[...]

The documentation, however, advises that Tkinter be used with a class definition, subclassing a Frame widget:

class App(Frame):
  [...]

I would like to understand why that is so. Why can't we subclass the Frame's container, the window? It appears that is what is done with statements in the first example, so why not in a class definition?

EDIT (following Bryan Oakley's answer):

I would like to instantiate at the highest level of Tkinter, which I assume to be Tk() (though I have come across references stating Frame is the top level, but never mind). Indeed, the following will create a window:

from Tkinter import *
class Application(Tk):
  pass
app = Application()
app.mainloop()

...but as soon as I try to add widgets I either get errors or two windows, with the widgets in a new window, depending on how I structure the code. Here's a basic example that will produce a second window with the button:

from Tkinter import *
class Application(Tk):
  tkBtn = Button()
  tkBtn.pack()
app = Application()
app.mainloop()

Anything more, using self, __init__, etc., produces errors. Could someone point me to working code that instantiates Tkinter at the highest level? Just like all the Frame subclasses I'm seeing, but at the highest level?

like image 740
X-Mann Avatar asked Oct 01 '15 09:10

X-Mann


People also ask

What is the use of frame in tkinter?

The Frame widget is very important for the process of grouping and organizing other widgets in a somehow friendly way. It works like a container, which is responsible for arranging the position of other widgets. It uses rectangular areas in the screen to organize the layout and to provide padding of these widgets.

What is the purpose of the frame's Mainloop method?

mainloop() tells Python to run the Tkinter event loop. This method listens for events, such as button clicks or keypresses, and blocks any code that comes after it from running until you close the window where you called the method.

What is frame class in tkinter?

Tkinter Frame widget is very useful for grouping multiple widgets in a frame. It includes all the functions and properties that applies to the parent window. To create a Frame widget, we can instantiate an object of the Frame class.

What is the difference between tkinter window and frame?

Tk creates the root window. Every tkinter application must have a root window. When you instantiate it you also create a tcl interpreter that is used by tkinter. Frame is just a widget, designed to be a container for other widgets.


1 Answers

There is nothing that says a tkinter class must inherit from a frame. You can inherit from any of the tkinter widgets, or any other classs. If you have found documentation that states otherwise, that documentation is wrong. Using Frame is a logical choice since it is designed to be a container of other widgets, but it is not the only choice.

Personally I inherit from a frame because I find it convenient. Some of my GUIs need the ability to open more than one identical window. By having my main code in a Frame I am able to create multiple windows simply by creating multiple instances of the frame, and packing them in Toplevel widgets.

When you inherit from Tk, you can only have a single instance. In the real world that's usually enough, and there's absolutely nothing wrong with doing it that way. Since I personally write a fair number of tkinter programs, having them all start out exactly the same is convenient for me.

Another good choice is a Canvas since you can easily add a background image, which is not something you can do with a Frame.

Bottom line: you are absolutely not required to inherit from Frame. Inherit from whatever you want.


(the following was written in response to an edit of the original question)

In reference to this code:

from Tkinter import *
class Application(Tk):
  tkBtn = Button()
  tkBtn.pack()
app = Application()
app.mainloop()

The reason you see two windows is that you're not creating the class properly. You need to call the __init__ method of the superclass before creating widgets, because that's what actually creates the root window. Because you don't, you end up with two windows. You get one that is created implicitly when you add a button to a not-yet-constructed root window, and you get another when your subclass finishes initializing.

The solution is to not take shortcuts, and instead initialize the class properly:

from Tkinter import *
class Application(Tk):
    def __init__(self):
        Tk.__init__(self)
        tkBtn = Button()
        tkBtn.pack()
app = Application()
app.mainloop()

Note that this isn't a tkinter-specific problem. When subclassing, unless you have explicit reasons to do otherwise, you always should call the __init__ method of the superclass.

You asked for working examples, here are a couple:

  • https://stackoverflow.com/a/22424245/7432
  • https://stackoverflow.com/a/11405393/7432

You might also want to read the responses in the question Inheriting from Frame or not in a Tkinter application

like image 199
Bryan Oakley Avatar answered Sep 23 '22 03:09

Bryan Oakley