Discussion:
Combo box initial value
Andre Polykanine
2014-09-12 17:56:03 UTC
Permalink
Hello everyone,
I'm a newbie, so please bear with me :).
In a dialog box I have a combo box and a text field. I would like to
make so that if one particular value in the combo box is selected, the
text field would be disabled (or hidden), and if another value is
selected, the text field would be enabled.
I have:

self.myCombo = wx.ComboBox(parent=self, choices=['value1', 'value2'], style = wx.CB_READONLY)
self.myCombo.Bind(wx.EVT_COMBOBOX, self.onChange)
# ...

def onChange(self, ev):
self.myTextField.Enable(False) if self.myCombo.GetValue()
!= "value1" else self.myTextField.Enable(True)

And this does work like a charm, the text field gets enabled and
disabled.
However, I would like to have the text field enabled or disabled
depending on the initial value of the combo box, meaning the value
gotten from a config file and selected when the dialog box is open.
I've tried the same:
self.myTextField = wx.TextCtrl(parent=self)
self.myTextField.Enable(False) if self.myCombo.GetValue() != "value1"
else self.myTextField.Enable(True)

but this doesn't work. I've tried GetSelection also, but when logging
this, both GetValue and GetSelection return -1.
Any help would be greatly appreciated.
Thanks!
--
With best regards from Ukraine,
Andre
Skype: Francophile
Twitter: http://twitter.com/m_elensule
Facebook: http://facebook.com/menelion
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Werner
2014-09-17 14:57:17 UTC
Permalink
Hi Andre,
Post by Andre Polykanine
Hello everyone,
I'm a newbie, so please bear with me :).
In a dialog box I have a combo box and a text field. I would like to
make so that if one particular value in the combo box is selected, the
text field would be disabled (or hidden), and if another value is
selected, the text field would be enabled.
self.myCombo = wx.ComboBox(parent=self, choices=['value1', 'value2'], style = wx.CB_READONLY)
self.myCombo.Bind(wx.EVT_COMBOBOX, self.onChange)
# ...
self.myTextField.Enable(False) if self.myCombo.GetValue()
!= "value1" else self.myTextField.Enable(True)
And this does work like a charm, the text field gets enabled and
disabled.
However, I would like to have the text field enabled or disabled
depending on the initial value of the combo box, meaning the value
gotten from a config file and selected when the dialog box is open.
self.myTextField = wx.TextCtrl(parent=self)
self.myTextField.Enable(False) if self.myCombo.GetValue() != "value1"
else self.myTextField.Enable(True)
but this doesn't work. I've tried GetSelection also, but when logging
this, both GetValue and GetSelection return -1.
Any help would be greatly appreciated.
You might need to provide more code, maybe you check myCombo.GetValue to
early, i.e. before it is set?

Werner
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Nathan McCorkle
2014-09-17 16:39:23 UTC
Permalink
Post by Andre Polykanine
Hello everyone,
I'm a newbie, so please bear with me :).
In a dialog box I have a combo box and a text field. I would like to
make so that if one particular value in the combo box is selected, the
text field would be disabled (or hidden), and if another value is
selected, the text field would be enabled.
self.myCombo = wx.ComboBox(parent=self, choices=['value1', 'value2'],
style = wx.CB_READONLY)
self.myCombo.Bind(wx.EVT_COMBOBOX, self.onChange)
# ...
self.myTextField.Enable(False) if self.myCombo.GetValue()
!= "value1" else self.myTextField.Enable(True)
And this does work like a charm, the text field gets enabled and
disabled.
However, I would like to have the text field enabled or disabled
depending on the initial value of the combo box, meaning the value
gotten from a config file and selected when the dialog box is open.
self.myTextField = wx.TextCtrl(parent=self)
self.myTextField.Enable(False) if self.myCombo.GetValue() != "value1"
else self.myTextField.Enable(True)
but this doesn't work. I've tried GetSelection also, but when logging
this, both GetValue and GetSelection return -1.
Right, because a selection isn't set by default on widget instantiation.
You need to SetSelection first for GetSelection to return non-negative. If
you don't know what index to pass SetSelection, but know the string, then
find the index first with FindString.
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Charles J. Daniels
2014-09-17 19:44:47 UTC
Permalink
Post by Andre Polykanine
Hello everyone,
I'm a newbie, so please bear with me :).
In a dialog box I have a combo box and a text field. I would like to
make so that if one particular value in the combo box is selected, the
text field would be disabled (or hidden), and if another value is
selected, the text field would be enabled.
self.myCombo = wx.ComboBox(parent=self, choices=['value1', 'value2'],
style = wx.CB_READONLY)
self.myCombo.Bind(wx.EVT_COMBOBOX, self.onChange)
# ...
self.myTextField.Enable(False) if self.myCombo.GetValue()
!= "value1" else self.myTextField.Enable(True)
And this does work like a charm, the text field gets enabled and
disabled.
However, I would like to have the text field enabled or disabled
depending on the initial value of the combo box, meaning the value
gotten from a config file and selected when the dialog box is open.
self.myTextField = wx.TextCtrl(parent=self)
self.myTextField.Enable(False) if self.myCombo.GetValue() != "value1"
else self.myTextField.Enable(True)
but this doesn't work. I've tried GetSelection also, but when logging
this, both GetValue and GetSelection return -1.
Any help would be greatly appreciated.
Thanks!
--
With best regards from Ukraine,
Andre
Skype: Francophile
Twitter: http://twitter.com/m_elensule
Facebook: http://facebook.com/menelion
If you're relying on EVT_COMBOX to be called from code that sets a value,
that may be the issue. I've run into a case where programmatically
"setting" stuff didn't trigger events that using the UI would. But also,
comboboxes support typing directly into them, and EVT_COMBOBOX doesn't pick
that up at all, I believe.

How are you setting the value? With a validator? I would image it would be
straightforward to notice the value you read from the config file and
disable based on that value.
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...