A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/creating-tabbed-widget-with-python-tkinter/ below:

Creating Tabbed Widget With Python-Tkinter

Creating Tabbed Widget With Python-Tkinter

Last Updated : 12 Jul, 2025

Python offers a range of GUI frameworks that can be used to develop GUI based applications in Python. The most widely used Python interface is Tk interface or

tkinter

( as renamed in Python 3.x) . The Tkinter module offers a wide range of widgets that can be used to develop GUI applications much faster and easier compared to the other interfaces offered by Python. The tkinter.ttk module serves as an improvement to the already existing tk module. The Ttk module is equipped with 18 widgets, 12 of which existed in the Tkinter module such as Button, Checkbutton, PanedWindow, Radiobutton, Entry, Frame, Label, LabelFrame, Menubutton, Scale, Scrollbar, and Spinbox. The newly added widgets are Combobox, Notebook, Sizegrip, Progressbar, Separator, and Treeview. In this article, the Notebook widget of ttk module is put into use to create a tabbed widget. The ttk.Notebook widget manages a collection of windows and displays one at a time. Each child window is associated with a tab. The user can select one tab at a time to view the content of the window. Steps to create a tkinter tabbed widget :

  1. Import tkinter module
    import tkinter as tk
    from tkinter import *
    Note: Name of the module in Python 2.x is ‘Tkinter’ and in Python 3.x it is ‘tkinter’. Python 3.x is used here.
  2. Import tkinter ttk module which contains the Notebook widget
    from tkinter import ttk
  3. Create the parent window (root).
    root = tk.Tk()
    
    Syntax:
    Tk()
    Explanation: Used to create the parent window. Tk class is instantiated without any arguments (here).
  4. Add title to the parent window (root).
    root.title("Tab Widget")
    Syntax:
    title(name)
    Parameter: name: used to pass the desired name to the parent window (root).
  5. Creating Tab Control
    tabControl = ttk.Notebook(root)
    Syntax:
    Notebook(master=None, **options)
    Parameter:
  6. Creating the tabs
    tab1 = ttk.Frame(tabControl)
    tab2 = ttk.Frame(tabControl)
    Syntax:
    Frame(master=None, **options)
    Parameter: Explanation: The Frame widget acts like a container and is used to group other widgets together. Here the Frame widget is used to group the tab widgets together.
  7. Adding the tab
    tabControl.add(tab1, text='Tab 1')
    tabControl.add(tab2, text='Tab 2')
    Syntax:
    add(child, **options)
    Parameter: Explanation: The add() method is present in tk.ttk.Notebook class. It is used to add new tabs to the Notebook widget.
  8. Packing the tab control to make the tabs visible
    tabControl.pack(expand=1, fill="both")
    Syntax:
    pack(**options)
    Parameter: Explanation: The pack() method is used to organize widgets in blocks before placing them in the parent widget. This can be done using various options like fill, expand and side.
  9. Creating Label widget as a child of the parent window (root)
    ttk.Label(tab1, text="Welcome to GeeksForGeeks").grid(column=0, row=0, padx=30, pady=30) ttk.Label(tab2, text="Lets dive into the world of computers").grid(column=0, row=0, padx=30, pady=30)
    Syntax:
    Label(master, **options)
    Parameter: Explanation: The Label widget is used to display text or images on the screen.The text displayed on the screen can further be formatted using the various options available in the Label widget. Syntax:
    grid(**options)
    Parameter: Explanation: The grid() method is used to split the parent widget in rows and columns more specifically a 2D table. It can be used to specify the position of the widgets in the parent widget. Here grid() method specifies the position of the Label widget ie. basically the position of the text in the parent window.
  10. Run the application
    root.mainloop() 
    Syntax: mainloop() Explanation: The mainloop() acts like an infinite loop and is used to run an application.
  11. Complete program is as follows: Python3 1==
    import tkinter as tk                    
    from tkinter import ttk
    
    
    root = tk.Tk()
    root.title("Tab Widget")
    tabControl = ttk.Notebook(root)
    
    tab1 = ttk.Frame(tabControl)
    tab2 = ttk.Frame(tabControl)
    
    tabControl.add(tab1, text ='Tab 1')
    tabControl.add(tab2, text ='Tab 2')
    tabControl.pack(expand = 1, fill ="both")
    
    ttk.Label(tab1, 
              text ="Welcome to \
              GeeksForGeeks").grid(column = 0, 
                                   row = 0,
                                   padx = 30,
                                   pady = 30)  
    ttk.Label(tab2,
              text ="Lets dive into the\
              world of computers").grid(column = 0,
                                        row = 0, 
                                        padx = 30,
                                        pady = 30)
    
    root.mainloop()  
    
    Output

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4