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_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
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.