Last Updated : 12 Jul, 2025
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.
What is the Toplevel WidgetIn 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:
Creating a New Window Using a Functionsudo apt-get install python-tk
This approach creates a new window when a button is clicked using the Toplevel widget. Here is the code:
Python
from tkinter import *
from tkinter.ttk import *
# Create the main window
master = Tk()
master.geometry("300x200") # Set window size
master.title("Main Window")
# Function to open a new window
def open_new_window():
new_window = Toplevel(master) # Create a new window
new_window.title("New Window")
new_window.geometry("250x150")
Label(new_window, text="This is a new window").pack(pady=20)
# Create a label and a button to open the new window
Label(master, text="This is the main window").pack(pady=10)
Button(master, text="Open New Window", command=open_new_window).pack(pady=10)
# Run the Tkinter event loop
master.mainloop()
Output:
Explanation:
This approach creates a dedicated class for new windows, making the code more reusable and organized. Here is the code:
Python
from tkinter import *
from tkinter.ttk import *
# Class for creating a new window
class NewWindow(Toplevel):
def __init__(self, master=None):
super().__init__(master)
self.title("New Window")
self.geometry("250x150")
Label(self, text="This is a new window").pack(pady=20)
# Create the main window
master = Tk()
master.geometry("300x200")
master.title("Main Window")
Label(master, text="This is the main window").pack(pady=10)
# Create a button to open the new window using the class
btn = Button(master, text="Open New Window")
btn.bind("<Button>", lambda e: NewWindow(master)) # Bind the event
btn.pack(pady=10)
# Run the Tkinter event loop
master.mainloop()
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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4