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 w3wiki 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 w3wiki using Glade”): We are printing the Hello w3wiki 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.

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.



How to Create GUI Applications Under Linux Desktop Using PyGObject

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

Similar Reads

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

Method 2: Glade Designer Method

...