Home Code How to access GTK UI objects in a glade XML file in...

How to access GTK UI objects in a glade XML file in Python?

Glade is a popular visual GUI builder for GTK+. You can use Glade to quickly build the entire user interface of your graphical user interface and then access the UI objects like window, list box, label etc in programming languages like Python to code their behavior in response to GUI events.

So how do you access an UI object, say a list box or a button in a Glade file in Python?

For that, first you need to import GTK into your python code as follows.

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

Next we need to load the glade file into a GTKBuilder. Assuming the name of your Glade file is “MyGUI.glade”, the code snippet to load it in Python would be as follows.

class MyGUIEvents:

    def __init__(self):
        self.builder = Gtk.Builder()
        self.builder.add_from_file("MyGUI.glade")
        self.builder.connect_signals(self)

We create a Python class above to handle all the GUI events of your glade file. That will make it easy to write code by linking events to different methods in the class. The connect_signals call pointing to the class instance itself indicates to the GTK Builder that functions for all events specified in the Glade file can be found in this class MyGUIEvents and should be called accordingly.

Now comes the part where you access individual UI elements like button, label, text box, list box, grid, toolbar, menu bar etc in your Python code. Assuming you have a list box in your Glade file with ID “myListBox1”, the code to access the same in your Python program would be as follows.

self.myListBox1=self.builder.get_object("myListBox1")

#below line not required if you have already set "row-selected" signal in Glade to "onRowSelected"
self.myListBox1.connect("row-selected",self.onRowSelected)

Please note that, you can also skip the call to self.myListBox1.connect if you have already done the linking in the Glade UI itself. You can manually specify function names for UI events in Glade in the “signals” tab.

So if you have already entered “onRowSelected” in Glade UI against the “row-selected” signal, then the line “self.builder.connect_signals(self)” will take care of linking and automatically calling the onRowSelected function without you having to specify it in Python code.

So, the entire code of the Python class to load a Glade file, and then to access a Listbox in it and link an Row Selection event to that list box would be as follows.

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class MyGUIEvents:

    def __init__(self):
        self.builder = Gtk.Builder()
        self.builder.add_from_file("MyGUI.glade")
        self.builder.connect_signals(self)
        self.myListBox1=self.builder.get_object("myListBox1")

       #below line not required if you have already set "row-selected" signal in Glade to "onRowSelected"
       self.myListBox1.connect('row-selected',self.onRowSelected)

    def onRowSelected(self,lstbox,selected_row):
        self.do_something(selected_row)

So now every time a row is selected in the listbox “myListBox1”, the onRowSelected function will be triggered or called and you can process the event accordingly. Note that the row selection event function receives two arguments – the list box and the selected row in that listbox.

Content Protection by DMCA.com
Gurudevhttps://www.hitxp.com
Gurudev is the developer of Gurunudi AI Platform. This is his official website where he pens his thoughts on a wide range of topics, answers queries, shares resources and tools developed by him.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

4,248FansLike
10FollowersFollow
153FollowersFollow
377FollowersFollow

Subscribe to HitXP Articles

To be updated with all the latest articles, offers and special announcements.

Latest Articles

An Efficient Strategy to fight Corona Virus

By setting up local police station level online delivery networks for essential items like grocery, medicines - the need for general public to come out of their homes can be removed.

Rising pollution can cause Pandemics deadlier than Corona Virus

Rising pollution can expose ancient killer microbes that may cause pandemics more deadlier than Corona. Melting Ice Sheets can resurrect killer agents of the past.

Corona Virus, SARS – Why are new viruses increasingly originating from China?

The reason why new strains of deadly viruses are emerging from China. Viruses like coronavirus and SARS should be stopped from evolving and spreading to prevent another global pandemic.

Evolution of life – primitive cells, complex organisms, intelligence, what next after humans?

Evolution of life from primitive life forms to complex multi cellular organisms to intelligent human species - the advent of AI raises a natural question - What next after human?
Content Protection by DMCA.com