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.

%MINIFYHTML916eca1f069a1b3be98b6b8196b062b943%
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

FacebookLike
InstagramFollow
PinterestFollow
RSS FeedSubscribe
Sound CloudFollow
TwitterFollow
YoutubeSubscribe

Latest Articles

The Complete List of Dhatus – Sanskrit Root Words and their English meaning

A complete list of all dhatus (root words or verb roots) in Sanskrit dictionary and their meanings with corresponding IPA transliteration.

Sanskrit Lesson 4 – Word Creation Magic using Dhatus

Learn how easy it is to create new words in Sanskrit using root words called Dhatus. This simple process of Sanskrit grammar powers the entire language.

The Secret Role of Karma in Mahabharata

The seeds of Mahabharata were sown long back Duryodhana or Dhritarashtra were born. The story of Kaurvas and Pandavas is only the second half of the cycle of Karma. So what is the first half?

The Real Super Food is free and all around you

Are you looking for some super food that can keep you healthy and strong forever? Do you want to stay free from all illness and health issues?

Latest Music Notations

Background Theme (BGM) – Mouna Ragam – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Background Theme (BGM) from the 1986 Tamil movie Mouna Ragam in Western and Indian Notations.

Dil Kya Kare Jab Kisi Se – Julie – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Dil Kya Kare Jab Kisi Se from the 1975 Hindi movie Julie in Western and Indian Notations.

Albela Sajan Aayo Re – Hum Dil De Chuke Sanam – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Albela Sajan Aayo Re from the 1999 Hindi movie Hum Dil De Chuke Sanam in Western and Indian Notations.

Chinni Chinni Aasa Chinnadaani Asa – Roja – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Chinni Chinni Aasa Chinnadaani Asa from the 1992 Telugu movie Roja in Western and Indian Notations.
Content Protection by DMCA.com