VOOZH about

URL: https://www.tecmint.com/create-more-advance-gui-applications-in-linux/

⇱ Create More Advance GUI Applications Using PyGobject Tool in Linux - Part 2


Skip to content

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.

πŸ‘ Create Gui Applications in Linux
Create Gui Applications in Linux- Part 2

Requirements

  1. Create GUI Applications Under Linux Using PyGObject – Part 1

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.

πŸ‘ Glade Designer
Glade Designer

2. Click on the β€œWindow” button on the left sidebar in order to create a new one.

πŸ‘ Create New Window
Create New Window

3. Click on the β€œBox” widget and release it on the empty window.

πŸ‘ Select Box Widget
Select Box Widget

4. You will be prompted to enter the number of boxes you want, make it 3.

πŸ‘ Create Boxes
Create Boxes

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.

πŸ‘ Make Box Horizontal
Make Box 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.

πŸ‘ Create Simple Program
Create Simple Program

7. Now click on the β€œwindow1” widget from the right sidebar, and change its position to β€œCenterβ€œ.

πŸ‘ Make Widget Center
Make Widget Center

Scroll down to the β€œAppearance” section.. And add a title for the window β€œMy Programβ€œ.

πŸ‘ Add Widget Title
Add Widget Title

8. You can also choose an icon for the window by clicking on the β€œIcon Name” box.

πŸ‘ Set Widget Icon
Set Widget Icon

9. You can also change the default height & width for the application.. After all of that, you should have something like this.

πŸ‘ Set Widget Height Width
Set Widget Height Width

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.

πŸ‘ Create About Window
Create About Window

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.

πŸ‘ Set Widget Signals
Set Widget Signals

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β€œ.

πŸ‘ Add Widget Handler
Add Widget Handler

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).

πŸ‘ Set Default Focus
Set Default Focus

12. Now from the left sidebar, create a new β€œAbout Dialog” window.

πŸ‘ Create About Dialog
Create About Dialog

And you will notice that the β€œAbout Dialog” window is created.

πŸ‘ About Dialog
About Dialog

Let’s modify it.. Make sure that you insert the following settings for it from the right sidebar.

πŸ‘ Add Program Attributes
Add Program Attributes
πŸ‘ Select License
Select License
πŸ‘ Add About Authors
Add About Authors
πŸ‘ Set Window Appreance
Set Window Appreance
πŸ‘ Select Appreance Flags
Select Appreance Flags

After making above settings, you will get following about Window.

πŸ‘ My Program about Window
My Program 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.

πŸ‘ Change Window Boxes
Change Window Boxes

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.
πŸ‘ My Program Window
My Program Window

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.

πŸ‘ Box Output Text
Box Output Text

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, share it with someone on your team.
TecMint Weekly Newsletter
Get the Learn Linux 7 Days Crash Course free when you join 34,000+ Linux professionals reading every Thursday.
Check your email for a magic link to get started.
Something went wrong. Please try again.
β˜•
TecMint has been free for 14 years. Help keep it that way.
Google AI Overviews and tools like ChatGPT have cut into search traffic for independent tech sites like TecMint. Running this site costs over $2,000 every month for hosting, infrastructure, and paying authors to keep the content accurate and tested.

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.
β˜• Buy Me a Coffee
Hanny Helal
A Linux & Foss user since 2010, working on many projects in the field of Free Software.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

8 Comments

Leave a Reply
  1. I am not getting any output. The first program executed quite well but this is not working. I have no error but also no output. Please help.

    Reply
  2. I have created a new window separately how I can get the objects of the new window and how I can connect these two windows

    Reply
  3. none type has no attribute append

    Reply
  4. Great Tutorial, really.
    but I cannot get the expected result. The output I got rather is:
    usr/bin/python: bad interpreter: No such file or directory
    I guess the first line should read as following
    #!/usr/bin/python (you have put β€˜/usr/bin/python
    I’d appreciate any clue on this.

    Reply
    • Hello txe.

      Yes, the correct line is: #!/usr/bin/python.
      Please make sure that Python 2.7 package is installed, on Debian-based distros you may run the following command:
      $ sudo apt-get install python2.7

      Reply
      • @Hanny,
        Corrected in the writeup..please check and confirm..

        Reply
        • Yes Ravi, it’s correct now.

          Reply
  5. Great Tutorial, really.
    but I cannot get the expected result. The output I got rather is:
    usr/bin/python: bad interpreter: No such file or directory
    I guess the first line should read as following
    #!/usr/bin/python (you have put β€˜/usr/bin/python)
    I’d appreciate any clue on this. ~~
    but Ieven after changing it I keep getting the wron

    Reply

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

Free Course
Get a free Linux course before you go.
Subscribe to TecMint Weekly and get the Learn Linux 7 Days Crash Course free. Read by 34,000+ Linux professionals every Thursday.
Check your email for a magic link to get started.