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!
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()
❤
thanks a lot
Nice sir❤
Thanks Bro
can I add to each tab a script like for bots and add a play or pause button?
Good video
i like this this is very intersting]
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
Great,,
You are legend bro
now I can make a unliimited tabs
Good tutorial🙂
Hello, its telling me to define frame
Hi Bro, can you teach us how to add back button from Tkhinter please🥺
Thank you
Cool. Directly to the point.
Beefy
What about commands in the widget
Hi Bro, you really help me!! You talk clear and straight to the point, I like it brother!!