#+TITLE: Adding Dropdowns and spin buttons #+DATE: 2014-01-15 12:00:00 UTC #+DESCRIPTION: GTK example of some combo / slecet drop down boxesfor user interaction. #+FILETAGS: GTK-3:python:widgets #+CATEGORY: python #+SLUG: 04-adding-dropdowns-and-spin-buttons #+THUMBNAIL:../../../images/gtk/tut04-text-dropdown-spin.png #+BEGIN_COMMENT .. title: Adding Dropdowns and spin buttons .. slug: 04-adding-dropdowns-and-spin-buttons .. date: 2014-01-15 12:00:00 UTC .. tags: GTK-3, python, widgets .. category: python .. description: GTK example of some combo / slecet drop down boxesfor user interaction. .. type: text #+END_COMMENT #+CAPTION: Adding dropdown & spin widgets [[../../../images/gtk/tut04-text-dropdown-spin.png]] This example demonstrates using some drop down boxes and adding new items and retrieving the selected values. This snippet also demonstrates the use of spin buttons for selecting a value from a range. When creating a combobbox in glade make sure you add a liststore to the widget, and also edit the combobox properties in a seperate window so you can access and the hierarchy menu and assign a cell render to the column in your listview. #+CAPTION: Adding a list store [[../../../images/gtk/add-list-store.png]] #+CAPTION: Adding a cell renderer [[../../../images/gtk/attach-cell-renderer-set-text-column.png]] #+CAPTION: Adding a list store [[../../../images/gtk/attach-liststore-model.png]] #+CAPTION: Adding a combobox list store [[../../../images/gtk/edit-combobox-liststore.png]] #+BEGIN_SRC python #!/usr/bin/env python from gi.repository import Gtk class application_gui: """Tutorial 04 text input, spin input, drop down options""" def __init__(self): #load in our glade interface xml = Gtk.Builder() xml.add_from_file('tut04.glade') #grab our widget using get_object this is the name of the widget from glade, window1 is the default name self.window = xml.get_object('window1') self.text = xml.get_object('entry1') #load our widgets from the glade file self.widgets = {} self.widgets['spin1'] = xml.get_object('spinbutton1') self.widgets['spin2'] = xml.get_object('spinbutton2') #simple dropdown just text self.widgets['comboboxtext'] = xml.get_object('comboboxtext1') #more complex multi row multi data types in dropdown using renderers self.widgets['combobox'] = xml.get_object('combobox1') self.widgets['combobox_liststore']= xml.get_object('liststore1') #add some items to the drop down self.widgets['combobox_liststore'].append(['new row test']) self.widgets['combobox_liststore'].append(['second row test']) #adding new rows to the dropdown self.widgets['comboboxtext'].append_text('row4') self.widgets['comboboxtext'].append_text('row5') #connect to events self.widgets['comboboxtext'].connect('changed', self.dropdown_event_text) self.widgets['spin1'].connect('value-changed', self.spin_button_event) self.widgets['spin2'].connect('value-changed', self.spin_button_event) self.widgets['combobox'].connect('changed', self.dropdown_event) self.window.connect('delete_event', Gtk.main_quit) self.window.connect('destroy', lambda quit: Gtk.main_quit()) #show the window else there is nothing to see :) self.window.show_all() def dropdown_event_text(self, widget): self.text.set_text(widget.get_active_text()) def dropdown_event(self, widget): list_view_model = widget.get_model() active_iter_index = widget.get_active() row_iter = list_view_model.get_iter(active_iter_index) self.text.set_text(widget.get_name() + ' ' +list_view_model.get_value(row_iter, 0 )) def spin_button_event(self, widget): self.text.set_text(widget.get_name() + ' ' + ' ' + str(widget.get_value())) application = application_gui() Gtk.main() #+END_SRC