Combo box initial value

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

Hi Andre,

···

On 9/12/2014 19:56, Andre Polykanine wrote:

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.

You might need to provide more code, maybe you check myCombo.GetValue to early, i.e. before it is set?

Werner

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.

···

On Wednesday, September 17, 2014 6:50:16 AM UTC-7, Andre Polykanine wrote:

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.

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.

···

On Wednesday, September 17, 2014 6:50:16 AM UTC-7, Andre Polykanine wrote:

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