Notebook in Python GTK+ 3


In this article we will be creating the multi tabbed Notebook Graphical user interface with the help of Python GTK + 3. So we will be creating a window which will look like a notebook.

Understanding the Problem

The problem at hand is to create a notebook using Python GTK + 3. If the GTK + 3 package is not installed on your system then we can install this package with the help of command ‘pip install pygobject’ or using ‘MSYS2’ packages in windows.

So we will create a window and inside the window there will be two tabs or pages and users can interact with these pages. These tabs or pages will represent the separate pages with their own contents. The user will be able to switch between these tabs like we do in the browser. The tabs will contain different sets of information and can perform different tasks within the notebook.

Logic for The Above Problem

To solve the above problem for creating a notebook using Python GTK + 3 we will use the gtk.Notebook widget which is provided by the GTK library. In our code we are using version 3 of GTK. So basically we will create a notebook in which there will be two pages there called page 1 and page 2. In each page there will be different information provided. So we will initialize a main window and then create a notebook in it. In the notebook we can create the desired number of pages or tabs. So that the user can interact with the notebook.

Algorithm

  • Step 1 − First we will import the required libraries in our program. So we will be using the gi module and its functions.

  • Step 2 − Second we will create a class called NotebookClass and then create a main window with the help of Gtk.window. And also we will define the size of the window after that.

  • Step 3 − Next we will create a notebook with the help of Gtk.Notebook.

  • Step 4 − As we have discussed above, we will create tabs or pages in the notebook. So for creating the pages in the notebook we will use the Gtk.Box() function and this will be shown as page 1. Define its border width, content and label as Page 1.

  • Step 5 − Same as above step we will create a second page called page 2 and also define its border width, content and page label as Page 2.

  • Step 6 − Create the object of the class and call it to show the window or Notebook we have created.

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 Article")

      # Set the border width
      self.set_border_width(10)
      self.set_default_size(500, 300)

      # Create a notebook
      self.notebook = Gtk.Notebook()
      self.add(self.notebook)

      # Create the page 1
      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 page 2
      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"))


note = NotebookClass()
note.connect("destroy", Gtk.main_quit)
note.show_all()
Gtk.main()

Output

In the above example of a notebook we have shown two images for showing the different tabs of the notebook we have created using GTK +3. As per the requirement we can change the content of the tabs and manipulate accordingly.

Complexity

The time complexity for creating the Notebook using Python GTK +3 depends upon the number of tabs or pages we have created and also the content in each page. Adding or removing the tab takes a time complexity of O(1) because it operates on the notebook widget.

Updated on: 16-Oct-2023

70 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements