Search code examples
pythongtkwxpythonwxwidgets

wxGTK+ wxComboBox deselection


When creating wx.ComboBox under Windows you can specify wxCB_READONLY to let users select from only proposed options. But you can clear selection by:

combo.SetSelection(wx.NOT_FOUND)

But under linux (wxGTK) the option is deselected on creation, but once selected in cannot be cleared. Not by any of following:

combo.SetSelection(wx.NOT_FOUND)
combo.SetValue('')

Is it possible to do this anyhow?


Solution

  • Actually, setting the value to an empty string works for me on Arch Linux:

    import wx
    
    ########################################################################
    class MyPanel(wx.Panel):
        """"""
    
        #----------------------------------------------------------------------
        def __init__(self, parent):
            """Constructor"""
            wx.Panel.__init__(self, parent)
    
            choices = ["", "1", "2", "3"]
            self.choices = choices
            self.cbo = wx.ComboBox(self, value="1", choices=choices)
            btn = wx.Button(self, label="Reset")
            btn.Bind(wx.EVT_BUTTON, self.onReset)
    
            sizer = wx.BoxSizer(wx.VERTICAL)
            sizer.Add(self.cbo, 0, wx.ALL, 5)
            sizer.Add(btn, 0, wx.ALL, 5)
            self.SetSizer(sizer)
    
        #----------------------------------------------------------------------
        def onReset(self, event):
            """"""
            print "resetting"
            self.cbo.SetValue("")
    
    
    ########################################################################
    class MyFrame(wx.Frame):
        """"""
    
        #----------------------------------------------------------------------
        def __init__(self):
            """Constructor"""
            wx.Frame.__init__(self, None, title="CombBox")
            panel = MyPanel(self)
            self.Show()
    
    if __name__ == "__main__":
        app = wx.App(False)
        frame = MyFrame()
        app.MainLoop()