Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python | Add image on a Tkinter button
Next article icon

Tkinteris the most commonly usedGUI (Graphical User Interface) library inPython. It is simple, easy to learn and comes built-in with Python. The name "Tkinter" comes from thetk 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 Widget

  • Toplevel is used to create a new independent window in a Tkinter application.
  • It acts like the main window, having its own title bar, minimize, maximize and close buttons.
  • It can be used for pop-ups, dialogs or additional windows in the application.

In this guide, we will explore how to open a new window when a button is clicked using two approaches:

  1. Using a function to create a new window
  2. Using a class-based approach for better modularity

Setting Up Tkinter

Tkinter comespre-installed withPython. However, on some Linux distributions likeUbuntu, wemay need to install it using: 

sudo apt-get install python-tk 

Creating a New Window Using a Function

This approach creates a new window when a button is clicked using the Toplevel widget. Here is the code:

Python
fromtkinterimport*fromtkinter.ttkimport*# Create the main windowmaster=Tk()master.geometry("300x200")# Set window sizemaster.title("Main Window")# Function to open a new windowdefopen_new_window():new_window=Toplevel(master)# Create a new windownew_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 windowLabel(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 loopmaster.mainloop()

Output: 

Explanation:

  • Toplevel(master): Creates a new window that is independent of the main window.
  • new_window.title("New Window"): Sets the window title.
  • new_window.geometry("250x150"): Defines the size of the new window.
  • Label(...).pack(): Displays text inside the new window.
  • command=open_new_window: Calls the function when the button is clicked.

Using a Class-Based Approach

This approach creates a dedicated class for new windows, making the code more reusable and organized. Here is the code:

Python
fromtkinterimport*fromtkinter.ttkimport*# Class for creating a new windowclassNewWindow(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 windowmaster=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 classbtn=Button(master,text="Open New Window")btn.bind("<Button>",lambdae:NewWindow(master))# Bind the eventbtn.pack(pady=10)# Run the Tkinter event loopmaster.mainloop()

Output: 

Explanation:

1. Class-Based Window (NewWindow)

  • Inherits from Toplevel, so every instance behaves as a separate window.
  • Defines window properties like title, size and a label inside the window.

2. Binding Click Event (bind()): Instead of usingcommand=, we use.bind("<Button>", lambda e: NewWindow(master)), meaning the button click event creates a new window. 


Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp