Python Forum
Help with multi-window system
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with multi-window system
#4
The problem is not importing tkinter, it is calling Tk() and mainloop(). When you call Tk() it not only creates a top level window, but initializes tkinter. Doing this more than once leads to unpredictable results. mainloop() runs until the application is shut down. You could only run one mainloop(). Which of the many root windows would you choose to be THE root.

Your second experiment should work. You create a root window using Tk() and additional windows using TopLevel.
import tkinter as tk

class MainWindow(tk.Tk):
    def __init__(self, *args, title="Main Window", **kwargs):
        super().__init__(*args, **kwargs)
        self.title(title)
        tk.Label(self, font=(None, 20), text= \
            "This is the main window.\n"
            "It needs to be created first.") \
            .pack(padx=20, pady=20)

        tk.Button(text="Push me to make a window", command=lambda: SecondWindow()) \
            .pack(padx=20, pady=20)

        tk.Button(text="Push me to quit", command=self.quit) \
            .pack(padx=20, pady=20)

class SecondWindow(tk.Toplevel):
    def __init__(self, *args, title="Other Window", **kwargs):
        super().__init__(*args, **kwargs)
        self.title(title)
        tk.Label(self, font=(None, 20), text= \
            "This is a toplevel window.\n"
            "Create this after the main window.") \
            .pack(padx=20, pady=20)

MainWindow().mainloop()
Reply


Messages In This Thread
Help with multi-window system - by garynewport - Mar-14-2022, 01:01 PM
RE: Help with multi-window system - by deanhystad - Mar-14-2022, 03:54 PM
RE: Help with multi-window system - by garynewport - Mar-15-2022, 09:59 AM
RE: Help with multi-window system - by deanhystad - Mar-16-2022, 03:05 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there a way to call and focus any popup window outside of the main window app? Valjean 6 2,161 Oct-02-2023, 04:11 PM
Last Post: deanhystad
  Pyspark Window: perform sum over a window with specific conditions Shena76 0 1,287 Jun-13-2022, 08:59 AM
Last Post: Shena76
  Difference between os.system("clear") and os.system("cls") chmsrohit 7 16,922 Jan-11-2021, 06:30 PM
Last Post: ykumar34
Question Difference between Python's os.system and Perl's system command Agile741 13 7,135 Dec-02-2019, 04:41 PM
Last Post: Agile741
  run command on system with multi qoutes evilcode1 16 6,940 Aug-31-2018, 05:19 PM
Last Post: evilcode1

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020