Managing multiple windows in tkinter

Posted by


In this tutorial, we will learn how to work with multiple windows in tkinter, a popular GUI toolkit for Python. By creating multiple windows, we can build more complex and interactive user interfaces for our applications.

To get started, make sure you have Python installed on your computer. You can check if you have Python installed by opening a terminal or command prompt and typing python --version. If you don’t have Python installed, you can download it from the official website (https://www.python.org/).

Once you have Python installed, you can install tkinter by running the following command in your terminal or command prompt:

pip install tk

Now that we have tkinter installed, we can start creating multiple windows by following the steps below:

Step 1: Import the tkinter module
First, we need to import the tkinter module at the beginning of our script. This will give us access to all the functions and classes we need to create our windows.

import tkinter as tk

Step 2: Create the main window
Next, we will create the main window of our application using the Tk class from tkinter. This window will serve as the parent window for our other windows.

root = tk.Tk()
root.title("Main Window")

Step 3: Create a button to open a new window
Now, we will add a button to the main window that, when clicked, will open a new window. We can achieve this by creating a function that creates a new window and binding it to the button.

def open_window():
    new_window = tk.Toplevel(root)
    new_window.title("New Window")

button = tk.Button(root, text="Open New Window", command=open_window)
button.pack()

Step 4: Add widgets to the new window
In the open_window function, we can add widgets to the new window to make it more interactive. For example, we can add a label and a button to the new window.

def open_window():
    new_window = tk.Toplevel(root)
    new_window.title("New Window")

    label = tk.Label(new_window, text="Hello, this is a new window!")
    label.pack()

    close_button = tk.Button(new_window, text="Close", command=new_window.destroy)
    close_button.pack()

Step 5: Run the application
Finally, we need to start the tkinter event loop to display the main window and allow user interaction.

root.mainloop()

That’s it! You have now successfully created a tkinter application with multiple windows. You can further customize your windows by adding more widgets, such as entry fields, radio buttons, check boxes, and more. Experiment with different layouts and styles to create a user-friendly interface for your application.

I hope you found this tutorial helpful in understanding how to work with multiple windows in tkinter. If you have any questions or need further assistance, feel free to leave a comment below. Happy coding!

0 0 votes
Article Rating
11 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@despiertateymotivate
1 month ago

Thanks for this video…Still this went extremely advanced for me when you created the class…I hope you keep posting content teaching from the ground basic concepts to people like me, people that at their 30s just bumped into programming and are loving it…thanks!

@Sayied-s7d
1 month ago

0:30 what if it was on android
how this would be shown

@عليعلاوي-ت6ف2ي
1 month ago

كيف نعمل نافذة منبثقة من نافذة اساسية مثلا انا قمت بفتح ملف TXT وكتبت بملف وقمت بضغط على مربع خروج علامة اكس ويظهر نافذة صغيرة يقول هلتريد حفظ تغيرات او الغاء

@ddady15
1 month ago

The best tutorial i've watched. It helped me understand a lot of things. Thanks Atlas. Can you please make a one for Python OO?

@debnathmriganka2010
1 month ago

good tutorial

@sonu-jangir
1 month ago

Thank you so much sir for this helpful video.❤🎉

@ahmedy1598
1 month ago

Hi developers I need the Childfrom Frame with in window that execute multiple From windows appears

C# I used the code Childfrom that appear the panel

So Make vedio tkinter with Childfrom

@parajf
1 month ago

Why not create one jupyter book for different action and call them in master file in form of menu

@amanchaudhary8502
1 month ago

I need help!
My python program uses "import tkinter as tk" but doesn't use "from tkinter import ttk" and "from tkinter import messagebox"

@Knight_Sky35
1 month ago

Why this channel is so underrated. This channel helps me in my Capstone

@odams
1 month ago

Nice!