![]() |
VOOZH | about |
Tkinter is the most commonly used GUI (Graphical User Interface) library in Python. It is simple, easy to learn and comes built-in with Python. The name "Tkinter" comes from the tk interface, which is the underlying toolkit it uses.
To create multiple windows in a Tkinter application, we use the Toplevel widget. It functions similarly to a Frame, but it opens in a separate window with properties like a title bar, minimize, maximize and close buttons, just like the main application window.
In this guide, we will explore how to open a new window when a button is clicked using two approaches:
Tkinter comes pre-installed with Python. However, on some Linux distributions like Ubuntu, we may need to install it using:
sudo apt-get install python-tk
This approach creates a new window when a button is clicked using the Toplevel widget. Here is the code:
Output:
Explanation:
This approach creates a dedicated class for new windows, making the code more reusable and organized. Here is the code:
Output:
Explanation:
1. Class-Based Window (NewWindow)
2. Binding Click Event (bind()): Instead of using command=, we use .bind("<Button>", lambda e: NewWindow(master)), meaning the button click event creates a new window.