wxpython 模拟位图按钮点击

1 投票
1 回答
663 浏览
提问于 2025-04-18 07:26

我正在尝试模拟一个位图按钮的点击操作。代码没有报错,但就是没有任何反应...

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)

1 个回答

1

在另一个帖子中已经给出了匹配的答案。 假设你在同一个线程中(这在wxPython中应该是最常见的情况),你需要把PostEvent中的self替换成与按钮相关的东西。当然,你发送的事件不会有任何效果,因为你没有把事件绑定到位图按钮上。

# 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

撰写回答