Search code examples
comboboxwxpython

wxpython return type of masked combo


I have a frustrating problem with wxpython. I've been trying to use the masked ComboBox from wx.lib.masked, but whenever I try to compare its return value with a string, the result is always False, even if the string is the same as the return value from the ComboBox. With the usual ComboBox, everything works as expected. So, what is the problem with the masked ComboBox?

Here's a piece of test code:

import wx
import wx.lib.masked as masked

class TestMasked(wx.Panel):
    def __init__(self, parent):
         wx.Panel.__init__(self, parent, id=wx.ID_ANY)
         choices = ['one', 'two', 'three']
         self.combo_box_masked = masked.Ctrl( self, -1,
                              controlType = masked.controlTypes.COMBO,
                              choices = choices,
                              autoSelect=True
                              )
         self.combo_box_masked.SetCtrlParameters(formatcodes = 'F!V_')
         self.Bind(wx.EVT_COMBOBOX, self.EvtComboMasked, self.combo_box_masked)
         self.combo_box = wx.ComboBox(self, wx.ID_ANY, choices=choices)
         self.Bind(wx.EVT_COMBOBOX, self.EvtCombo, self.combo_box)
         sizer = wx.BoxSizer(wx.VERTICAL)
         sizer.Add(self.combo_box_masked, 1, wx.ALL, 5)
         sizer.Add(self.combo_box, 1, wx.ALL, 5)
         self.SetSizerAndFit(sizer)

    def EvtCombo(self, event):
        one_combo = self.combo_box.GetValue()
        one_str = 'one'
        print one_combo == one_str

    def EvtComboMasked(self, event):
        one_combo = self.combo_box_masked.GetValue()
        one_str = 'one'
        print one_combo == one_str

class DemoFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Test Masked")
        panel = TestMasked(self)
        self.SetSize((500,500))
        self.Center()
        self.Show()

#----------------------------------------------------------------------
if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = DemoFrame()
    app.MainLoop()

Solution

  • The difference is that the masked combo is returning this:

    'one          '
    

    Note that it's not 'one', but 'one' with a bunch of spaces after it.

    Just add a .strip() to the end of the call:

    self.combo_box_masked.GetValue().strip()
    

    That will fix it.