• *Wellcome Guest This Forum is created to help each other. Join us in our journey.

    * Guest Earn Piont Every Post, like, comment,and etc.

How to Create a Simple Application in Python and GTK3

N

Nitesh Kumar

Guest
This article will explain how to create a graphical “Hello World” application in Linux using Python 3 programming language and GTK3 GUI framework. All code samples in the tutorial are tested with Python 3.8.6 and GTK 3.24.23 on Ubuntu 20.10.

Install Necessary Packages


To create an app using Python 3 and GTK3, you need to install Python bindings for GObject libraries. This can be done by installing the PyGObject package included in repositories of almost all major Linux distributions. To install these dependencies in Ubuntu, you can use the command below:

$ sudo apt install python3 python3-gi

You can also compile PyGObject from source code available here.

Full Code of the Hello World Application


Full code sample of a Hello World application written in Python 3 and GTK3 can be found below. This application sends a “Hello World !!” notification to the desktop on click of a button.

import gi

gi.require_version("Gtk", "3.0")

gi.require_version('Notify', '0.7')

from gi.repository import Gtk

from gi.repository import Notify

class MyWindow(Gtk.Window):

def __init__(self):

Gtk.Window.__init__(self, title="Hello World")

Gtk.Window.set_default_size(self, 640, 480)

Notify.init("Simple GTK3 Application")

self.box = Gtk.Box(spacing=6)

self.add(self.box)

self.button = Gtk.Button(label="Click Here")

self.button.set_halign(Gtk.Align.CENTER)

self.button.set_valign(Gtk.Align.CENTER)

self.button.connect("clicked", self.on_button_clicked)

self.box.pack_start(self.button, True, True, 0)

def on_button_clicked(self, widget):

n = Notify.Notification.new("Simple GTK3 Application", "Hello World !!")

n.show()

win = MyWindow()

win.connect("destroy", Gtk.main_quit)

win.show_all()

Gtk.main()

Screenshot of the final result.



The desktop notification that arrives when “Click Here” button is clicked:



Step-by-Step Explanation


Various “import” statements in the first few lines of the code import necessary modules required for the application to work. These modules expose numerous classes and functions that can be used within the application. “Gi.require_version” statements ensure that only the required version of the library is imported to avoid compatibility issues and crashes. In this case “3.0” is used to make sure that GTK3 library is used in the application and not GTK2 or any other version of GTK.

Similar to the GTK class, Notify class is also imported by specifying its required version (0.7 is that latest version at the time of writing this article). This class will be used later in the application.

import gi

gi.require_version("Gtk", "3.0")

gi.require_version('Notify', '0.7')

from gi.repository import Gtk

from gi.repository import Notify

The next statement subclasses “Gtk.Window” class as “MyWindow” class. The “Gtk.Window.__init__” method initializes the constructor of the super class (Gtk.Window) from which a subclass (MyWindow) was created. In the constructor, the application title is also set as “Hello World” using the “title” argument. Default geometry of the application window is also set by specifying width and height in the “set_default_size” method.

class MyWindow(Gtk.Window):

def __init__(self):

Gtk.Window.__init__(self, title="Hello World")

Gtk.Window.set_default_size(self, 640, 480)

Next, “init” method of the Notify class is used to initialize the “libnotify” library by supplying an application title. Without initialization, notifications won’t be sent and shown on the Linux desktop.

Notify.init("Simple GTK3 Application")

The “box” statement adds an empty layout container to the main application window. This container is like an empty canvas where any number of widgets can be put on. “Spacing” refers to the space between widgets in the “pixel” units.

self.box = Gtk.Box(spacing=6)

self.add(self.box)

A new button widget “Click Here” is created using the “Gtk.Button” method. It is then aligned to horizontal and vertical centers of the parent box layout using “set_halign” and “set_valign” methods. The button is connected to the “clicked” signal so that whenever the button is pressed, the callback method “on_button_clicked” can be invoked. Lastly, button widget is inserted from left along the horizontal axis to the box layout using the “box.pack_start” method. This method takes four arguments: the widget object to be added, boolean for expanding the widget, boolean for filling the widget, and padding between the added and other adjacent widgets.

self.button = Gtk.Button(label="Click Here")

self.button.set_halign(Gtk.Align.CENTER)

self.button.set_valign(Gtk.Align.CENTER)

self.button.connect("clicked", self.on_button_clicked)

self.box.pack_start(self.button, True, True, 0)

The “on_button_click” method is invoked whenever the “Click Here” button is pressed. A new notification is created using the “new” method which takes notification title and content as arguments. This notification is then shown on the desktop using the “show” method. You can also move the “new” statement in the main constructor to avoid recreating the notification whenever the button is pressed.

def on_button_clicked(self, widget):

n = Notify.Notification.new("Simple GTK3 Application", "Hello World !!")

n.show()

The next few statements are about creating, managing and showing a new application window. A new instance of “MyWindow” class is created and it is connected to “destroy” signal to ensure that application is closed properly whenever a user decides to quit application by clicking on the “x” button. The “show_all” method displays the application window on your Linux desktop. Lastly, the main application loop responsible for handling events and signals is run.

Conclusion


GTK3 and Qt are some of the most popular GUI frameworks used for creating applications that run natively on Linux. Both these frameworks allow you to write main logic in C++ and Python languages. You can’t go wrong by choosing any of these toolkits for your next Linux application project.

Credit to linux
 

Latest threads

Top