In this Python TKinter article i want to show you How to Create TabWidgets in Python TKinter, so first of all let’s talk about TabWidgets.
What is TKinter?
Tkinter is the standard GUI (Graphical User Interface) toolkit for Python. It is included with most Python installations and provides a set of modules and classes for creating desktop GUI applications.
What are TabWidgets in TKinter?
In Tkinter, Notebook widgets are also known as TabWidgets, it provides a tabbed container for organizing and displaying multiple frames or widgets inside a single window. They allows users to switch between different pages or “tabs” to view different sets of content.
This is the complete code for Creating TabWidgets In Python TKinter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import tkinter as tk from tkinter import ttk # Define App class, inheriting from tk.Tk class App(tk.Tk): def __init__(self): # Call constructor of parent class super(App, self).__init__() # Set title of the application window self.title("Codeloop.org - Tkinter TabWidgets") # Set window icon self.iconphoto(True, tk.PhotoImage(file='codeloop.png')) # Create Notebook widget (TabWidget) to hold multiple tabs tabControl = ttk.Notebook(self) # Create first tab (Tab 1) as a Frame tab1 = ttk.Frame(tabControl) # Add first tab to the Notebook with the specified text tabControl.add(tab1, text="Tab 1") # Create second tab (Tab 2) as a Frame tab2 = ttk.Frame(tabControl) # Add second tab to the Notebook with the specified text tabControl.add(tab2, text="Tab 2") # Pack Notebook widget into the main application window tabControl.pack(expand=1, fill="both") # Create an instance of the App class app = App() # Start the Tkinter event loop app.mainloop() |
Explanation of code:
- Import TKinter Statements: Import tkinter module as tk and import ttk module from tkinter. ttk provides access to themed Tk widgets.
- TKinter App Class Definition: Define a class at name of App that inherits from tk.Tk. This class is our main application window.
- TKinter Constructor Method (__init__): Define __init__ method of the App class, it serves as the constructor. This method is called when an instance of the App class is created.
- TKinter Super Method: Call the constructor of the parent class (tk.Tk) using super() to initialize the application window.
- TKinter Window Title: Set the title of the application window using title method.
- TKinter Window Icon: Set the window icon using iconphoto method. The file parameter specifies the path to the image file for the icon.
- TKinter Notebook (TabWidget): Create a ttk.Notebook widget named tabControl to hold multiple tabs.
- TKinter Tab Creation: Create two tabs (tab1 and tab2) as ttk.Frame widgets and add them to the tabControl Notebook using add method. also text parameter specifies the text displayed on each tab.
- TKinter Packing the Notebook: Pack the tabControl Notebook widget into the main application window using the pack method. expand parameter set to 1, and it allows the Notebook to expand to fill any available space, and fill set to “both” allows the Notebook to fill both the horizontal and vertical directions.
- Create TKinter App Instance: Create an instance of the App class named app.
- TKinter Main Event Loop: Start the Tkinter event loop using the mainloop method to keep the application running and responsive to user interactions.
Run the code and this will be the result
Subscribe and Get Free Video Courses & Articles in your Email