wx.ComboBox SetValue() automatically selects values from the list.

Hi,

When I call wx.ComboBox.SetValue() with a string, it selects one from its
predefined list if my string PARTIALLY matches with one of the predefined
strings:

self.MyComboBox = wx.ComboBox(self, -1, choices=['10','20'],
style=wx.CB_DROPDOWN)
...

self.MyComboBox.SetValue('1')
<<<

Then the combobox displays '10' rather than '1'. However if I set '1.' for
example, then it displays '1.'.

Is this feature designed?

···

--
View this message in context: http://wxpython-users.1045709.n5.nabble.com/wx-ComboBox-SetValue-automatically-selects-values-from-the-list-tp5723426.html
Sent from the wxPython-users mailing list archive at Nabble.com.

I could be wrong, but I think that is the intention. My first thought is to make the ComboBox READ-ONLY with the style CB_READONLY then see if you don’t get that behaviour.

···

On Monday, February 2, 2015 at 8:59:25 AM UTC-8, sungjune.lee wrote:

Then the combobox displays ‘10’ rather than ‘1’. However if I set ‘1.’ for

example, then it displays ‘1.’.

Is this feature designed?

Thanks for the reply.

But my intention was to allow users to choose any numbers. Actual problem
goes like this:

- User enters number '1' in the combobox
- Program reads the user input and save the number '1' in a file.
- Later when user run the program again, he will see '10' instead of '1'
because python will try

   MyComboBox.SetValue('1')

- But if he choose '1.' instead then he will see '1.'

···

--
View this message in context: http://wxpython-users.1045709.n5.nabble.com/wx-ComboBox-SetValue-automatically-selects-values-from-the-list-tp5723426p5723433.html
Sent from the wxPython-users mailing list archive at Nabble.com.

Are you sure the "10" being displayed is a result of your SetValue? It may
be just
left over from the button setup when the run starts because you say the "10"
appears
the next time the user runs the program. And "10" is index 0 of your
choices.

Or are you looking at the field before the screen has a chance to update,
perhaps?

···

--
View this message in context: http://wxpython-users.1045709.n5.nabble.com/wx-ComboBox-SetValue-automatically-selects-values-from-the-list-tp5723426p5723435.html
Sent from the wxPython-users mailing list archive at Nabble.com.

I've tested with following piece of code:

···

==============================
class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.combobox = wx.ComboBox(self, wx.ID_ANY, '',
            choices=['10','20','30'], style=wx.CB_DROPDOWN)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.combobox, 0, 0, 0)
        self.SetSizer(sizer)
        sizer.Fit(self)
        self.Layout()

        self.combobox.SetValue('1')

if __name__ == "__main__":
    gettext.install("app")

    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    frame = MyFrame(None, wx.ID_ANY, "")
    app.SetTopWindow(frame)
    frame.Show()
    app.MainLoop()

Interesting thing is that in my Lunux machine it shows '1' but in my Windows
it shows '10'. Both have the same version of wx (2.8.12.1).

--
View this message in context: http://wxpython-users.1045709.n5.nabble.com/wx-ComboBox-SetValue-automatically-selects-values-from-the-list-tp5723426p5723449.html
Sent from the wxPython-users mailing list archive at Nabble.com.

A simple workaround would be:

make sure the index returned is at least 0

if self.combobox.FindString(‘1’)>=0:

self.combobox.SetValue('1')  
···

On Tuesday, February 3, 2015 at 5:47:23 PM UTC-8, sungjune.lee wrote:

I’ve tested with following piece of code:

==============================

class MyFrame(wx.Frame):

def __init__(self, *args, **kwds):

    kwds["style"] = wx.DEFAULT_FRAME_STYLE

    wx.Frame.__init__(self, *args, **kwds)

    self.combobox = wx.ComboBox(self, wx.ID_ANY, '',

        choices=['10','20','30'], style=wx.CB_DROPDOWN)



    sizer = wx.BoxSizer(wx.VERTICAL)

    sizer.Add(self.combobox, 0, 0, 0)

    self.SetSizer(sizer)

    sizer.Fit(self)

    self.Layout()



    self.combobox.SetValue('1')

For those who may suffer from the same issue, my quick and dirty fix is
adding a space character at the end.

···

==========

self.MyComboBox.SetValue(str(some_integer_value) + " ")

==========

--
View this message in context: http://wxpython-users.1045709.n5.nabble.com/wx-ComboBox-SetValue-automatically-selects-values-from-the-list-tp5723426p5723460.html
Sent from the wxPython-users mailing list archive at Nabble.com.