Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add combobox in glade with python and GObject

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.

like image 763
Daniel Dudola Avatar asked Nov 18 '25 05:11

Daniel Dudola


1 Answers

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:

  1. Add a Combobox to some place in the GUI.
  2. Create a ListStore that will hold the data. Configure the Liststore to whatever columns you need to have (each column has a type).
  3. Return to the combobox, set the previous created Liststore as it's model.
  4. Edit the combobox (right click, edit) and add a cell renderer. Map that cell renderer to display data from some column of the model.
  5. If your data is static, within Glade you can add rows to your ListStore. If your data is dynamic you will need to get the liststore in your code and then fill it with list that have the same type of elements as your liststore.

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

like image 85
Havok Avatar answered Nov 19 '25 18:11

Havok



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!