#-----------------------------------------------------------------------------
# Name: GridCombo.py
# Purpose: Dynamic list updating with a wx.grid.GridCellChoiceEditor
#
# Author: Thomas M Wetherbee
#
# Created: 2009/04/27
# RCS-ID: $Id: GridCombo.py $
# Copyright: (c) 2009
# Licence: Distributed under the terms of the GNU General Public License
#-----------------------------------------------------------------------------
#!/usr/bin/env python
‘‘‘
Dynamic list updating with a wx.grid.GridCellChoiceEditor.
This example shows how to dynamically update the choices in a
GridCellChoiceEditor. This simple example creates a two column
grid where the top row in each column is a wx.grid.GridCellChoiceEditor.
The choices listed in the editor are created on the fly, and may change
with each selection. Text entered into the GridCellChoiceEditor cell
is appended as an additional choice.
In addition to appending new choices, this example also shows how to get
the selection index and client data from the choice.
Cell editor interactions are printed for every step.
This example is deliberately simple, lacking sizers and other useful but
confusing niceties.
Theory:
The GridCellChoiceEditor uses an underlying ComboBox to do the editing.
This underlying ComboBox is created when the cell editor is created. Normally
the ComboBox is completely hidden, but in this example we retrieve a reference
to the ComboBox and use it to load choices and retrieve index and client data.
The example starts with a GridCellChoiceEditor attached to the two top cells of
the grid. When the GridCellChoiceEditor is invoked for the first time, two
choice items are added to the choice list along with their associated user
data. The items are (‘spam‘, 42) and (‘eggs‘, 69), where spam is the text to
display and 42 is the associated client data. In this example ‘spam‘ has an
index of 0 while eggs, being the second item of the list, has an index of 1.
Note that the index and user data are not required. The demonstrated method
works fine without either, but sometimes it is useful to know the index of a
selection, especially when the user is allowed to create choices. For example,
we might have the list [‘spam‘, ‘eggs‘, ‘spam‘, ‘spam‘] where the three spam
items are different objects. In this case simply returning the item value
‘spam‘ is ambiguous. We need to know the index, or perhaps some associated
client data.
In our example, when the user enters a new choice, the choice is appended to
the end of the choice list. A unique integer number is created for each new
choice, in succession, with the first number being 100. This number is used
for client data.
In this example we bind directly to the ComboBox events, rather than getting
the events through the frame. This is done to keep the grid from eating the
events. The difference in binding can be seen in the two binding methods: