Python Forum
Help with multi-window system
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with multi-window system
#1
I am developing a multi-window system using Python and tkinter. Though I am, in no way, new to programming (or Python) this aspect is new to me. Previously, when developing multi-window systems I have developed using interfaces such as Visual Studio, etc.

I have undertaken two potential experiments. One where each window is defined within it's own file, as a series of functions (dealing with events, etc), all held together via a single main file. The other is by building each window as a class, then using this within the main file.

At this point I cannot see any considerable issue with either approach; though I seem to find that, due tohaving independent imports of tkinter, the class methodology results in windows that are always independent of eachother; so I cannot yet get the main window to close all other, open windows (I feel this is something I should be able to define as a closing function).

Sorry for the vagueness but before I go down a terrible rabbit hole, is there a preferred option that those with experience would recommend? Is it one of the two I have opted for, or is there a third one? Or does this purely come down to personal preferences?
Reply
#2
Multiple windows in a GUI has fallen out of favor and most designs have one window with a way to move between multiple views.

In tkinter you can have multiple top level windows but only one root/main window. The root window is made by calling Tk(). This creates a window and initializes the event loop. Additional top level windows are made using TopLevel(). When you exit the root window you exit Tk and all the other top level windows close automatically.

I don't understand your "first experiment". Are your running multiple Python executables, multiple threads, multiple processes? You can only run one mainloop() and you can only create one root window in Python. How are you running these independent windows?
Reply
#3
(Mar-14-2022, 03:54 PM)deanhystad Wrote: Multiple windows in a GUI has fallen out of favor and most designs have one window with a way to move between multiple views.

In tkinter you can have multiple top level windows but only one root/main window. The root window is made by calling Tk(). This creates a window and initializes the event loop. Additional top level windows are made using TopLevel(). When you exit the root window you exit Tk and all the other top level windows close automatically.

I don't understand your "first experiment". Are your running multiple Python executables, multiple threads, multiple processes? You can only run one mainloop() and you can only create one root window in Python. How are you running these independent windows?

Sorry, yes, I wasn't clear.

When I refer to experiments, I simply mean me trying out ideas. I am not using any threading, etc. I simply created a class that represented the graph window, one that represented the data window, and so on. The thing is, for me to get the referencing for each window to work I had to import tkinter into each class file. An example, is the initial graph window class that was like this...

import tkinter as tk
from tkinter import *
from tkinter import ttk


class graph():

    def __init__(self):
        gw = tk.Tk()
        gw.title("Graph")
        gw.geometry("400x400")
        gw.mainloop()


def main():
    graph()


if __name__ == '__main__':
    main()
My main page then contained this...

import tkinter as tk
from tkinter import *
from tkinter import Menu
from tkinter import filedialog

import graph_windows


# root window
def main():
    mw.title("STELCOR Data Analyser")
    mw.geometry("1000x500")
    menu()
    just_data_label = Label(mw, text="Data label", width=100, height=4, fg="blue")
    just_data_label.grid(column=2, row=2)
    graph.main()
    mw.mainloop()
    


dataFile = []
mw = tk.Tk()
if __name__ == '__main__':
    main()
However, I think you are correct. I used to develop within the single window, with multiple views (I remember using the tabs system in VB once). The multi-window approach felt like quite a nice way of working but I am already finding that tkinter's ability to control the window structure a little restrictive (removing the control box from the top bar of each window, without making the window fixed in position and size is one such restriction).

I think I will go back to the design stage and think about how I might best do this within a single window.
Reply
#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


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 1,799 Oct-02-2023, 04:11 PM
Last Post: deanhystad
  Pyspark Window: perform sum over a window with specific conditions Shena76 0 1,185 Jun-13-2022, 08:59 AM
Last Post: Shena76
  Difference between os.system("clear") and os.system("cls") chmsrohit 7 16,659 Jan-11-2021, 06:30 PM
Last Post: ykumar34
Question Difference between Python's os.system and Perl's system command Agile741 13 6,860 Dec-02-2019, 04:41 PM
Last Post: Agile741
  run command on system with multi qoutes evilcode1 16 6,556 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