Creating the main window: Tk().
Creation of the modal window: Toplevel().

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
from tkinter import * from tkinter import ttk def popup(): info = Toplevel() # Popup -> Toplevel() info.geometry('200x100') info.title('Hello') 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() pack.title('Main window') pack.geometry('300x100') 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.
Finally, the 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.