How to Create GUI Applications Under Linux Desktop Using PyGObject
Last Updated :
20 Feb, 2022
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
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:
- #!/usr/bin/python3: This line specify the default Python3 interpreter path.
- # -*- coding: utf-8 -*-: In this line of code, we have specified the default Unicode as UTF-8.
- from gi.repository import Gtk: Here, we are importing the GTK 3 library.
- Class ourwindow(Gtk.Window): In this, we are creating a class named as ourWindow and setting the class object type as Gtk.Window.
- def __init__(self): In this, we are defining the main window components.
- Gtk.Window.__init__(self, title="Demonstration of PyObject GUI Application Creation"): In this we are setting the title to the Window.
- Gtk.Window.set_default_size(self, 400,325): In this, we are setting up window width and width.
- 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.
- button1 = Gtk.Button("GeeksforGeeks"): In this we are creating a button named GeeksforGeeks.
- button1.connect(“clicked”, self.whenbutton1_clicked): In this, we are activating the event clicked when the button is been pressed.
- self.add(button1): In this, we are adding a button on the created window.
- def whenbutton1_clicked(self, button): In this, we are defining the function named whenbutton1_clicked and writing the event code into it.
- print("GeeksforGeeks"): We are printing the GeeksforGeeks message.
- window = ourwindow(): In this, we are creating a global variable which we can use later in the code.
- window.connect(“delete-event”, Gtk.main_quit): In this, we are deleting all the widgets after we close our program window automatically.
- window.show_all(): Showing the window.
- 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
Step 4: Run the test.py file by using the following command.
./test.py or
python3 test.py
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.
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
Step 2: Launch the Glade Designer application by terminal itself or by navigating through all apps on Linux applications.
glade
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.
Step 5: Now, place the button on the Window from Control option.
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:
- #!/usr/bin/python3: This line specify the default Python3 interpreter path.
- # -*- coding: utf-8 -*-: In this line of code, we have specified the default Unicode as UTF-8.
- from gi.repository import Gtk: Here, we are importing the GTK 3 library.
- class Handler: In this line, we are creating a new class named Handler.
- def button1_clicked(self, button): In this, we are defining the function named button1_clicked and writing the event code into it.
- print("Hello GeeksForGeeks using Glade"): We are printing the Hello GeeksForGeeks using Glade message.
- builder = Gtk.Builder(): We are creating a global variable builder which is used to import the glade file.
- builder.add_from_file(“myprogram.glade”): Here we’re importing the “myprogram.glade” file to use it as a default GUI for our program.
- builder.connect_signals(Handler()): In this we are connecting glade file with handler class.
- ournewbutton.set_label(“Hello, World!”): We are setting the default button text as Demo using Glade!
- 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.
- window.connect(“delete-event”, Gtk.main_quit): In this, we are deleting all the widgets after we close our program window automatically.
- window.show_all(): Showing the window.
- Gtk.main(): Running the Gtk library.
Step 9: Change the permissions of glademethod.py file by using the following command.
sudo chmod 755 glademethod.py
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.
So, in this way we can create GUI applications using Glade Designer method only be dragging and dropping the components.
Similar Reads
Create More Advance GUI Applications Using PyGobject Tool in Linux
PyGObject is a valuable tool for creating advanced GUI applications in Linux. It allows developers to create visually appealing and user-friendly interfaces while leveraging the power of the GObject system to manage memory and other resources efficiently. What is GUI in Linux? A graphical user inter
4 min read
Create First GUI Application using Python-Tkinter
We are now stepping into making applications with graphical elements, we will learn how to make cool apps and focus more on its GUI(Graphical User Interface) using Tkinter.What is Tkinter?Tkinter is a Python Package for creating GUI applications. Python has a lot of GUI frameworks, but Tkinter is th
12 min read
GUI chat application using Tkinter in Python
Chatbots are computer program that allows user to interact using input methods. The Application of chatbots is to interact with customers in big enterprises or companies and resolve their queries. Â Chatbots are mainly built for answering standard FAQs. The benefit of this type of system is that cust
7 min read
How to Run GUI Based Applications inside Docker?
A Docker Container is an isolated application platform that contains everything needed to run an application built from one or more images. Docker is an Open Source project that provides an open platform to run any number of applications inside a container according to your requirements and you can
7 min read
How to create GUI in C programming using GTK Toolkit
Introduction to GTK Many programming languages bolster GUI improvement as one of the centrepieces of its language highlights. C has no such library connected to it like the string library, IO library, etc, that we every now and again use. This weakness opened the skyline for engineers to pick from a
7 min read
PyQt in Python : Designing GUI applications
Building GUI applications using the PYQT designer tool is comparatively less time-consuming than coding the widgets. It is one of the fastest and easiest ways to create GUIs. The normal approach is to write the code even for the widgets and for the functionalities as well. But using Qt-designer, one
5 min read
Creating Your First Application in Python
Python is one of the simplest programming language out there. In fact, it was developed for the sole purpose of simplifying the process of learning a programming language and exposed beginners to the concepts of Programming. In this article, we will be building a Python Application. Not to worry it
5 min read
Simple Chatbot application using Python, GoogleAPIKey
Google-GenerativeAI is Google AI Python SDK. It uses Gemini to build AI-powered features. In this article, we will see how to create a Simple Chatbot application using Python GoogleAPIKey. What is Google-GenerativeAI?Google-GenerativeAI is nothing but Google AI Python SDK. It enables to use of Gemin
4 min read
Create Copy-Move GUI using Tkinter in Python
Everyone reading this post is well aware of the importance of Copying the file or moving the file from one specific location to another. In this post, we have tried to explain not only the program but added some exciting pieces of Interface. Up to now, many of you may get about what we are talking a
4 min read
Build an Application to Search Installed Application using Python
In this article, we are going to write python scripts to search for an installed application on Windows and bind it with the GUI application. We are using winapps modules for managing installed applications on Windows. Prerequisite - Tkinter in Python To install the module, run this command in your
6 min read