Notebook in Python GTK+ 3

In this article we will create a multi-tabbed Notebook graphical user interface using Python GTK+ 3. A notebook widget provides tabbed pages similar to browser tabs, allowing users to switch between different content areas.

Understanding the Problem

We need to create a notebook interface using Python GTK+ 3. If GTK+ 3 is not installed, you can install it using pip install pygobject on Linux/macOS or MSYS2 packages on Windows.

Our notebook will contain multiple tabs or pages, each with separate content. Users can switch between these tabs just like in a web browser. Each tab can contain different widgets and perform different tasks within the notebook interface.

Implementation Approach

We'll use the Gtk.Notebook widget from the GTK library. Our implementation will create a main window containing a notebook with two pages, each displaying different content.

Algorithm

  • Step 1 Import required libraries including the gi module

  • Step 2 Create a class inheriting from Gtk.Window and set window properties

  • Step 3 Create a notebook widget using Gtk.Notebook()

  • Step 4 Create first page using Gtk.Box() with content and label

  • Step 5 Create second page with different content and label

  • Step 6 Initialize the application and display the notebook window

Example

# Import the necessary libraries
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

# Create a class to create a notebook
class NotebookClass(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="Notebook Tutorial")
        
        # Set the border width and window size
        self.set_border_width(10)
        self.set_default_size(500, 300)
        
        # Create a notebook widget
        self.notebook = Gtk.Notebook()
        self.add(self.notebook)
        
        # Create the first page
        self.page1 = Gtk.Box()
        self.page1.set_border_width(50)
        self.page1.add(Gtk.Label(label="This is the content of page 1"))
        self.notebook.append_page(self.page1, Gtk.Label(label="Page 1"))
        
        # Create the second page
        self.page2 = Gtk.Box()
        self.page2.set_border_width(50)
        self.page2.add(Gtk.Label(label="This is the content of page 2"))
        self.notebook.append_page(self.page2, Gtk.Label(label="Page 2"))

# Create and run the application
note = NotebookClass()
note.connect("destroy", Gtk.main_quit)
note.show_all()
Gtk.main()

Output

The program creates a window with two tabs labeled "Page 1" and "Page 2". Users can click between tabs to view different content in each page.

Notebook Tutorial Page 1 Page 2 This is the content of page 1

Key Features

  • Tab Navigation Users can click tabs to switch between pages

  • Flexible Content Each page can contain different widgets and layouts

  • Dynamic Pages Pages can be added or removed programmatically

  • Customizable Labels Tab labels can be text or even widgets

Complexity

The time complexity for creating a notebook depends on the number of pages and their content. Adding or removing tabs operates in O(1) time since it directly manipulates the notebook widget's internal structure.

Conclusion

GTK+ 3 Notebook widgets provide an efficient way to organize multiple content areas in a tabbed interface. This pattern is ideal for applications requiring organized, switchable content sections.

Updated on: 2026-03-27T15:25:52+05:30

368 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements