Python – Module 17 – Modal Windows in TKInter (Spring 2024)
Welcome to Module 17 of Python programming course! In this module, we will be learning about modal windows in TKInter.
What are Modal Windows?
Modal windows are pop-up dialog boxes that require user interaction before allowing the user to continue interacting with the main application. They are often used to display important information or request user input.
Using Modal Windows in TKInter
TKInter is a built-in Python library for creating graphical user interfaces. Modal windows in TKInter can be created using the Toplevel
class. This class represents a window that appears on top of the main application window and blocks interaction with the main window until the modal window is closed.
Example:
import tkinter as tk def open_modal_window(): modal_window = tk.Toplevel() label = tk.Label(modal_window, text="This is a modal window") label.pack() btn_close = tk.Button(modal_window, text="Close", command=modal_window.destroy) btn_close.pack() root = tk.Tk() btn_open_modal = tk.Button(root, text="Open Modal Window", command=open_modal_window) btn_open_modal.pack() root.mainloop()
Conclusion
Modal windows are a useful way to display information or request user input in a TKInter application. By using the Toplevel
class, you can create modal windows that block interaction with the main application window until the modal window is closed.
Obrigado amigo.. me ajudou.