Skip to content

Instantly share code, notes, and snippets.

@sulincix
Created December 2, 2024 12:56
Show Gist options
  • Select an option

  • Save sulincix/446c6db7c3b3270db5a2cb3bc24a625f to your computer and use it in GitHub Desktop.

Select an option

Save sulincix/446c6db7c3b3270db5a2cb3bc24a625f to your computer and use it in GitHub Desktop.
Automatically define all glade objects by id
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
import xml.etree.ElementTree as ET
def glade_loader(ctx, glade):
builder = Gtk.Builder()
builder.add_from_file(glade)
# Parse the Glade XML file
tree = ET.parse(glade)
root = tree.getroot()
# Find all objects and print their IDs
for obj in root.findall('.//object'):
obj_id = obj.get('id')
if obj_id:
exec("ctx."+ obj_id +" = builder.get_object(\""+obj_id+"\")")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment