Search code examples
pythoncheckboxwxpython

python checkbox event trigger


I want to trigger a button event without having to click on it(manually)

self.cb1 = wx.CheckBox(self, -1, "pewpew")
self.Bind(wx.EVT_CHECKBOX, self.lg, self.cb1)
self.cb1.SetValue(True)

I tried the above code , it just initializes the button on checked but it does not trigger the event function. Is it possible to trigger the function manually ?


Solution

  • Yes, you can use wx.CommandEvent and wx.PostEvent:

    import wx
    
    class TestFrame(wx.Frame):
        def __init__(self, parent, title):
            wx.Frame.__init__(self, parent, id=-1, title=title)
            text = wx.StaticText(self, label=title)
    
            self.cb1 = wx.CheckBox(self, -1, "pewpew")
            self.Bind(wx.EVT_CHECKBOX, self.lg, self.cb1)
    
            evt = wx.CommandEvent(wx.EVT_CHECKBOX.typeId, self.cb1.GetId())
            wx.PostEvent(self, evt)
    
    
        def lg(self, in_event):
            print in_event
            print 'In lg'
    
    
    app = wx.App()
    frame = TestFrame(None, "Hello, world!")
    frame.Show()
    app.MainLoop()