[Apologies in advance if quoting is mangled - GroupWise is not my
friend.]
godoy@ieee.org 05/04/04 22:16 PM >>>
Is there any widget that given 'dictionary_of_interest' shows the
values and
when clicked can give back the key for the corresponding
'event.GetStringSelection()'? If I click on 'ValueA' on the requested
widget I'd get back '1'.
To my knowledge there isn't a widget that handles all of this for you
automatically. However, associating a key, or other data with each
selection & retrieving it when an item is selected is quite doable.
See:
http://www.wxwidgets.org/manuals/2.5.1/wx_wxcontrolwithitems.html#wxcontrolwithitems
and
http://wiki.wxpython.org/index.cgi/ManipulatingComboBoxItems
That info should get you up to speed. Basically, a number of controls
inherit from wxControlWithItems, which has methods for setting and
retrieving data per item. So instead of just giving a widget a
dictionary, you'd set this client data (in your case database record
keys perhaps) on a per item basis in a loop, as you populate the string
items of a combobox, listbox, etc.
To my knowledge there isn't a widget that handles all of this for you
automatically. However, associating a key, or other data with each
selection & retrieving it when an item is selected is quite doable.
Hmmm... I was getting to the same conclusion as you: there's no such object
ready to use.
See:
http://www.wxwidgets.org/manuals/2.5.1/wx_wxcontrolwithitems.html#wxcontrolwithitems
and
http://wiki.wxpython.org/index.cgi/ManipulatingComboBoxItems
Thanks, but I don't see the wiki details while using wxPython 2.4.2.4... Is
wx.ItemContainer something new on 2.5?
That info should get you up to speed. Basically, a number of controls
inherit from wxControlWithItems, which has methods for setting and
retrieving data per item. So instead of just giving a widget a
dictionary, you'd set this client data (in your case database record
keys perhaps) on a per item basis in a loop, as you populate the string
items of a combobox, listbox, etc.
This would simplify a lot of things... 
I just need to find it for wxPython 2.4...
···
On Qua 05 Mai 2004 03:09, Brian Almond wrote:
--
Godoy. <godoy@ieee.org>
Hi Jorge,
Jorge Godoy wrote:
To my knowledge there isn't a widget that handles all of this for you
automatically. However, associating a key, or other data with each
selection & retrieving it when an item is selected is quite doable.
Hmmm... I was getting to the same conclusion as you: there's no such object
ready to use.
See:
http://www.wxwidgets.org/manuals/2.5.1/wx_wxcontrolwithitems.html#wxcontrolwithitems
and
http://wiki.wxpython.org/index.cgi/ManipulatingComboBoxItems
Thanks, but I don't see the wiki details while using wxPython 2.4.2.4... Is
wx.ItemContainer something new on 2.5?
That info should get you up to speed. Basically, a number of controls
inherit from wxControlWithItems, which has methods for setting and
retrieving data per item. So instead of just giving a widget a
dictionary, you'd set this client data (in your case database record
keys perhaps) on a per item basis in a loop, as you populate the string
items of a combobox, listbox, etc.
This would simplify a lot of things... 
I just need to find it for wxPython 2.4...
I use the following to htis on a combobox with wxPython 2.4:
def LoadChoicesToCB(self, items):
self.Clear() # Clear existing choices
self.Append('', 0) # insert a blank entry - to allow reset to blank/null
for item in items:
appendString = getattr(item, self.dbCol)
appendClientData = getattr(item, self.dbPK)
self.Append(appendString, appendClientData)
See also wxComboBox.SetClientData and wxComboBox.GetClientData
See you
Werner
···
On Qua 05 Mai 2004 03:09, Brian Almond wrote:
Hi Werner!
I use the following to htis on a combobox with wxPython 2.4:
def LoadChoicesToCB(self, items):
self.Clear() # Clear existing choices
self.Append('', 0) # insert a blank entry - to allow reset to
blank/null
for item in items:
appendString = getattr(item, self.dbCol)
appendClientData = getattr(item, self.dbPK)
self.Append(appendString, appendClientData)
See also wxComboBox.SetClientData and wxComboBox.GetClientData
This is very interesting, without messing with anything else, just using the
same Append method...
I must confess that although I have read the docs, I haven't tried that yet.
It's interesting how simple things are simple
I can even use the
dictionary approach I had on mind...
And I was already looking at SetClientData / GetClientData, which would
solve the problem another way, but I think this is simpler and I just need
to 'GetSelection' instead of 'GetStringSelection' to get the 'PK' back.
Thanks! I'll be trying it right now.
Be seeing you,
···
On Qua 05 Mai 2004 04:55, Werner F. Bruhin wrote:
--
Godoy. <godoy@ieee.org>
Hmmm... It solves the problem when there are no holes in the PKs. If there
are holes, then when using the GetSelection method you get an incorrect
value, since the position on the widget isn't associated with the same
'number' as the index on the dictionary/database.
With SetClientData and GetClientData it works.
So, I ended up with something like:
···
On Qua 05 Mai 2004 07:20, Jorge Godoy wrote:
Hi Werner!
On Qua 05 Mai 2004 04:55, Werner F. Bruhin wrote:
I use the following to htis on a combobox with wxPython 2.4:
def LoadChoicesToCB(self, items):
self.Clear() # Clear existing choices
self.Append('', 0) # insert a blank entry - to allow reset to
blank/null
for item in items:
appendString = getattr(item, self.dbCol)
appendClientData = getattr(item, self.dbPK)
self.Append(appendString, appendClientData)
See also wxComboBox.SetClientData and wxComboBox.GetClientData
This is very interesting, without messing with anything else, just using
the same Append method...
I must confess that although I have read the docs, I haven't tried that
yet. It's interesting how simple things are simple
I can even use the
dictionary approach I had on mind...
===============================================================================
for i in item:
self.choiceSomeChoice.Append(item[i], i)
self.choiceSomeChoice.SetClientData(self.choiceSomeChoice.GetCount()-1,i)
to setup the ChoiceBox (or Combo or...) and with something like:
===============================================================================
index =
self.choiceSomeChoice.GetClientData(self.choiceSomeChoice.GetSelection())
to recover the index number correctly.
If we're talking about ComboBoxes, it's possible to replace the GetCount
method by the GetLastPosition method, though the result is the same (if you
append to the end of the list, of course... SetClientData works for any
case here).
As I said, this method works with contiguous and non-contiguous sequences
while the one proposed by Werner works with contiguous only. As I see, the
SetClientData / GetClientData is more robust than the append only solution.
Thanks to everybody for the hints.
Be seeing you,
--
Godoy. <godoy@ieee.org>
Hi Jorge,
Jorge Godoy wrote:
Hi Werner!
I use the following to htis on a combobox with wxPython 2.4:
def LoadChoicesToCB(self, items):
self.Clear() # Clear existing choices
self.Append('', 0) # insert a blank entry - to allow reset to
blank/null
for item in items:
appendString = getattr(item, self.dbCol)
appendClientData = getattr(item, self.dbPK)
self.Append(appendString, appendClientData)
See also wxComboBox.SetClientData and wxComboBox.GetClientData
This is very interesting, without messing with anything else, just using
the same Append method...
I must confess that although I have read the docs, I haven't tried that
yet. It's interesting how simple things are simple
I can even use the
dictionary approach I had on mind...
Hmmm... It solves the problem when there are no holes in the PKs. If there
are holes, then when using the GetSelection method you get an incorrect
value, since the position on the widget isn't associated with the same
'number' as the index on the dictionary/database.
I am not sure I understand what your problem is. My PK's are NOT consecutive, there are definitely holes here and there.
I use Append to load the choices and then GetSelection and GetClientData. To access your dictionary you would have to do a GetClientData and use its result to access your dictionary, or am I missing something?
···
On Qua 05 Mai 2004 07:20, Jorge Godoy wrote:
On Qua 05 Mai 2004 04:55, Werner F. Bruhin wrote:
With SetClientData and GetClientData it works.
So, I ended up with something like:
===============================================================================
for i in item:
self.choiceSomeChoice.Append(item[i], i)
self.choiceSomeChoice.SetClientData(self.choiceSomeChoice.GetCount()-1,i)
to setup the ChoiceBox (or Combo or...) and with something like:
===============================================================================
index =
self.choiceSomeChoice.GetClientData(self.choiceSomeChoice.GetSelection())
to recover the index number correctly.
If we're talking about ComboBoxes, it's possible to replace the GetCount
method by the GetLastPosition method, though the result is the same (if you
append to the end of the list, of course... SetClientData works for any
case here).
As I said, this method works with contiguous and non-contiguous sequences
while the one proposed by Werner works with contiguous only. As I see, the
SetClientData / GetClientData is more robust than the append only solution.
Thanks to everybody for the hints.
Be seeing you,
Maybe I am doing something wrong
In fact, I was... And I know what it
was. While writing this message, I found out.
Let me write a little more.
Suppose I have:
pk_code integer (unique --- it's a PK!)
description string (unique)
notes string
And suppose I have both the pk_code and the description as ComboBoxes, in
such a way that the user can get the code directly from the Combo (GetValue
has to be used...) --- and here was my mistake: I have relied on the code
directly, not on the ClientData stuff --- and the other combo should have
its value filled in by an SQL search or something like that. The same
happens for notes on a EVT_COMBOBOX or EVT_TEXT_ENTER.
If I'm filling two comboboxes one with the code and the other with the
description but I'm allowing the user to use any of these to search for the
notes (after all, the human being works better with words than with
numbers ;-)), how would it be better to do it?
Did I make myself clearer? Or I made it worse?
See you,
···
On Qua 05 Mai 2004 15:18, Werner F. Bruhin wrote:
I am not sure I understand what your problem is. My PK's are NOT
consecutive, there are definitely holes here and there.
I use Append to load the choices and then GetSelection and
GetClientData. To access your dictionary you would have to do a
GetClientData and use its result to access your dictionary, or am I
missing something?
--
Godoy. <godoy@ieee.org>