简体   繁体   English

python复选框事件触发器

[英]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 ? 是否可以手动触发功能?

Yes, you can use wx.CommandEvent and wx.PostEvent : 是的,您可以使用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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM