We continue our series about creating GUI applications under the Linux desktop using PyGObject, This is the second part of the series and today weβll be talking about creating more functional applications using some advanced widgets.
Requirements
In the previous article we said that there are two ways for creating GUI applications using PyGObject: the code-only-way and the Glade designer way, but from now on, weβll only be explaining the Glade designer way since itβs much easier for most users, you can learn the code-only-way by yourself using python-gtk3-tutorial.
Creating Advance GUI Applications in Linux
1. Letβs start programming! Open your Glade designer from the applications menu.
2. Click on the βWindowβ button on the left sidebar in order to create a new one.
3. Click on the βBoxβ widget and release it on the empty window.
4. You will be prompted to enter the number of boxes you want, make it 3.
And youβll see that the boxes are created, those boxes are important for us in order to be able to add more than just 1 widget in a window.
5. Now click on the box widget, and change the orientation type from vertical to horizontal.
6. In order to create a simple program, add a βText Entryβ, βCombo Box Textβ and a βButtonβ widgets for each one of the boxes, you should have something like this.
7. Now click on the βwindow1β widget from the right sidebar, and change its position to βCenterβ.
Scroll down to the βAppearanceβ section.. And add a title for the window βMy Programβ.
8. You can also choose an icon for the window by clicking on the βIcon Nameβ box.
9. You can also change the default height & width for the application.. After all of that, you should have something like this.
In any program, one of the most important thing is to create a βAboutβ window, to do this, first weβll have to change the normal button we created before into a stock button, look at the picture.
10. Now, weβll have to modify some signals in order to run specific actions when any event occur on our widgets. Click on the text entry widget, switch to the βSignalsβ tab in the right sidebar, search for βactivatedβ and change its handler to βenter_button_clickedβ, the βactivatedβ signal is the default signal that is sent when the βEnterβ key is hit while focusing on the text entry widget.
Weβll have to add another handler for the βclickedβ signal for our about button widget, click on it and change the βclickedβ signal to βbutton_is_clickedβ.
11. Go to the βCommonβ tab and mark on βHas Focusβ as it follows (To give the default focus for the about button instead of the entry).
12. Now from the left sidebar, create a new βAbout Dialogβ window.
And you will notice that the βAbout Dialogβ window is created.
Letβs modify it.. Make sure that you insert the following settings for it from the right sidebar.
After making above settings, you will get following about Window.
In the above window, you will notice the empty space, but you can remove it by declining the number of boxes from 3 to 2 or you can add any widget to it if you want.
13. Now save the file in your home folder in the name βui.gladeβ and open a text editor and enter the following code inside it.
#!/usr/bin/python
# -*- coding: utf-8 -*-
from gi.repository import Gtk
class Handler:
def button_is_clicked(self, button):
## The ".run()" method is used to launch the about window.
ouraboutwindow.run()
## This is just a workaround to enable closing the about window.
ouraboutwindow.hide()
def enter_button_clicked(self, button):
## The ".get_text()" method is used to grab the text from the entry box. The "get_active_text()" method is used to get the selected item from the Combo Box Text widget, here, we merged both texts together".
print ourentry.get_text() + ourcomboboxtext.get_active_text()
## Nothing new here.. We just imported the 'ui.glade' file.
builder = Gtk.Builder()
builder.add_from_file("ui.glade")
builder.connect_signals(Handler())
ournewbutton = builder.get_object("button1")
window = builder.get_object("window1")
## Here we imported the Combo Box widget in order to add some change on it.
ourcomboboxtext = builder.get_object("comboboxtext1")
## Here we defined a list called 'default_text' which will contain all the possible items in the Combo Box Text widget.
default_text = [" World ", " Earth ", " All "]
## This is a for loop that adds every single item of the 'default_text' list to the Combo Box Text widget using the '.append_text()' method.
for x in default_text:
ourcomboboxtext.append_text(x)
## The '.set.active(n)' method is used to set the default item in the Combo Box Text widget, while n = the index of that item.
ourcomboboxtext.set_active(0)
ourentry = builder.get_object("entry1")
## This line doesn't need an explanation :D
ourentry.set_max_length(15)
## Nor this do.
ourentry.set_placeholder_text("Enter A Text Here..")
## We just imported the about window here to the 'ouraboutwindow' global variable.
ouraboutwindow = builder.get_object("aboutdialog1")
## Give that developer a cookie !
window.connect("delete-event", Gtk.main_quit)
window.show_all()
Gtk.main
Save the file in your home directory under that name βmyprogram.pyβ, and give it the execute permission and run it.
$ chmod 755 myprogram.py $ ./myprogram.py
This is what you will get, after running above script.
Enter a text in the entry box, hit the βEnterβ key on the keyboard, and you will notice that the sentence is printed at the shell.
Thatβs all for now, itβs not a complete application, but I just wanted to show you how to link things together using PyGObject, you can view all methods for all GTK widgets at gtkobjects.
Just learn the methods, create the widgets using Glade, and connect the signals using the Python file, Thatβs it! Itβs not hard at all my friend.
Weβll explain more new things about PyGObject in the next parts of the series, till then stay updated and donβt forget to give us your comments about the article.
If this article helped you solve a problem, consider buying a coffee. It helps keep TecMint free, supports the authors, and keeps the project going.

Got Something to Say? Join the Discussion... Cancel reply