How to Create GUI Applications Under Linux Desktop Using PyGObject – Part 1

Creating applications on Linux can be done using different ways, but there are a limited ways of doing, so using the simplest and the most functional programming languages and libraries, that’s why we’re going to have a quick look about creating applications under the Linux desktop using the GTK+ library with Python programming language which is called “PyGObject”.

PyGObject uses the GObject Introspection to create binding for programming languages like Python, PyGObject is the next generation from PyGTK, you can say that PyGObject = Python + GTK3.

Create GUI Applications in Linux
Create GUI Applications in Linux – Part 1

Today, we’re going to start a series about creating GUI (Graphical User Interface) applications under the Linux desktop using GTK+ library and PyGobject language, the series will cover the following topics:

Part 1: How to Create GUI Applications Under Linux Desktop Using PyGObject
About Python

First of all, you must have some basic knowledge in Python; Python is a very modern and easy to use programming language. It’s one of the most famous programming languages in the world, using Python, you will be able to create many great applications & tools. You may take some free courses like those at codeacademy.com or you may read some books about Python at:

Dive Into Python
Python Testing: Beginner's Guide
Making Games with Python & Pygame
Invent Your Own Computer Games with Python
About GTK+

GTK+ is an open-source cross-platform toolkit to create graphical user interfaces for desktop applications, it was first started in 1998 as a GUI toolkit for the GIMP, later, it was used in many other applications and soon became one of the most famous libraries to create GUIs. GTK+ is released under the LGPL license.

Creating GUI Applications Under Linux

There are 2 ways for creating the applications using GTK+ & Python:

  1. Writing the graphical interface using code only.
  2. Designing the graphical interface using the “Glade” program; which is RAD tool to design GTK+ interfaces easily, Glade generates the GUI as a XML file which can be used with any programming language to build the GUI, after exporting the GUI’s XML file, we’ll be able to link the XML file with our program to do the jobs we want.

We’ll explain both ways in short.

The Code-Only Way

Writing the GUI using code only can be little bit hard for noob programmer’s and very time-wasting, but using it, we can create very functional GUIs for our programs, more than those we create using some tools like Glade.

Let’s take the following example.

#!/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()

Copy the above code, paste it in a “test.py” file and set 755 permission on the test.py file and run the file later using “./test.py”, that’s what you will get.

# nano test.py
# chmod 755 test.py
# ./test.py
Hello World Script
Hello World Script

By clicking the button, you see the “Hello, World!” sentence printed out in the terminal:

Test Python Script
Test Python Script

Let me explain the code in detailed explanation.

  1. #!/usr/bin/python: The default path for the Python interpreter (version 2.7 in most cases), this line must be the first line in every Python file.
  2. # -*- coding: utf-8 -*-: Here we set the default coding for the file, UTF-8 is the best if you want to support non-English languages, leave it like that.
  3. from gi.repository import Gtk: Here we are importing the GTK 3 library to use it in our program.
  4. Class ourwindow(Gtk.Window): Here we are creating a new class, which is called “ourwindow”, we are also setting the class object type to a “Gtk.Window”.
  5. def __init__(self): Nothing new, we’re defining the main window components here.
  6. Gtk.Window.__init__(self, title=”My Hello World Program”): We’re using this line to set the “My Hello World Program” title to “ourwindow” window, you may change the title if you like.
  7. Gtk.Window.set_default_size(self, 400,325): I don’t think that this line need explanation, here we’re setting the default width and height for our window.
  8. Gtk.Window.set_position(self, Gtk.WindowPosition.CENTER): Using this line, we’ll be able to set the default position for the window, in this case, we set it to the center using the “Gtk.WindowPosition.CENTER” parameter, if you want, you can change it to “Gtk.WindowPosition.MOUSE” to open the window on the mouse pointer position.
  9. button1 = Gtk.Button(“Hello, World!”): We created a new Gtk.Button, and we called it “button1”, the default text for the button is “Hello, World!”, you may create any Gtk widget if you want.
  10. button1.connect(“clicked”, self.whenbutton1_clicked): Here we’re linking the “clicked” signal with the “whenbutton1_clicked” action, so that when the button is clicked, the “whenbutton1_clicked” action is activated.
  11. self.add(button1): If we want our Gtk widgets to appear, we have to add them to the default window, this simple line adds the “button1” widget to the window, it’s very necessary to do this.
  12. def whenbutton1_clicked(self, button): Now we’re defining the “whenbutton1_clicked” action here, we’re defining what’s going to happen when the “button1” widget is clicked, the “(self, button)” parameter is important in order to specific the signal parent object type.
  13. print “Hello, World!”: I don’t have to explain more here.
  14. window = ourwindow(): We have to create a new global variable and set it to ourwindow() class so that we can call it later using the GTK+ library.
  15. window.connect(“delete-event”, Gtk.main_quit): Now we’re connecting the “delete-event” signal with the “Gtk.main_quit” action, this is important in order to delete all the widgets after we close our program window automatically.
  16. window.show_all(): Showing the window.
  17. Gtk.main(): Running the Gtk library.

That’s it, easy isn’t? And very functional if we want to create some large applications. For more information about creating GTK+ interfaces using the code-only way, you may visit the official documentation website at:

Python GTK3 Tutorials

The Glade Designer Way

Like I said in the beginning of the article, Glade is a very easy tool to create the interfaces we need for our programs, it’s very famous among developers and many great applications interfaces were created using it. This way is called “Rapid applications development”.

You have to install Glade in order to start using it, on Debian/Ubuntu/Mint run:

$ sudo apt­-get install glade

On RedHat/Fedora/CentOS, run:

# yum install glade

After you download and install the program, and after you run it, you will see the available Gtk widgets on the left, click on the “window” widget in order to create a new window.

Create New Widget
Create New Widget

You will notice that a new empty window is created.

New Window Widget
New Window Widget

You can now add some widgets to it, on the left toolbar, click on the “button” widget, and click on the empty window in order to add the button to the window.

Add Widget
Add Widget

You will notice that the button ID is “button1”, now refer to the Signals tab in the right toolbar, and search for the “clicked” signal and enter “button1_clicked” under it.

Glade Button Properties
Button Properties
Glade Signals Tab
Signals Tab

Now that we’ve created our GUI, let’s export it. Click on the “File” menu and choose “Save”, save the file in your home directory in the name “myprogram.glade” and exit.

Glade Export Widget File
Export Widget File

Now, create a new “test.py” file, and enter the following code inside it.

#!/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("myprogram.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, give it 755 permissions like before, and run it using “./test.py”, and that’s what you will get.

# nano test.py
# chmod 755 test.py
# ./test.py
Hello World Window
Hello World Window

Click on the button, and you will notice that the “Hello, World!” sentence is printed in the terminal.

Now let’s explain the new things:

  1. class Handler: Here we’re creating a class called “Handler” which will include the the definitions for the actions & signals, we create for the GUI.
  2. builder = Gtk.Builder(): We created a new global variable called “builder” which is a Gtk.Builder widget, this is important in order to import the .glade file.
  3. builder.add_from_file(“myprogram.glade”): Here we’re importing the “myprogram.glade” file to use it as a default GUI for our program.
  4. builder.connect_signals(Handler()): This line connects the .glade file with the handler class, so that the actions and signals that we define under the “Handler” class work fine when we run the program.
  5. ournewbutton = builder.get_object(“button1”): Now we’re importing the “button1” object from the .glade file, we’re also passing it to the global variable “ournewbutton” to use it later in our program.
  6. ournewbutton.set_label(“Hello, World!”): We used the “set.label” method to set the default button text to the “Hello, World!” sentence.
  7. window = builder.get_object(“window1”): Here we called the “window1” object from the .glade file in order to show it later in the program.

And that’s it! You have successfully created your first program under Linux!

Of course there are a lot more complicated things to do in order to create a real application that does something, that’s why I recommend you to take a look into the GTK+ documentation and GObject API at:

  1. GTK+ Reference Manual
  2. Python GObject API Reference
  3. PyGObject Reference

Have you developed any application before under the Linux desktop? What programming language and tools have used to do it? What do you think about creating applications using Python & GTK 3?

If you liked this article, then do subscribe to email alerts for Linux tutorials. If you have any questions or doubts? do ask for help in the comments section.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

23 thoughts on “How to Create GUI Applications Under Linux Desktop Using PyGObject – Part 1”

  1. Thank you Hanny and tecmint.com for publishing this wonderful, step by step, instructional article, covering all aspects of GUI design, including packaging. In fact, I found the “Desktop Recorder” Application in part 3, so useful, that I developed a couple of my own applications, on similar lines and even packaged it for Debian as well.

    The entire project ( including packaging for Debian ) that I did, is shared for the benefit of viewers here : https://sourceforge.net/projects/python-glade-application-desig/

    It can resolve most of the issues reported here and also give tips on coding and packaging as well. If you need any more inputs, a simple Google Search will do.

    Also, tips on Glade File Design can be seen here : https://youtu.be/86QppCSRNkI and tips on icon design here : https://youtu.be/arPVw8VWM_0

    Reply
      • P.S. While Porting the above sources from Python 2.7 to Python 3.5, I realized a few new things :

        1. Support for tabs as an indentation method, has been removed
        2. 4 spaces is the preferred method of indentation
        3. Modules like subprocess, stringio etc. has undergone changes.

        I have upgraded the sources mentioned above, to reflect these new changes. Please download them again to get the latest version.

        Reply
        • After gaining confidence, in Coding & Packaging, using Hanny’s Examples & the Sources Shared above, Viewers may try developing a few simple Apps, like for eg.

          1. A Manpage Creator/Studio App ( Just Google “linux create man page”. You will find examples / structure of a man page. It is just a matter of Adding a Few Special Keywords, while generating/filing a manpage, with your GUI Text Entry’s / Text View’s )

          2. A Diary App ( Just a matter of Reading / Writing to Files, based on the Date Picked, on a Calendar Widget )

          3. GUI for OS Commands, that can simplify tasks ( Accept Input on Text Entry’s/Views, Process Them, Show Result on Text Entry’s/Views )

          4. Apps listed on your Software Manager, to make them even better

          If you have developed a Unique, Well Tested ( Across Python Versions i.e. Change First Line of Script ) ( Across Linux Flavours / Versions i.e. Using Virtual Machines ) & Useful App for the World, then you may contact Debian Developers/Mentors ( Sponsors ) on the Debian IRC Channels List (Country Specific also available), who can help you, Publish Your Apps to the World. Also Sponsors Listed on their ftp-master site can be contacted on their Email for further progress.

          Note: Utility Apps may require multiple GIT Setups, for Upstream / Downstream Releases.

          Good Luck With Your Apps Development / Publishing !

          Reply
        • Some info: The #!/usr/bin/python line in the test.py code file above, refers to python 2 series, which is marked for end-of-life in 2020. Hence, viewers can substitute python with python3 in the test.py file, as well as the debian/control and debian/rules files, explained in the part 4 article of this series.

          I have upgraded my sources shared above, to reflect all these, and incremented the version numbers to 2.1-1. Future upgrades to them will not carry a notice here.

          Reply
  2. I am new in developing GUI Application. Which is the best way to develop GUI App in gtk?

    Using Glade and writing GUI interface design code

    Reply
  3. None of your examples work…copying and pasting the code you provided gives me nothing but errors:

    “IndentationError: expected an indented block”

    PyGIWarning: Gtk was imported without specifying a version first. Use gi.require_version(‘Gtk’, ‘3.0’) before import to ensure that the right version gets loaded.
    from gi.repository import Gtk

    Reply
    • Had the same issue. A bit of Google and I found this:

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

      Concerning indention:
      Seems like the formatting was just lost. In python tabulation is used instead of bracketing. Colon must be followed by tabulated code, meaning that it’s a part of the block. It’s easy to restore if you can read the code for the slightest.

      Reply
  4. There is an error in the script of the builder. The name of the function in the Handler() should be the same as the one in glade.

    Instead of buttonclicked, it should be button1_clicked ;)

    Thanks for this tuto! Really helpful

    Reply

Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.