Skip to content

Instantly share code, notes, and snippets.

@thorsummoner
Created August 7, 2014 05:52
Show Gist options
  • Save thorsummoner/60cf609391b24c70364c to your computer and use it in GitHub Desktop.
Save thorsummoner/60cf609391b24c70364c to your computer and use it in GitHub Desktop.
PyGtk3 Populate ComboBox from Glade in Python
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<!-- interface-requires gtk+ 3.0 -->
<object class="GtkListStore" id="myliststore">
<columns>
<!-- column-name code -->
<column type="gchararray"/>
<!-- column-name legible -->
<column type="gchararray"/>
</columns>
</object>
<object class="GtkWindow" id="window">
<property name="can_focus">False</property>
<property name="window_position">center-always</property>
<property name="default_width">400</property>
<signal name="destroy" handler="main_quit" swapped="no"/>
<child>
<object class="GtkBox" id="box">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkLabel" id="label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Best color in the world:</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkComboBox" id="mycombobox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="model">myliststore</property>
<signal name="changed" handler="combobox_changed" swapped="no"/>
<child>
<object class="GtkCellRendererText" id="renderer"/>
<attributes>
<attribute name="text">1</attribute>
</attributes>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
# A big thank you to Havoc
# http://stackoverflow.com/a/10527393/1695680
from gi.repository import Gtk
from os.path import abspath, dirname, join
WHERE_AM_I = abspath(dirname(__file__))
class MyApp(object):
def __init__(self):
# Build GUI
self.builder = Gtk.Builder()
self.glade_file = join(WHERE_AM_I, 'test.glade')
self.builder.add_from_file(self.glade_file)
# Get objects
go = self.builder.get_object
self.window = go('window')
self.myliststore = go('myliststore')
self.mycombobox = go('mycombobox')
# Initialize interface
colors = [
['#8C1700', 'Redish'],
['#008C24', 'Greenish'],
['#6B6BEE', 'Blueish'],
]
for c in colors:
self.myliststore.append(c)
self.mycombobox.set_active(0)
# Connect signals
self.builder.connect_signals(self)
# Everything is ready
self.window.show()
def main_quit(self, widget):
Gtk.main_quit()
def combobox_changed(self, widget, data=None):
model = widget.get_model()
active = widget.get_active()
if active >= 0:
code = model[active][0]
print('The code of the selected color is {}'.format(code))
else:
print('No color selected')
if __name__ == '__main__':
try:
gui = MyApp()
Gtk.main()
except KeyboardInterrupt:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment