如何在wxPython中以编程方式生成事件

如何在wxPython中以编程方式生成事件,python,datagrid,wxpython,Python,Datagrid,Wxpython,我有一个wxpythongui,我想通过编程生成一个事件 我尝试过这样的语法: 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) 或: 所以问题很简单,我需要使用什么确切的文字语法来创建

我有一个wxpythongui,我想通过编程生成一个事件

我尝试过这样的语法:

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)
或:

所以问题很简单,我需要使用什么确切的文字语法来创建事件对象?或者,有人能给我指出一个很好的资源来理解事件吗?我不知道我的理解是否遗漏了一些简单的东西。我还没有在网上找到这个问题的直接答案。我签出了这个问题:,但我不想创建自定义事件


提前谢谢

我认为您最好使用win32gui.PostMessage()

这对你有帮助


您可能希望使用
wx.PostEvent

要以编程方式生成事件,请执行以下操作:

wx.PostEvent(self.GetEventHandler(), wx.PyCommandEvent(wx.EVT_BUTTON.typeId, self.GetId()))
如果要发布
wx.EVT\u按钮
事件。使其成为
PyCommandEvent
意味着它将向上传播;默认情况下,其他事件类型不会传播

wx.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\u dummy

使用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)

谢谢@我的评论救了我

我试图在Windows下调用WxPython上的“下一步”按钮

当我尝试使用pythonic事件时,我的向导将在不浏览页面的情况下结束,就好像我按下了“Finish”按钮一样,即使它在页面上不可见

如果我理解正确,因为这是一个Windows类(而不是Python类),所以我不能使用Python事件。因此,我不得不调用windows事件

也许另一种策略是从windows向导类切换到Python向导类,但我没有尝试

顺便说一句,我最后发送的消息是:

win32gui.PostMessage(wizard.GetHandle(),0x0111,wx.ID_FORWARD,self.wizard.FindWindowById(wx.ID_FORWARD).GetHandle())

你应该在答案中添加相关代码,以防链接失效。此外,我们也不确定OP是否使用Windows,或者他/她是否可以接受仅使用Windows的解决方案。谢谢,这救了我一命。我试图在Windows下调用WxPython上的“下一步”按钮。如果我理解正确,因为这是一个Windows类(而不是Python类),所以我不能使用Python事件。因此,我不得不调用windows事件。当我尝试使用pythonic事件时,我的向导将在不浏览页面的情况下结束,就好像我按下了“Finish”按钮一样,即使它在页面上不可见。也许另一种策略是从windows向导类切换到Python向导类,但我没有尝试。谢谢。。我不理解的关键信息是wx.PyCommandEvent接受一个整数。另外,感谢其他回答者指出wx.wxEVT_*语法。
win32gui.PostMessage(wizard.GetHandle(),0x0111,wx.ID_FORWARD,self.wizard.FindWindowById(wx.ID_FORWARD).GetHandle())