Tkinter Notebook to create tabs and managing options to add image underline state with click events
We can add underline to the text string based on its index position (starting from 0).
my_tabs.add(tab1, text ='TabNo-0', underline=0) # adding tab
my_tabs.add(tab2, text ='TabNo-1', underline=1) # adding tab
my_tabs.add(tab3, text ='TabNo-2', underline=2) # adding tab
With this option underline we can add mnemonic activation. For this we have to enable enable_traversal()
my_tabs.enable_traversal()
We can use Alt and the underlined char to select the tab.
Full code 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,padding=10) # declaring
tab1 = ttk.Frame(my_tabs)
tab2 = ttk.Frame(my_tabs)
tab3 = ttk.Frame(my_tabs)
my_tabs.add(tab1, text ='TabNo-0', underline=0) # adding tab
my_tabs.add(tab2, text ='TabNo-1', underline=1) # adding tab
my_tabs.add(tab3, text ='TabNo-2', underline=2) # adding tab
my_tabs.pack(expand = 1, fill ="both")
font1=('time',22,'normal')
l1=tk.Label(tab1,text='I am tab-0',bg='yellow',font=font1)
l1.place(relx=0.4,rely=0.2) # using place
l2=tk.Label(tab2,text='I am tab-1',bg='yellow',font=font1)
l2.place(relx=0.4,rely=0.2) # using grid
l3=tk.Label(tab3,text='I am tab-2',bg='yellow',font=font1)
l3.place(relx=0.4,rely=0.2) # using place
my_tabs.enable_traversal()
my_w.mainloop() # Keep the window open
Updating underline option
my_dict=my_tabs.tab(tab3) # reading all option values as dictionary
print(my_dict['underline']) # index position of underlined char
# shift underline char by one
my_tabs.tab(tab3,underline=my_dict['underline']+1)
We can connect this to one click event of a button, on Click the underline char will move to next position.
Full code 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,padding=10) # declaring
tab1 = ttk.Frame(my_tabs)
tab2 = ttk.Frame(my_tabs)
tab3 = ttk.Frame(my_tabs)
my_tabs.add(tab1, text ='TabNo-0',underline=0) # adding tab
my_tabs.add(tab2, text ='TabNo-1',underline=1) # adding tab
my_tabs.add(tab3, text ='TabNo-2',underline=2) # adding tab
my_tabs.pack(expand = 1, fill ="both")
font1=('time',22,'normal')
l1=tk.Label(tab1,text='I am tab-0',bg='yellow',font=font1)
l1.place(relx=0.4,rely=0.2) # using place
l2=tk.Label(tab2,text='I am tab-1',bg='yellow',font=font1)
l2.place(relx=0.4,rely=0.2) # using grid
l3=tk.Label(tab3,text='I am tab-2',bg='yellow',font=font1)
l3.place(relx=0.4,rely=0.2) # using place
def my_move(): # Click event of the button
my_dict=my_tabs.tab(tab3) # all option values as dictionary
print(my_dict['underline']) # index position of underlined char
# shift underline char by one
my_tabs.tab(tab3,underline=my_dict['underline']+1) my_tabs.enable_traversal()
b1=tk.Button(my_w,text='Move underline',
command=lambda:my_move())
b1.pack(side=LEFT)
my_w.mainloop() # Keep the window open
By enabling traversal we can use following controls to navigate between tabs
Cltrl + Tab : Select the following tab of the currently selected one Shift + Ctrl + Tab : Select the preceding tab Alt + K : here K the underlined char used for the tab.
« Notebook