Creating Tabbed Widget With Python-Tkinter

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:



    • master: parent window (root).
    • options: The options accepted by the Notebook() method are height, padding and width. Options are not used in this program.
  6. Creating the tabs
    tab1 = ttk.Frame(tabControl)
    tab2 = ttk.Frame(tabControl)

    Syntax:

    Frame(master=None, **options)

    Parameter:

    • master: tabControl is the parent widget for the tabs.
    • options: The options accepted by the Frame() method are class_, cursor, padding, relief, style, takefocus, height and width. Options are not used in this program.

    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:

    • child: tab1 and tab2 are the child widget of tabControl.
    • options: The options supported by add() method are sticky, state, padding, text, image, compound, underline.

    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:

    • expand: The expand option ensures equal distribution of space between widgets that have non zero expand value when the parent widget is expanded.
    • fill: The fill option ensures that the widget occupies the space allocated to it. Now fill=”both” specifies that the widget occupies the space along both X and Y axis, fill=”X” specifies that the widget occupies the space along X axis and fill=”Y” specifies that the widget occupies the space along Y axis.

    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:

    • master: The tabs act as parent for the Label widget.
    • options: The options supported by Label() method are anchor, bg, bitmap, bd, cursor, font, fg, height, width, image, justify, relief, padx, pady, textvariable, underline and wraplength.

    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:

    • options: The options supported by grid() method are column, columnspan, row, rowspan, padx, pady, ipadx, ipady and sticky.

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    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()  

    chevron_right

    
    

    Output
    python-tkinter

    python-tkinter




    My Personal Notes arrow_drop_up


    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


    Article Tags :

    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.