Introduction to Tkinter Notebook
In Python Tkinter notebook is defined as tabs for controlling tabs that are provided by ttk library and such a tabbed widget is used for allowing to navigate from one page to another which can be done using the index tab. In general, this notebook widget is defined as an area or tab which allows users to select pages of content that are required by the user, and this is done by pressing or clicking on tabs that are created at the top area of the parent notebook and after setting up such parent notebook we can easily add the tabs and each click on these tabs the notebook widget will display the associated child pane.
Working of Tkinter Notebook with Example
In this article, we are discussing the notebook widget of Tkinter in Python. This widget acts as tabs or we can say tabbed notebook. This widget is obtained from the ttk library which is useful in managing a set of windows and it will start displaying one window at each time. Therefore we can say that users can select the slave window related to the tab to change the present displaying window. This notebook widget uses few methods to add and remove tabs such as add() for adding tabs and some other methods for temporarily hide the tabs or delete the tabs. This widget also provides a collection of options for each tab which are generally used for controlling the appearances and the behavior of the tabs within the widgets, where these options are classified as standard options, widget specific options, and tab options and the notebook widget uses tab id to refer to another tab within the widgets to switch from one page to another.
In the below section let us see the syntax of the Tkinter notebook which can be declared as the command form as wells as method form.
Syntax:
ttk.notebook(root, options)
In the above syntax, we can see that the notebook widget is obtained from ttk module and it has 2 parameters the root parameter which specifies the parent or master window, and the second parameter options are classified into as standard options, tab options, widget options such as class, cursor, weight, padding, state, text, image, etc.
There are a few methods provided by the notebook widget such as add() for adding tabs, cget() can obtain the current value of the configuration option, hide() to hide the tabs, index() to obtain the index in the numeric value of the specified tab, etc.
In Python, Tkinter is a module for developing GUI-based apps, whereas the notebook widget is obtained from the ttk module which is considered as an extension of the Tkinter module or we can say the improvement of this Tkinter module. So when we are using this notebook widget we have to import ttk module which is used for creating tabs in the windows and can display one window at a time and the child window is associated with each of the tabs created and these tabs are usually used whenever the user wants to display some message of the widow.
Let us see a simple example of creating tabs in the window using a notebook widget of ttk module.
Example
Code:
from Tkinter import *
import Tkinter as tk
import ttk
import tkMessageBox
master = tk.Tk()
master.geometry('500x200')
def func():
tkMessageBox.showinfo( "Hello Educba", "Click on the tabs to view the content")
b1 = Button( master, text='Click me for next step', background = 'Red', fg = '#000000', command = func)
b1.pack()
tc = ttk.Notebook(master)
t1 = ttk.Frame(tc)
t2 = ttk.Frame(tc)
tc.add(t1, text ='Notebook tab1')
tc.add(t2, text ='Notebook tab2')
tc.pack(expand = 1, fill ="both")
ttk.Label(t1,
text ="Hello Educba Technology Institute").grid(column = 3,
row = 3)
ttk.Label(t2,
text ="Notebook widget demonstration").grid(column = 3,
row = 3)
master.mainloop()
Output:
In the above article, we can see we are first importing the modules Tkinter for developing GUI, ttk module for using notebook widget for creation of tabs within the parent window, tkmessagebox module for displaying any message on the pop-up dialogue box, and in the above example it will pop up the message when we click the button widget on the window it will display as “click on the tabs t view the content” so that the user can go for switching between windows one at a time using these created tabs using notebook widget. In this program then we have passed the parent window to the notebook() so that we can have control over the tabs to switch and to display the content as the tabs are created we need separate frames to do so. Therefore for each tab, we have used the frame method and have passed the tab names to this frame() method. Then by using the add() function we are adding the name to each tab by using text argument and we have named the tabs as t1 as “Notebook Tab1”, t2 as “Notebook tab2”. Then to display the content by clicking each tab we have for tab “Notebook tab1” we are displaying the content as “Hello Educba Technology Institute” and for the tab “Notebook tab2” we are displaying “Notebook widget demonstration” and this content is displayed using label() function where it also takes text argument for the content and where the content should be displayed can be used by grid() function and here we have to display the message on column 3 and row 3 for both tabs. And the output of the entire program can be seen in the above screenshots.
Conclusion
In this article, we conclude in the Python ttk module is used as an extension or improvement of the Tkinter module. In ttk module, it provides some new widgets and in this article, we have discussed one such widget called notebook widget which is used for the creation of tabs so that using notebook widget we can toggle between the windows but one at a time and also display the content of the window. In this article, we have seen the syntax of the notebook widget and the options such as a cursor, weight, height, etc it uses and few methods like add(), Label, etc. In this article, we also saw one simple example which has two tabs in the same parent window but when tabs are clicked we are switching from one window to another one at a time.
Recommended Articles
This is a guide to Tkinter Notebook. Here we discuss the Introduction and working of Tkinter Notebook along with an example and code implementation. You may also have a look at the following articles to learn more –