I'm trying to apply a combobox to a small GTK3 interface, but I can't really figure out how to populate its list, and how to connect the interface's combobox with my python code.
Could someone show me how in a little example? The rest of it I will be able to finish.
ComboBoxes, like TextViews or TreeViews are widgets that clearly separates the View (what it looks like) from the Model (What info they hold). You need to do in Glade:
The smaller example I could think of is this one:
test.glade
<?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>
test.py
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
Kind regards
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With