Adding Dropdowns and spin buttons


Figure 1: Adding dropdown & spin widgets

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.


Figure 2: Adding a list store


Figure 3: Adding a cell renderer


Figure 4: Adding a list store


Figure 5: Adding a combobox list store

#!/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()

	#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

	#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 :)

    def dropdown_event_text(self, widget):

    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()