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.