Create a box in Python GTK+ 3


GTK+ 3 is a sophisticated and used graphical user interface library (GUIs). It comes with an extensive range of tools and widgets for creating cross-platform interactive and appealing apps. Let us concentrate on the fundamentals of GTK+ 3 and its box layout to manage and arrange widgets within a window.

Setup

Windows users require the Windows Subsystem for Linux (WSL). It uses Linux commands and PyGObject in Windows contexts. This simplifies access to libraries and GObject Introspection bindings.

When you have it:

pip install PyGObject
sudo apt install  libcairo2-dev python3-gi gir1.2-gtk-3.0gcc 
libgirepository1.0-dev gobject-introspection pkg-config  python3-dev 

The GObject Introspection library and bindings have a good integration for programming languages.

PyGObject development tools and dependencies provide information on installed libraries. They must be installed to move ahead.

About GTK+ 3 Library

It is a cross-platform graphical user interface (GUI) toolkit. Desktop apps are the primary use case and it supports upgrades too. Available in a wide range of programming languages, including Python.

Powered with several widgets (buttons, labels, and entry fields) . These are ordered and structured using layout containers. The box layout is one such container, which allows widgets to be stacked horizontally or vertically, resulting in a versatile and dynamic user interface design. To make a box layout in Python, import the modules, and configure the GTK+ library. Then design a custom class, construct horizontal and vertical Gtk.Boxes, add widgets, and arrange them using the pack start() and pack end() methods.

Algorithm

  • Initialize the GTK library and set the required version to 3.0.

  • Import the Gtk module from the gi.repository to access GTK functionalities.

  • Define a custom class CustomBox that inherits from Gtk.Window.

  • Inside the __init__ method, initialize the window and set its title, default size, and connect the "destroy" signal to Gtk.main_quit to handle window closing.

  • Create a horizontal Gtk.Box called hbox to hold the widgets horizontally.

  • Add the hbox to the window using the self.add() method.

  • Set the Gtk.Label widgets, label1 and label2. Pack horizontally in the hbox.

  • Make a vertical Gtk.Box called vbox to hold widgets vertically.

  • Add vbox to the hbox using the hbox.add() method.

  • Create 2 more Gtk.Label widgets, label3 and label4, and pack them vertically in the vbox.

  • Create an instance of CustomBox called window.

  • Show all the widgets in the window using window.show_all().

  • Start the main GTK loop using Gtk.main() to handle events and user interactions.

Example

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class CustomBox(Gtk.Window):
   def __init__(self):
      Gtk.Window.__init__(self)
      self.set_title("Custom GTK +3 Box")
      self.set_default_size(300, 300)
      self.connect("destroy", Gtk.main_quit)

      hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=5)
      self.add(hbox)

      label1 = Gtk.Label(label="TutorialsPoint")
      hbox.pack_start(label1, True, True, 0)

      label2 = Gtk.Label(label="Atharva Shah")
      hbox.pack_start(label2, True, True, 0)

      vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=5)
      hbox.add(vbox)

      label3 = Gtk.Label(label="GTK Tutorial")
      vbox.pack_start(label3, True, True, 0)

      label4 = Gtk.Label(label="Try to Resize the Window")
      vbox.pack_start(label4, True, True, 0)

window = CustomBox()
window.show_all()
Gtk.main()

Output

Explanation

The code initializes the GTK library and imports the Gtk module. Define the CustomBox class, with a window title and default size. The "destroy" signal is coupled to the Gtk.main quit function for graceful window shutting. A horizontal Gtk. A hbox accommodates horizontal widgets, while a vertical Gtk Box vBox holds vertical widgets. Generate two more GTK Label3 and label4 widgets.

First with the text "GTK Tutorial" and second with "Try to Resize the Window."

A CustomBox object (window) and all widgets are displayed using the window. show all" (). Gtk.main() initiates the main GTK loop, which handles user interactions and events.

The output has a custom window with horizontal Gtk labels grouped in a box style. Vertical GTK box with two labels side by side. Two labels are layered on top of a box.

On maximizing the window, the labels update.

Conclusion

GTK +3 is used to create a user-friendly interface by grouping widgets within the window using the box layout. With an array of useful widgets and assets, it is important to develop complicated GUI interfaces that are cross-platform. The article showed how to create a basic GTK application and layout widgets using boxes.

Updated on: 09-Aug-2023

513 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements