Creating the main window: Tk().
Creation of the modal window: Toplevel().
from tkinter import *
from tkinter import ttk
info = Toplevel() # Popup -> Toplevel()
ttk.Button(info, text='Quit', command=info.destroy).pack(padx=10, pady=10)
info.transient(pack) #Popup reduction impossible
info.grab_set() #Interaction with window impossible game
pack = Tk() #Main window -> Tk()
Button(pack, text='Open popup', command=popup).pack(padx=10, pady=10)
pack.mainloop() #Only for the main window
This code creates a simple Tkinter GUI with a main window and a pop-up window. The main window contains a button that opens the pop-up window when clicked.
The main window is created using the
Tk class, and the pop-up window is created using the
Toplevel class. The
Toplevel class is used to create a window that is independent of the main window, but is still a part of the same application.
The pop-up window contains a button that closes the window when clicked. The
transient method is used to make the pop-up window a “transient” window, which means that it will stay on top of the main window and cannot be minimized independently. The
grab_set method is used to disable interaction with the main window while the pop-up window is open.
mainloop function is called on the main window to start the Tkinter event loop and handle events for the main window.
Warning: only one loop mainloop() corresponding to the main window.
The main_window_name.wait_window (popup window_name) method stops the main script until the popup window is closed.
The pop_popup.grab_set () method prevents any interaction with the main window.