How to Create Tabbed Interfaces in Python with Tkinter Notebook Widget?

As a Python developer working on various projects for my clients, I have faced the challenge of organizing complex user interfaces into easy-to-navigate tabs. Then I explored more about this topic. In this tutorial, I will explain how to create tabbed interfaces in Python with Tkinter Notebook widget with suitable examples and screenshots that will help you understand.

Python Tkinter Notebook Widget

The Notebook widget in Tkinter is a container that allows you to create tabbed interfaces, where each tab can hold different content or widgets. It provides a way to organize and switch between multiple pages or sections of your application, making it more user-friendly and efficient.

The Notebook widget is part of the ttk module, which stands for “themed Tk”. It offers a more modern and stylish appearance compared to the classic Tkinter widgets.

Read Python Tkinter Events

Create Tabbed Interfaces in Python with Tkinter Notebook Widget

To create a Notebook widget in your Python Tkinter application, you need to follow these steps:

  1. Import the required libraries:
import tkinter as tk
from tkinter import ttk
  1. Create an instance of the Notebook widget:
root = tk.Tk()
notebook = ttk.Notebook(root)

Here, root is the parent window or frame where you want to place the Notebook widget.

Check out Python Tkinter Animation

  1. Create tabs (frames) and add them to the Notebook:
tab1 = ttk.Frame(notebook)
notebook.add(tab1, text="Tab 1")

tab2 = ttk.Frame(notebook)
notebook.add(tab2, text="Tab 2")

Each tab is created as a ttk.Frame widget and added to the Notebook using the add() method. The text parameter specifies the label for each tab.

  1. Pack the Notebook widget:
notebook.pack(expand=True, fill="both")
root.mainloop()

This ensures that the Notebook widget expands to fill the available space in the parent window or frame.

You can see the output in the screenshot below.

Create Tabbed Interfaces in Python with Tkinter Notebook Widget

Read Python Tkinter Multiple Windows

Example: Customer Management System

Let’s consider a real-world scenario where we need to create a customer management system for a business in the USA. We’ll use the Tkinter Notebook widget to organize the different sections of the application into tabs.

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
root.title("Customer Management System")

# Create a Notebook widget
notebook = ttk.Notebook(root)

# Create tabs
tab1 = ttk.Frame(notebook)
notebook.add(tab1, text="Customer Details")

tab2 = ttk.Frame(notebook)
notebook.add(tab2, text="Orders")

tab3 = ttk.Frame(notebook)
notebook.add(tab3, text="Reports")

# Add widgets to the tabs
# Tab 1: Customer Details
name_label = ttk.Label(tab1, text="Name:")
name_label.grid(row=0, column=0, padx=5, pady=5)
name_entry = ttk.Entry(tab1)
name_entry.grid(row=0, column=1, padx=5, pady=5)

email_label = ttk.Label(tab1, text="Email:")
email_label.grid(row=1, column=0, padx=5, pady=5)
email_entry = ttk.Entry(tab1)
email_entry.grid(row=1, column=1, padx=5, pady=5)

# Tab 2: Orders
order_table = ttk.Treeview(tab2)
order_table["columns"] = ("Order ID", "Product", "Quantity", "Price")
order_table.heading("Order ID", text="Order ID")
order_table.heading("Product", text="Product")
order_table.heading("Quantity", text="Quantity")
order_table.heading("Price", text="Price")
order_table.pack(fill="both", expand=True)

# Tab 3: Reports
report_text = tk.Text(tab3)
report_text.pack(fill="both", expand=True)

# Pack the Notebook widget
notebook.pack(expand=True, fill="both")

root.mainloop()

In this example, we create a customer management system with three tabs: Customer Details, Orders, and Reports.

  • The Customer Details tab contains input fields for customer name and email.
  • The Orders tab displays a table of order information using the ttk.Treeview widget.
  • The Reports tab provides a text area for generating and displaying reports.

By organizing the different sections into tabs, the application becomes more structured and easier to navigate for the user.

You can see the output in the screenshot below.

How to Create Tabbed Interfaces in Python with Tkinter Notebook Widget

Read Python Tkinter Editor

Access Tab Widgets

Sometimes, you may need to access the actual tab widget of a Notebook to perform certain operations or modifications. There are two approaches to accessing the tab widgets:

  1. Using the tabs() method:
tab_widget = notebook.tabs()[tab_index]

The tabs() method returns a list of all the tab widgets in the Notebook. You can access a specific tab widget by providing its index.

  1. Using the select() method:
selected_tab = notebook.select()
tab_widget = notebook.nametowidget(selected_tab)

The select() method returns the name of the currently selected tab. You can then use the nametowidget() method to get the corresponding tab widget.

Create Tabbed Interfaces in Python with Tkinter Notebook Widget access tab widgets

Check out Python Tkinter Table Tutorial

Customize the Appearance

The Tkinter Notebook widget allows you to customize its appearance to match your application’s style. You can modify the tab background color, foreground color, font, and other visual properties using the ttk.Style class.

from tkinter import *
from tkinter.ttk import Notebook, Style
ws = Tk()
Mysky = "#DCF0F2"
Myyellow = "#F2C84B"

style = Style()
style.theme_create( "dummy", parent="alt", settings={
        "TNotebook": {"configure": {"tabmargins": [2, 5, 2, 0] } },
        "TNotebook.Tab": {
            "configure": {"padding": [5, 1], "background": Mysky },
            "map":       {"background": [("selected", Myyellow)],
                          "expand": [("selected", [1, 1, 1, 0])] } } } )

style.theme_use("dummy")

notebook = Notebook(ws)
frame1 = Frame(notebook, width=300, height=200)
notebook.add(frame1, text = 'First')
frame2 = Frame(notebook, width=300, height=200)
notebook.add(frame2, text = 'Second')
notebook.pack(expand=1, fill='both', padx=7, pady=7)

Button(ws, text='dummy!').pack(fill='x')

ws.mainloop()

You can see the output in the screenshot below.

Create Tabbed Interfaces in Python with Tkinter Notebook Widget style

Read Python Tkinter Quiz

Disable Tab

Python Tkinter Notebook disables tab we create a notebook inside the notebook we add two tabs. If the first tab is disabled they move to the next tab.

from tkinter import *
from tkinter.ttk import Notebook, Style
ws = Tk()

style = Style()
style.layout('TNotebook.Tab', []) # turn off tabs

note = Notebook(ws)

frame1 = Frame(note)
text = Text(frame1, width=40, height=10)
text.insert('end', 'page0 : a text widget')
text.pack(expand=1, fill='both')
note.add(frame1)

frame2 = Frame(note)
lablel = Label(frame2, text='page1 : a label')
lablel.pack(expand=1, fill='both')
note.add(frame2)
note.pack(expand=1, fill='both', padx=5, pady=5)

def Do_Something():
    note.select(1)

ws.after(3000, Do_Something)
ws.mainloop()

You can see the output in the screenshot below.

Create Tabbed Interfaces in Python with Tkinter Notebook Widget disable

Check out Python Tkinter Mainloop with Examples

Conclusion

In this tutorial, I explained how to create tabbed interfaces in Python with Tkinter Notebook widget. I discussed what is notebook widget and how to create a tabbed interface with a notebook widget. I also covered an example of creating a customer management system , how to access tab widgets customizing the appearance.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.