radio-buttonwxpythontextctrl

Enable/disable text entry in IntCtrl on RadioButton click in wxPython


I have a simple gui that has two radio buttons and an IntCtrl for text entry. Initially, I have the top radio button selected and the IntCtrl disabled (unfortunately, I can't figure out how to set it to blank or "grayed out"):

enter image description here

Relevant code snippet:

def loadSettingsPanel(self):
    panel = wx.Panel(self)

    self.exposureAutomatic = wx.RadioButton(panel, label="Automatic (1ms)", style=wx.RB_GROUP)
    self.exposureManual = wx.RadioButton(panel, label="Manual")
    self.exposureValue = wx.lib.intctrl.IntCtrl(panel, style=wx.TE_READONLY)

    self.exposureManual.Bind(wx.EVT_RADIOBUTTON, self.onClick)

    # Add sizers, etc.

I want to "enable" the IntCtrl area in the onClick method, but I can't figure out how to do this. SetStyle() doesn't seem to have the option clear out the wx.TE_READONLY styling and I'd prefer not to recreate the IntCtrl entirely because then it gets annoying to reshuffle everything in the sizer. If there's some way to do this using a TextCtrl, I'd be happy to switch to that and just do the character filtering manually, but I haven't been able to figure out how to enable/disable those either.


Solution

  • Use the Enable function rather than a style.

    import wx
    import wx.lib.intctrl
    
    class MyFrame(wx.Frame):
    
        def __init__(self, parent):
    
            wx.Frame.__init__(self, parent, -1, "Intctrl Demo")
    
            panel = wx.Panel(self)
            self.exposureAutomatic = wx.RadioButton(panel, label="Automatic (1ms)", style=wx.RB_GROUP, pos=(50,50))
            self.exposureManual = wx.RadioButton(panel, label="Manual", pos=(50,80))
            self.ic = wx.lib.intctrl.IntCtrl(panel, -1, pos=(150, 80))
            self.ic.Enable(False)
            self.Bind(wx.EVT_RADIOBUTTON, self.onClick)
    
        def onClick(self, event):
            self.ic.Enable(self.exposureManual.GetValue())
    
    
    app = wx.App()
    
    frame = MyFrame(None)
    app.SetTopWindow(frame)
    frame.Show()
    
    app.MainLoop()
    

    enter image description hereenter image description here