Creating Tabbed Widget With Python-Tkinter

26 Mar, 2020
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 :
Import tkinter moduleimport 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.Import tkinter ttk module which contains the Notebook widgetfrom tkinter import ttkCreate the parent window (root).root = tk.Tk()
Syntax:Tk()Explanation:Used to create the parent window. Tk class is instantiated without any arguments (here).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).Creating Tab ControltabControl = 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.Creating the tabstab1 = 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.Adding the tabtabControl.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.Packing the tab control to make the tabs visibletabControl.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.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.Run the applicationroot.mainloop() Syntax: mainloop()Explanation:The mainloop() acts like an infinite loop and is used to run an application.Complete program is as follows: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()  OutputMy Personal Notes
arrow_drop_upSave
Import tkinter moduleimport 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.
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.
Import tkinter ttk module which contains the Notebook widgetfrom tkinter import ttk
from tkinter import ttk
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).
root = tk.Tk()

Syntax:
Tk()
Explanation:Used to create the parent window. Tk class is instantiated without any arguments (here).
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).
root.title("Tab Widget")
Syntax:
title(name)
Parameter:name: used to pass the desired name to the parent window (root).
Creating Tab ControltabControl = 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.
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.
Creating the tabstab1 = 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.
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.
Adding the tabtabControl.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.
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.
Packing the tab control to make the tabs visibletabControl.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.
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.
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.
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.
Run the applicationroot.mainloop() Syntax: mainloop()Explanation:The mainloop() acts like an infinite loop and is used to run an application.
root.mainloop() 
Syntax: mainloop()Explanation:The mainloop() acts like an infinite loop and is used to run an application.
Complete program is as follows:
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
Python-tkinter
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
How to drop one or multiple columns in Pandas Dataframe
Python | os.path.join() method
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | datetime.timedelta() function
Python | Get unique values from a list