Python tkinter Notebook ( tabs )

Basics of Python Tkinter

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

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_w.geometry("400x200")  

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)
l1.place(relx=0.4,rely=0.2) # using place
b1=tk.Button(tab1,text='I am tab 0')
b1.place(relx=0.4,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')
b2.grid(row=2,column=2)

my_w.mainloop()  # Keep the window open

Options

Notebook Options

height

Desired height of the pane area.

width

Desired width of the pane area.

padding

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

Tab Options

state

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

sticky

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

Text appearing at the top for navigation on the tab.

image

Specifies an image to display in the tab.

compound

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

underline

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.
my_dict=my_tabs.tab(tab3) # all option values as dictionary
print(my_dict['underline']) # index position of underlined char

Methods

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

add

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 )

forget(tab_id)

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

hide(tab_id)

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

identify(x,y)

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

index(tab_id)

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 )
my_tabs.insert('end',tab3,text='tab-3')

select(tab_id)

Select the specified tab_id, if tab_id is not given then returns the currently selected pane.
my_tabs.select(tab3)
We can use three buttons and on Click of the button respective tab will open. Here is the code.
b1=tk.Button(my_w,text='tab-0',command=lambda:my_tabs.select(tab1))
b1.pack(side=LEFT)
b2=tk.Button(my_w,text='tab-1',command=lambda:my_tabs.select(tab2))
b2.pack(side=LEFT)
b3=tk.Button(my_w,text='tab-3',command=lambda:my_tabs.select(tab3))
b3.pack(side=LEFT)

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.
my_dict=my_tabs.tab(tab3) # 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

tabs()

Returns the list of tabs

enable_traversal()

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.
my_tabs.bind('<<NotebookTabChanged>>',my_msg)
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

* indicates required
Subscribe to plus2net

    plus2net.com



    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-2021 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer