Python复选框事件触发器



我想触发一个按钮事件,而不必点击它(手动)

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

我尝试了上面的代码,它只是初始化按钮检查,但它不触发事件功能。是否可以手动触发该函数?

是的,您可以使用wx.CommandEventwx.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()

最新更新