我有一个wxPython gui,我想以编程方式生成一个事件。
我尝试过这样的语法:
e = wx.Event.__init__(grid, eventType=wx.EVT_LEFT_DOWN)
导致:
TypeError: unbound method __init__() must be called with Event instance as first argument (got Grid instance instead)
或:
e = wx.CommandEvent(commandType=wx.EVT_BUTTON)
TypeError: in method 'new_CommandEvent', expected argument 1 of type 'wxEventType'
所以问题很简单,我需要用什么来创建一个事件对象?或者,是否有人能够指出我对事件有意义的良好资源?我不知道我是否只是在理解中遗漏了一些简单的东西。我还没能在网上找到这个问题的直接答案。我查看了这个问题:Generate a custom CommandEvent in wxPython,但我不想制作自定义事件。
提前致谢!
答案 0 :(得分:3)
您可能希望使用wx.PostEvent
以编程方式生成事件:
wx.PostEvent(self.GetEventHandler(), wx.PyCommandEvent(wx.EVT_BUTTON.typeId, self.GetId()))
如果您想发布wx.EVT_BUTTON
个活动。使它成为PyCommandEvent
意味着它会向上传播;其他事件类型默认不会传播。
wx.PostEvent()
的一般形式:http://www.wxpython.org/docs/api/wx-module.html#PostEvent
这是一个小示例代码:
import wx
class MyFrame ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"Test", pos = wx.DefaultPosition, size = wx.Size( 200,200 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
sizer_inside = wx.BoxSizer( wx.VERTICAL )
# Adding a button and a textCtrl widget
self.button = wx.Button( self, wx.ID_ANY, u"Click Me", wx.DefaultPosition, wx.DefaultSize, 0 )
sizer_inside.Add( self.button, 0, wx.ALIGN_CENTER|wx.ALL, 5 )
self.textCtrl = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, wx.TE_NO_VSCROLL )
sizer_inside.Add( self.textCtrl, 0, wx.ALIGN_CENTER|wx.ALL, 5 )
self.SetSizer( sizer_inside )
self.Layout()
self.Centre( wx.BOTH )
self.Show()
self.counter = 0
# Binding Events
self.Bind( wx.EVT_BUTTON, self.on_click )
self.Bind( wx.EVT_CHOICE, self.test_dummy)
#Event handlers
def on_click( self, event ):
self.counter += 1
wx.PostEvent(self.GetEventHandler(), wx.PyCommandEvent(wx.EVT_CHOICE.typeId, self.GetId()))
def test_dummy(self, event):
self.counter += 1
self.textCtrl.SetValue(str(self.counter))
if __name__ == "__main__":
app = wx.App(False)
MyFrame(None)
app.MainLoop()
如果您运行此操作,请注意点击按钮后textCtrl
将显示 2 。第一个事件处理程序手动触发由test_dummy
处理的第二个事件。
答案 1 :(得分:0)
答案 2 :(得分:0)
使用wx.PostEvent ...就像这样:
class launcherWindow(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, parent=None, title='New Window')
#now add the main body, start with a panel
panel = wx.Panel(self)
#instantiate a new dropdown
self.productDropDown = wx.ComboBox(panel, size=wx.DefaultSize, style = wx.CB_READONLY)
#get the products and product subtypes
self.productDict = self.getProductsAndSubtypes()
#setup subtypes first, just in case, since onProductSelection will reference this
self.productSubtypeDropDown = wx.ComboBox(panel, size=wx.DefaultSize, style = wx.CB_READONLY)
#add products
for product in self.productDict.keys():
self.productDropDown.Append(product)
#bind selection event
self.productDropDown.Bind(wx.EVT_COMBOBOX, self.onProductSelection)
#set default selection
self.productDropDown.SetSelection(0)
#pretend that we clicked the product selection, so it's event gets called
wx.PostEvent(self.productDropDown, wx.CommandEvent(wx.wxEVT_COMMAND_COMBOBOX_SELECTED))
#now add the dropdown to a sizer, set the sizer for the panel, fit the panel, etc...
def onProductSelection(self, event):
productSelected = self.productDropDown.GetStringSelection()
productSubtypes = self.productDict[productSelected]
#clear any existing product subtypes, since each product may have different ones
self.productSubtypeDropDown.Clear()
for productSubtype in productSubtypes:
self.productSubtypeDropDown.Append(productSubtype)
#select the first item by default
self.productSubtypeDropDown.SetSelection(0)
答案 3 :(得分:0)
感谢。 @ i5on9i评论救了我。
我试图在Windows下调用WxPython上的Next按钮。
当我尝试使用pythonic事件时,我的向导只会在没有浏览页面的情况下结束,就像我按下“完成”按钮一样,即使它在页面上不可见。
如果我理解正确,因为这是一个Windows类(而不是pythonic类),我无法使用pythonic事件。因此我不得不打电话给windows事件。
也许不同的策略是从Windows Wizard类切换到pythonic向导类,但我没有尝试过。
BTW我最后发送的消息是:
win32gui.PostMessage(wizard.GetHandle(),0x0111,wx.ID_FORWARD,self.wizard.FindWindowById(wx.ID_FORWARD).GetHandle())