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 GtkCode language: Python (python)

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)Code language: Python (python)

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)Code language: Python (python)

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)Code language: Python (python)

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

Download HitXP Mobile App

Get it on Google Play
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

FacebookLike
InstagramFollow
PinterestFollow
RSS FeedSubscribe
Sound CloudFollow
TwitterFollow
YoutubeSubscribe

Latest Articles

The Power of Democracy: How Free Societies Foster Innovation in Science and Technology

Democracy allows freedom of speech and thought, boosting creativity and innovation. This leads to scientific breakthroughs and technological advancements.

How does GOD helps us?

Do prayers really help? Do miracles happen when we are in trouble? Does God really help us in times of need? Are prayers enough to save us during bad times? How should we face problems?

Sanskrit poem on Ramayana – read in reverse becomes Mahabharata!

A Sanskrit poem about Ramayana when read in reverse direction becomes a poem on Mahabharata!

The difference between Itihasa and Puranas

Documentation of ancient Indian history and the historical events of Indian civilization in the form of Itihasa and Puranas - Ramayana and Mahabharata.

Latest Music Notations

Jai Shri Ram – Adipurush – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Jai Shri Ram from the 2023 Hindi movie Adipurush in Western and Indian Notations.

Sojugada Soojumallige – Garuda Gamana Vrishabha Vahana – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Sojugada Soojumallige from the 2021 Kannada movie Garuda Gamana Vrishabha Vahana in Western and Indian Notations.

Oo Antava Mava – Pushpa – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Oo Antava Mava from the 2022 Telugu movie Pushpa in Western and Indian Notations.

Kaa Chalige Bangalore (Tiningaa Miningaa Tishaaaa) – Salaga – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Tiningaa Miningaa Tishaaaa (Kaa Chali Ge) from the 2022 Kannada movie Salaga in Western and Indian Notations.
Content Protection by DMCA.com