Why are multiple instances of Tk discouraged?

Posted on

Solving problem is about exposing yourself to as many situations as possible like Why are multiple instances of Tk discouraged? and practice these strategies over and over. With time, it becomes second nature and a natural way you approach any problems in general. Big or small, always start with a plan, use other strategies mentioned here till you are confident and ready to code the solution.
In this post, my aim is to share an overview the topic about Why are multiple instances of Tk discouraged?, which can be followed any time. Take easy to follow this discuss.

Why are multiple instances of Tk discouraged?

Consider below example:

import tkinter as tk
root = tk.Tk()
root.title("root")
other_window = tk.Tk()
other_window.title("other_window")
root.mainloop()

and also see below example that creates instances of Tk back-to-back instead of at once, so there’s exactly one instance of Tk at any given time:

import tkinter as tk
def create_window(window_to_be_closed=None):
    if window_to_be_closed:
        window_to_be_closed.destroy()
    window = tk.Tk()
    tk.Button(window, text="Quit", command=lambda arg=window : create_window(arg)).pack()
    window.mainloop()
create_window()
  • Why is it considered bad to have multiple instances of Tk?
  • Is the second snippet considered a bit better, or does it suffer from
    the same conditions the first code does?
Asked By: Nae

||

Answer #1:

Why is it considered bad to have multiple instances of Tk?

Tkinter is just a python wrapper around an embedded Tcl interpreter that imports the Tk library. When you create a root window, you create an instance of a Tcl interpreter.

Each Tcl interpreter is an isolated sandbox. An object in one sandbox cannot interact with objects in another. The most common manifestation of that is that a StringVar created in one interpreter is not visible in another. The same goes for widgets — you can’t create widgets in one interpreter that has as a parent widget in another interpreter. Images are a third case: images created in one cannot be used in another.

From a technical standpoint, there’s no reason why you can’t have two instances of Tk at the same time. The recommendation against it is because there’s rarely an actual need to have two or more distinct Tcl interpreters, and it causes problems that are hard for beginners to grasp.

Is the second snippet considered a bit better, or does it suffer from the same conditions the first code does?

It’s impossible to say whether the second example in the question is better or not without knowing what you’re trying to achieve. It probably is not any better since, again, there’s rarely ever a time when you actually need two instances.

The best solution 99.9% of the time is to create exactly one instance of Tk that you use for the life of your program. Quite simply, that is how tkinter and the underlying Tcl/Tk interpreter was designed to be used.

Answered By: Nae

Answer #2:

The best reference I’ve found so far is this section in the tkinterbook.

In the simple examples we’ve used this far, there’s only one window on the screen; the root window. This is automatically created when you call the Tk constructor

and

If you need to create additional windows, you can use the Toplevel widget. It simply creates a new window on the screen, a window that looks and behaves pretty much like the original root window

My take on it is that a Tk instance creates a Toplevel widget, plus things like the mainloop, of which there should be only one.

Answered By: Bryan Oakley

Answer #3:

Tk() initializes the hidden tcl interpreter so that the code can run, as Tkinter is just a wrapper around tcl/tk. It also automatically creates a new window. Toplevel() just creates a new window, and wont work if Tk() hasn’t been instantiated, as it requires the tcl interpreter that Tk() initializes. You cannot create any Tkinter widgets without instantiating Tk(), and Toplevel is merely a widget. In the question, you use Tk() to create a second window. You should instead create another file, because initializing the tcl interpreter multiple times can get confusing, as @Bryan Oakley explains so well. Then you should do:

from os import startfile
startfile(nameOfTheOtherFile)

, because, as Toplevel() is just a widget, it closes when the Tk() window is closed. Having the other window in a separate file makes it less confusing.

Answered By: Roland Smith
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.