Open In App

How to Create GUI Applications Under Linux Desktop Using PyGObject

Improve
Improve
Like Article
Like
Save
Share
Report

The creation of applications in Linux can be done through various methods. But, the most efficient way of creating a GUI application in Linux can be done through PyGObject in  Python. PyGObject is the next generation from the PyGTK library in Python, we can say that PyGObject = Python + GTK3. So, in this article, we will be creating a GUI application under a Linux environment using PyGObject.

Note: Make sure you have installed Python on your Linux machine.

There are two ways for creating GUI applications in Linux.

  • The Code-Only Method
  • Glade Designer Method

Method 1: The Code Only Method

In this method, we will be creating the GUI components directly by program code, rather than using any drag and drop approach. So follow the below steps to create the application using the code-only method.

Step 1: Create the test.py file by using a text editor.

nano test.py

How to Create GUI Applications Under Linux Desktop Using PyGObject

Step 2: Write the following code in test.py file.

Python3




#!/usr/bin/python3
# -*- coding: utf-8 -*-
  
from gi.repository import Gtk
  
class ourwindow(Gtk.Window):
  
    def __init__(self):
        Gtk.Window.__init__(self, title="Demonstration\
        of PyObject GUI Application Creation")
        Gtk.Window.set_default_size(self, 400,325)
        Gtk.Window.set_position(self, Gtk.WindowPosition.CENTER)
  
        button1 = Gtk.Button("GeeksforGeeks")
        button1.connect("clicked", self.whenbutton1_clicked)
  
        self.add(button1)
          
    def whenbutton1_clicked(self, button):
      print("GeeksforGeeks")
  
window = ourwindow()        
window.connect("delete-event", Gtk.main_quit)
window.show_all()
Gtk.main()


Code Explanation:

  1. #!/usr/bin/python3: This line specify the default Python3 interpreter path.
  2. # -*- coding: utf-8 -*-: In this line of code, we have specified the default Unicode as UTF-8.
  3. from gi.repository import Gtk: Here, we are importing the GTK 3 library.
  4. Class ourwindow(Gtk.Window): In this, we are creating a class named as ourWindow and setting the class object type as Gtk.Window.
  5. def __init__(self): In this, we are defining the main window components.
  6. Gtk.Window.__init__(self, title=”Demonstration of PyObject GUI Application Creation”): In this we are setting the title to the Window.
  7. Gtk.Window.set_default_size(self, 400,325): In this, we are setting up window width and width.
  8. Gtk.Window.set_position(self, Gtk.WindowPosition.CENTER): In this line of code, we are setting up the default position of the window as CENTER.
  9. button1 = Gtk.Button(“GeeksforGeeks”): In this we are creating a button named GeeksforGeeks.
  10. button1.connect(“clicked”, self.whenbutton1_clicked): In this, we are activating the event clicked when the button is been pressed.
  11. self.add(button1): In this, we are adding a button on the created window.
  12. def whenbutton1_clicked(self, button): In this, we are defining the function named whenbutton1_clicked and writing the event code into it.
  13. print(“GeeksforGeeks”): We are printing the GeeksforGeeks message.
  14. window = ourwindow(): In this, we are creating a global variable which we can use later in the code.
  15. window.connect(“delete-event”, Gtk.main_quit): In this, we are deleting all the widgets after we close our program window automatically.
  16. window.show_all(): Showing the window.
  17. Gtk.main(): Running the Gtk library.

Step 3: Change the permissions of test.py file by using the following command

sudo chmod 755 test.py

How to Create GUI Applications Under Linux Desktop Using PyGObject

Step 4: Run the test.py file by using the following command.

./test.py or
python3 test.py

How to Create GUI Applications Under Linux Desktop Using PyGObject

Step 5: Now, you can see that the application window have been displayed and the button is been visible on the screen. So after clicking on the “GeeksforGeeks” button. The message is been printed on the terminal.

How to Create GUI Applications Under Linux Desktop Using PyGObject

So, in this way, we can create GUI applications directly from the code itself.

Method 2: Glade Designer Method

In this method, we will be creating the GUI components by using Glade Designer which provides the drag and drops facility of window components and also controls components like buttons, labels etc. So follow the below steps to create an application using Glade Designer method.

Step 1: Install the Glade package on a Linux environment by using the following command.

sudo apt-get install glade

How to Create GUI Applications Under Linux Desktop Using PyGObject

Step 2: Launch the Glade Designer application by terminal itself or by navigating through all apps on Linux applications.

glade

How to Create GUI Applications Under Linux Desktop Using PyGObject

Step 3: The following screenshot displays the GUI of Glade application.

Step 4: Now, we will be creating the Window on which control objects are been placed. So, select the Window(GtkWindow) from Top Levels option.

How to Create GUI Applications Under Linux Desktop Using PyGObject

Step 5: Now, place the button on the Window from Control option.

How to Create GUI Applications Under Linux Desktop Using PyGObject

Step 6: Make sure to click the clicked option in signals tab and give the name to the handler as button1_clicked.

Step 7: Click on the Save option and save the file as myprogram.glade.

Step 8: Now, create a new python file by using a text editor and adding the below line of code into it. Make sure to add the file name of .glade file in program code.

nano glademethod.py

Python3




#!/usr/bin/python
# -*- coding: utf-8 -*-
  
from gi.repository import Gtk
  
class Handler:
    def button_1clicked(self, button):
      print("Hello GeeksForGeeks using Glade")
  
builder = Gtk.Builder()
builder.add_from_file("myprogram.glade")
builder.connect_signals(Handler())
  
ournewbutton = builder.get_object("button1")
ournewbutton.set_label("Demo using Glade!")
  
window = builder.get_object("window1")
  
window.connect("delete-event", Gtk.main_quit)
window.show_all()
Gtk.main()


Code Explanation:

  1. #!/usr/bin/python3: This line specify the default Python3 interpreter path.
  2. # -*- coding: utf-8 -*-: In this line of code, we have specified the default Unicode as UTF-8.
  3. from gi.repository import Gtk: Here, we are importing the GTK 3 library.
  4. class Handler: In this line, we are creating a new class named Handler.
  5. def button1_clicked(self, button): In this, we are defining the function named button1_clicked and writing the event code into it.
  6. print(“Hello GeeksForGeeks using Glade”): We are printing the Hello GeeksForGeeks using Glade message.
  7. builder = Gtk.Builder(): We are creating a global variable builder which is used to import the glade file.
  8. builder.add_from_file(“myprogram.glade”): Here we’re importing the “myprogram.glade” file to use it as a default GUI for our program.
  9. builder.connect_signals(Handler()): In this we are connecting glade file with handler class.
  10. ournewbutton.set_label(“Hello, World!”): We are setting the default button text as Demo using Glade!
  11. window = builder.get_object(“window1”): In this line we have called the “window1” object from the .glade file in order to show it later in the program.
  12. window.connect(“delete-event”, Gtk.main_quit): In this, we are deleting all the widgets after we close our program window automatically.
  13. window.show_all(): Showing the window.
  14. Gtk.main(): Running the Gtk library.

How to Create GUI Applications Under Linux Desktop Using PyGObject

Step 9: Change the permissions of glademethod.py file by using the following command.

sudo chmod 755 glademethod.py

How to Create GUI Applications Under Linux Desktop Using PyGObject

Step 10: Run the glademethod.py file by using the following command. You will see the Window opened and the created button will be displayed.

python3 glademethod.py

Step 11: After clicking on the button on the window, the message is been displayed on the terminal.

How to Create GUI Applications Under Linux Desktop Using PyGObject

So, in this way we can create GUI applications using Glade Designer method only be dragging and dropping the components.



Last Updated : 20 Feb, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads