How to Create GUI Applications Using PyGObject in Linux

Creation of GUI Applications Using PyGObject Under Linux Desktop

Different ways to create GUI applications using PyGObject in Linux are discussed in this article.

Creation of GUI Applications Under Linux

Following are the ways to create the applications using PyGObject.

Using Code-Only

Create functional GUI' s for our programs. Here is an example,

[root@linuxhelp Desktop]# nano sample.py
GNU nano 2.3.1                  File: sample.py                                            
#!/usr/bin/python
# -*- coding: utf-8 -*-

from gi.repository import Gtk

class ourwindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title=" My Hello World Program" )
        Gtk.Window.set_default_size(self, 400,325)
        Gtk.Window.set_position(self, Gtk.WindowPosition.CENTER)

        button1 = Gtk.Button(" Hello, World!" )
        button1.connect(" clicked" , self.whenbutton1_clicked)

        self.add(button1)

    def whenbutton1_clicked(self, button):
      print " Hello, World!" 

window = ourwindow()
window.connect(" delete-event" , Gtk.main_quit)
window.show_all()
Gtk.main()

Paste the above code in “ sample.py” file. Set 755 permission and execute it.

[root@linuxhelp Desktop]# chmod 755 sample.py 
[root@linuxhelp Desktop]# ./sample.py 
Hello, World!
Hello, World!
Hello, World!

You can view the “ Hello, World!” sentence in the terminal, by clicking the button.

Using Glade Designer

An easy tool for the creation of the GUI interfaces is Glade. Install and execute Glade using the following command.

[root@linuxhelp Desktop]# yum install glade
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: centos.excellmedia.net
 * extras: centos.excellmedia.net
 * updates: centos.excellmedia.net
Resolving Dependencies
-->  Running transaction check
--->  Package glade.x86_64 0:3.15.0-5.el7 will be installed
.
.
.
  Installing : glade-3.15.0-5.el7.x86_64                                                 1/1 
  Verifying  : glade-3.15.0-5.el7.x86_64                                                 1/1 

Installed:
  glade.x86_64 0:3.15.0-5.el7                                                                

Complete!

[root@linuxhelp Desktop]# glade

In the left panel select the window icon, For creation of a new window.

We can Insert several widgets by using control and display palette . For Example select and insert the button to the window by clicking on the empty window.

Right click the “ button1 GTK Button “ from the project view panel. And select “ Edit Separately” .

Button properties window will appears as follows,

Select “ signal” tab, Find the “ clicked” and set “ button1_clicked” in handler option.

And then save the project.

We have successfully created GUI. Save the file in the name “ test.glade” in your Desktop directory. Now create “ testglad.py” file.

[root@linuxhelp Desktop]# nano testglad.py
Insert the following code in the newly created “ testglad.py”  file.

#!/usr/bin/python
# -*- coding: utf-8 -*-

from gi.repository import Gtk

class Handler:
    def button_1clicked(self, button):
      print " Hello, World!" 

builder = Gtk.Builder()
builder.add_from_file(" test.glade" )
builder.connect_signals(Handler())

ournewbutton = builder.get_object(" button1" )
ournewbutton.set_label(" Hello, World!" )

window = builder.get_object(" window1" )

window.connect(" delete-event" , Gtk.main_quit)
window.show_all()
Gtk.main()

Save the file and Set the 755 permission for testglad.py file and execute it.

[root@linuxhelp Desktop]# chmod -R 755 testglad.py 
[root@linuxhelp Desktop]# ./testglad.py

Tag : GUI
FAQ
Q
Who is using the PyGObject currently in Linux?
A
here you can see the PyGObject currently in Linux,
1.Anaconda - an installation program used by Fedora, RHEL, and others
2. D-Feet - an easy to use D-Bus debugger
3. GNOME Music - a music player for GNOME
4. GNOME Tweak Tool - a tool to customize advanced GNOME 3 options
5. Gramps - a genealogy program
6. Lollypop - a modern music player
7. Meld - a visual diff and merge tool
8. MyPaint - a nimble, distraction-free, and easy tool for digital painters
9. Orca - a flexible and extensible screen reader
10. Pithos - a Pandora Radio client
11. Pitivi - a free and open source video editor
12. Quod Libet - a music library manager/player
13. Transmageddon - a video transcoder
Q
what is mean by PyGObject?
A
PyGObject is a Python package which provides bindings for GObject based libraries such as GTK+, GStreamer, WebKitGTK+, GLib, GIO and many more.
Q
How can I use PyGObject with the official CPython builds on Windows?
A
The following link will provide binaries which should be ABI compatible with the official CPython binaries. I’d recommend using msys2 if at all possible since there are more people involved and it’s easier to fix/patch things yourself. "https://sourceforge.net/projects/pygobjectwin32/"
Q
The PyGObject is able to act as an image editing tool?
A
Not exactly, it’s a graphical developing tool but it also having the basic functionality to make an image editing process.
Q
How can I check the version of PyGObject?
A
You can check the version of PyGObject where found in "Help-> About"