Python tkinter Notebook ( tabs )

Youtube Live session on Tkinter

ttk Notebook
We can add Tabs to our Tkitner GUI by using Notebook which is part of ttk module. ( What is ttk ? )

Tkinter Notebook to create tabs and managing options to add image underline state with click events

Code for the above window is here.
import tkinter  as tk 
from tkinter import *
from tkinter import ttk
my_w = tk.Tk()

my_tabs = ttk.Notebook(my_w) # declaring 

tab1 = ttk.Frame(my_tabs)
tab2 = ttk.Frame(my_tabs)

my_tabs.add(tab1, text ='Tab-0') # adding tab
my_tabs.add(tab2, text ='Tab-1') # adding tab 
my_tabs.pack(expand = 1, fill ="both")

l1=tk.Label(tab1,text='I am tab-0',bg='yellow',width=10),rely=0.2) # using place
b1=tk.Button(tab1,text='I am tab 0'),rely=0.4) 

l2=tk.Label(tab2,text='I am tab-1',bg='yellow',width=10)
l2.grid(row=1,column=1) # using grid 
b2=tk.Button(tab2,text='I am tab-1')

my_w.mainloop()  # Keep the window open


Notebook Options


Desired height of the pane area.


Desired width of the pane area.


Notebook padding option
my_tabs = ttk.Notebook(my_w,padding=20)
Amount of extra space around the Notebook.

Tab Options


Value can be normal , disabled or hidden.
Tab with different state Option values


Notebook sticky and underline options
Positioning of child window within the pane area, values can be one of combination fo n,s,e,w. We can use sticky='ne' to position at top right corner.
my_tabs.add(tab2, text ='Tab-1',sticky='se',underline=2)
Tab with different sticky Option values


Text appearing at the top for navigation on the tab.


Specifies an image to display in the tab.


How image to display relative to text. Values can be 'text','image','top','bottom', 'left','right','none'
Tab with image & compound options


Specifies the index (0-based) of a character to underline in the text string. Check the image and code above.
Tab with underline option for mnemonic activation

All options with values

We can collect all options ( as keys ) and values as a dictionary. # all option values as dictionary
print(my_dict['underline']) # index position of underlined char


Tkinter Notebook methods to select hide forget tabs and using notebook tab change event


We already used this in above code to add one new tab. While adding the options like state, sticky, padding, text, image, compound, underline can be added ( check the details above )


Removes the tab. We can use click event of a button to remove the tab. Check the example below.


Hides the tabe specified by tab_id, can be restored by using add().
hide() forget() and select() Methods


Returns the name of the tab element at x,y position. Empty string is return if none.


Returns the index ( numeric ) of the specified tab_id. If 'end' is provided then total number of tabs are returned.

insert(pos, child, **kw)

Add a tab or Pane at a specified position. We can use 'end' as position to place the tab at the end. ( we must create tab3 before assigning )


Select the specified tab_id, if tab_id is not given then returns the currently selected pane.
We can use three buttons and on Click of the button respective tab will open. Here is the code.

tab(tab_id,option=None, **kw)

Read or modify the options of the tab. We get the dictionary of all option values if the option is not given. # all option values as dictionary
print(my_dict['underline']) # index position of underlined char
Find out the underline option of one tab and then change the value to one previous char by using the underline option.
Tab with underline option for mnemonic activation


Returns the list of tabs


Enable keyboard traversal for a toplevel window containing this notebook. This is known as mnemonic activation.
Tab with underline option for mnemonic activation

Virtual Events

Once a new tab is selected, one event <<NotebokTabChanged>> is generated.
On Tab change event to display Tab Number

Example 1

Disable, enable or hide any tab by click of a button. The buttons should be outside the pane.
Managing State option of a tab on Button click
Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net

    Post your comments , suggestion , error , requirements etc here

    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 All rights reserved worldwide Privacy Policy Disclaimer