我正在尝试模拟位图按钮的按钮单击。代码不会抛出任何错误而且什么都不做......
self.buttonImage = wx.Bitmap(button_image, wx.BITMAP_TYPE_PNG)
self.button = wx.BitmapButton(self, -1, self.buttonImage, pos=(100, 300), style = wx.NO_BORDER)
evt = wx.PyCommandEvent(wx.EVT_BUTTON.typeId, self.button.GetId())
wx.PostEvent(self, evt)
答案 0 :(得分:1)
A matching answer has been given already in another post.假设您在同一个帖子中(这应该是wxPython中最常见的情况),您必须将self
中的PostEvent
替换为与该按钮相关的内容。当然,您发布的事件不会产生任何结果,因为您没有将事件绑定到位图按钮。
# button creation
self.button = wx.BitmapButton(pnl, -1, self.buttonImage, pos=(100, 300), style=wx.NO_BORDER)
# Bind an event
self.button.Bind(wx.EVT_BUTTON, self.on_btn)
# GUI test code
testbtn = frm.button # the button to be tested
evt = wx.PyCommandEvent(wx.EVT_BUTTON.typeId, testbtn.GetId())
wx.PostEvent(testbtn, evt) # try frm instead of testbtn and it will not work