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 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.
- Import tkinter ttk module which contains the Notebook widget
from tkinter import ttk
- Create the parent window (root).
root = tk.Tk()
Used to create the parent window. Tk class is instantiated without any arguments (here).
- Add title to the parent window (root).
name: used to pass the desired name to the parent window (root).
- Creating Tab Control
tabControl = ttk.Notebook(root)
- 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 tabs
tab1 = ttk.Frame(tabControl) tab2 = ttk.Frame(tabControl)
- 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.
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 tab
tabControl.add(tab1, text='Tab 1') tabControl.add(tab2, text='Tab 2')
- 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.
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 visible
- 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.
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)
- 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.
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.
- options: The options supported by grid() method are column, columnspan, row, rowspan, padx, pady, ipadx, ipady and sticky.
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 application
The mainloop() acts like an infinite loop and is used to run an application.
Complete program is as follows:
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.