Adding Window Tabs in Python Tkinter 📑

Posted by


In Python tkinter, adding window tabs can be done using the notebook widget. The notebook widget allows you to create multiple tabs within a single window, making it a great tool for organizing and displaying different pieces of information or functionalities in your GUI application. In this tutorial, I will guide you through the process of adding window tabs in Python tkinter using the notebook widget.

Step 1: Import the necessary modules
First, you need to import the tkinter module and the ttk class from the tkinter module. The ttk class provides access to themed widgets in tkinter, including the notebook widget.

import tkinter as tk
from tkinter import ttk

Step 2: Create the main window
Next, you need to create the main window for your GUI application using the Tk() class.

root = tk.Tk()
root.title("Window Tabs Tutorial")

Step 3: Create the notebook widget
Now, you can create the notebook widget using the ttk.Notebook() class. The ttk.Notebook() class is used to create a notebook widget that can contain multiple tabs.

notebook = ttk.Notebook(root)
notebook.pack(fill='both', expand=True)

Step 4: Add tabs to the notebook widget
To add tabs to the notebook widget, you need to create frames for each tab and then add those frames to the notebook widget.

tab1 = ttk.Frame(notebook)
tab2 = ttk.Frame(notebook)

notebook.add(tab1, text='Tab 1')
notebook.add(tab2, text='Tab 2')

Step 5: Add content to the tabs
Now, you can add content to each tab by adding widgets to the frames created for each tab.

label_tab1 = tk.Label(tab1, text='This is Tab 1')
label_tab1.pack(padx=10, pady=10)

label_tab2 = tk.Label(tab2, text='This is Tab 2')
label_tab2.pack(padx=10, pady=10)

Step 6: Run the GUI application
Finally, run the GUI application by calling the mainloop() method on the root window.

root.mainloop()

When you run the code, you should see a new window with two tabs labeled ‘Tab 1’ and ‘Tab 2’. Each tab will contain the content that you added to the respective frames. You can add as many tabs as you need by creating additional frames and adding them to the notebook widget.

Adding window tabs in Python tkinter using the notebook widget is a simple and effective way to organize and display different parts of your GUI application. I hope this tutorial was helpful in guiding you through the process of adding window tabs in tkinter. Happy coding!

0 0 votes
Article Rating

Leave a Reply

34 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@BroCodez
4 hours ago

from tkinter import *

from tkinter import ttk

window = Tk()

notebook = ttk.Notebook(window) #widget that manages a collection of windows/displays

tab1 = Frame(notebook) #new frame for tab 1

tab2 = Frame(notebook) #new frame for tab 2

notebook.add(tab1,text="Tab 1")

notebook.add(tab2,text="Tab 2")

notebook.pack(expand=True,fill="both") #expand = expand to fill any space not otherwise used

#fill = fill space on x and y axis

Label(tab1,text="Hello, this is tab#1",width=50,height=25).pack()

Label(tab2,text="Goodbye, this is tab#2",width=50,height=25).pack()

window.mainloop()

@hydarhydar2338
4 hours ago

@raffaelschellenberg4724
4 hours ago

thanks a lot

@percussion1999
4 hours ago

Nice sir❤

@alexoriginals
4 hours ago

Thanks Bro

@cahyakomara1468
4 hours ago

can I add to each tab a script like for bots and add a play or pause button?

@SanjayRaut-l4v
4 hours ago

Good video

@RARYAMEHD
4 hours ago

i like this this is very intersting]

@OrhanAhmedov-h7z
4 hours ago

Thank you a lot I understand you. Because you are not like other tutors, Just do this then this cant express or explain, You are basically just pulling the routine out and why and what is used for how and why, Basically you explaining by giving optional to others. Like # Widget manages collection of windows/displays #TheBest #Amazing

@truquichan
4 hours ago

Great,,

@Complex2.0GameR
4 hours ago

You are legend bro

@ruchigoel3242
4 hours ago

now I can make a unliimited tabs

@bartekgrechuta7691
4 hours ago

Good tutorial🙂

@hawraaalmayahi6137
4 hours ago

Hello, its telling me to define frame

@lichtblade1375
4 hours ago

Hi Bro, can you teach us how to add back button from Tkhinter please🥺

@fajrulfalah93
4 hours ago

Thank you

@elevendarter112
4 hours ago

Cool. Directly to the point.

@jhassee
4 hours ago

Beefy

@Giodidthat
4 hours ago

What about commands in the widget

@joeri29
4 hours ago

Hi Bro, you really help me!! You talk clear and straight to the point, I like it brother!!

34
0
Would love your thoughts, please comment.x
()
x