Introduction
Basic Widgets
Toplevel Widgets
Geometry Management
Binding Functions
Working with Images in Tkinter
Tkinter Advance
Applications and Projects
Creating a tabbed interface in a Tkinter application can help organize content efficiently. The ttk
module in tkinter
provides a Notebook
widget which can be used for this purpose.
Here's a step-by-step tutorial to create a tabbed widget using Tkinter:
ttk.Notebook
in TkinterSetting up the basic GUI with Tkinter:
Initialize a basic window.
import tkinter as tk from tkinter import ttk root = tk.Tk() root.title("Tabbed Widget in Tkinter") root.geometry("400x300")
Creating the Notebook (Tabbed) Widget:
# Initialize the notebook notebook = ttk.Notebook(root) notebook.pack(pady=10, expand=True, fill=tk.BOTH)
Creating Frames for Tabs:
Each tab in the notebook will have its own frame, where you can place other widgets.
frame1 = ttk.Frame(notebook, width=400, height=280) frame2 = ttk.Frame(notebook, width=400, height=280) frame1.pack(fill=tk.BOTH, expand=True) frame2.pack(fill=tk.BOTH, expand=True)
Adding Frames to the Notebook as Tabs:
notebook.add(frame1, text="Tab 1") notebook.add(frame2, text="Tab 2")
Populating the Tabs with Content:
For demonstration purposes, let's add a label to each tab.
label1 = ttk.Label(frame1, text="This is content in Tab 1") label1.pack(pady=50, padx=50) label2 = ttk.Label(frame2, text="This is content in Tab 2") label2.pack(pady=50, padx=50)
Run the main loop:
root.mainloop()
Now, combine all the code snippets above and run the script. You'll be presented with a Tkinter window containing two tabs, each labeled with its respective text and containing the content we provided.
This provides the foundational structure for creating tabbed interfaces. Depending on your application needs, you can add more tabs, fill them with different widgets, or even nest another tabbed widget within an existing tab for multi-level tabbed interfaces.
Python Tkinter notebook widget example:
Notebook
widget in Tkinter to create a tabbed interface with multiple tabs.import tkinter as tk from tkinter import ttk root = tk.Tk() root.title("Tkinter Notebook Widget Example") # Create a Notebook widget notebook = ttk.Notebook(root) notebook.pack(pady=10, padx=10) # Create and add tabs to the Notebook tab1 = tk.Frame(notebook) tab2 = tk.Frame(notebook) notebook.add(tab1, text="Tab 1") notebook.add(tab2, text="Tab 2") label_tab1 = tk.Label(tab1, text="Content of Tab 1") label_tab1.pack(pady=10) label_tab2 = tk.Label(tab2, text="Content of Tab 2") label_tab2.pack(pady=10) root.mainloop()
How to create tabs in Tkinter:
Notebook
widget for a tabbed interface.import tkinter as tk from tkinter import ttk root = tk.Tk() root.title("Creating Tabs in Tkinter") # Create a Notebook widget notebook = ttk.Notebook(root) notebook.pack(pady=10, padx=10) # Create and add tabs to the Notebook tab1 = tk.Frame(notebook) tab2 = tk.Frame(notebook) notebook.add(tab1, text="Tab 1") notebook.add(tab2, text="Tab 2") label_tab1 = tk.Label(tab1, text="Content of Tab 1") label_tab1.pack(pady=10) label_tab2 = tk.Label(tab2, text="Content of Tab 2") label_tab2.pack(pady=10) root.mainloop()
Tkinter notebook widget with tabs:
Notebook
widget in Tkinter to create a tabbed interface with tabs.import tkinter as tk from tkinter import ttk root = tk.Tk() root.title("Tkinter Notebook Widget with Tabs") # Create a Notebook widget notebook = ttk.Notebook(root) notebook.pack(pady=10, padx=10) # Create and add tabs to the Notebook tab1 = tk.Frame(notebook) tab2 = tk.Frame(notebook) notebook.add(tab1, text="Tab 1") notebook.add(tab2, text="Tab 2") label_tab1 = tk.Label(tab1, text="Content of Tab 1") label_tab1.pack(pady=10) label_tab2 = tk.Label(tab2, text="Content of Tab 2") label_tab2.pack(pady=10) root.mainloop()
Creating tabbed interface with Tkinter:
Notebook
widget for organizing content.import tkinter as tk from tkinter import ttk root = tk.Tk() root.title("Creating Tabbed Interface with Tkinter") # Create a Notebook widget notebook = ttk.Notebook(root) notebook.pack(pady=10, padx=10) # Create and add tabs to the Notebook tab1 = tk.Frame(notebook) tab2 = tk.Frame(notebook) notebook.add(tab1, text="Tab 1") notebook.add(tab2, text="Tab 2") label_tab1 = tk.Label(tab1, text="Content of Tab 1") label_tab1.pack(pady=10) label_tab2 = tk.Label(tab2, text="Content of Tab 2") label_tab2.pack(pady=10) root.mainloop()
Tkinter notebook widget multiple tabs:
Notebook
widget with multiple tabs for organizing and displaying content.import tkinter as tk from tkinter import ttk root = tk.Tk() root.title("Tkinter Notebook Widget Multiple Tabs") # Create a Notebook widget notebook = ttk.Notebook(root) notebook.pack(pady=10, padx=10) # Create and add multiple tabs to the Notebook for i in range(1, 6): tab = tk.Frame(notebook) notebook.add(tab, text=f"Tab {i}") label_tab = tk.Label(tab, text=f"Content of Tab {i}") label_tab.pack(pady=10) root.mainloop()
Adding tabs to a Tkinter GUI:
Notebook
widget.import tkinter as tk from tkinter import ttk root = tk.Tk() root.title("Adding Tabs to Tkinter GUI") # Create a Notebook widget notebook = ttk.Notebook(root) notebook.pack(pady=10, padx=10) # Create and add tabs to the Notebook tab1 = tk.Frame(notebook) tab2 = tk.Frame(notebook) notebook.add(tab1, text="Tab 1") notebook.add(tab2, text="Tab 2") label_tab1 = tk.Label(tab1, text="Content of Tab 1") label_tab1.pack(pady=10) label_tab2 = tk.Label(tab2, text="Content of Tab 2") label_tab2.pack(pady=10) root.mainloop()
Tkinter tabbed frame example:
Notebook
widget for organizing and displaying content.import tkinter as tk from tkinter import ttk root = tk.Tk() root.title("Tkinter Tabbed Frame Example") # Create a Notebook widget as a tabbed frame notebook = ttk.Notebook(root) notebook.pack(pady=10, padx=10) # Create and add tabs to the Notebook tab1 = tk.Frame(notebook) tab2 = tk.Frame(notebook) notebook.add(tab1, text="Tab 1") notebook.add(tab2, text="Tab 2") label_tab1 = tk.Label(tab1, text="Content of Tab 1") label_tab1.pack(pady=10) label_tab2 = tk.Label(tab2, text="Content of Tab 2") label_tab2.pack(pady=10) root.mainloop()
Creating tabbed GUI in Python:
Notebook
widget for organizing and displaying content.import tkinter as tk from tkinter import ttk root = tk.Tk() root.title("Creating Tabbed GUI in Python") # Create a Notebook widget notebook = ttk.Notebook(root) notebook.pack(pady=10, padx=10) # Create and add tabs to the Notebook tab1 = tk.Frame(notebook) tab2 = tk.Frame(notebook) notebook.add(tab1, text="Tab 1") notebook.add(tab2, text="Tab 2") label_tab1 = tk.Label(tab1, text="Content of Tab 1") label_tab1.pack(pady=10) label_tab2 = tk.Label(tab2, text="Content of Tab 2") label_tab2.pack(pady=10) root.mainloop()