问题描述
我有一个wxPython gui,我想以编程方式生成一个事件。我尝试过这样的语法:
e = wx.Event .__ init __(grid,eventType = wx.EVT_LEFT_DOWN)
导致:
TypeError:未绑定的方法__init __()必须使用Event实例作为第一个参数调用(取而代之的是Grid实例)
或:
e = wx.CommandEvent(commandType = wx.EVT_BUTTON)
TypeError:在方法'new_CommandEvent'中,'wxEventType'类型的预期参数1
所以问题是简单的,我需要用来创建事件对象的确切的字面语法是什么?或者,有人可以指出我有一个很好的资源来感受事件吗?我不知道我是否只是在我的理解中缺少一些简单的东西。我还没有在线找到这个问题的直接答案。我查看了这个问题:,但是我不想做一个自定义的事件。
提前感谢
code> wx.PostEvent
要以编程方式生成事件:
wx.PostEvent(self.GetEventHandler(),wx.PyCommandEvent(wx.EVT_BUTTON.typeId,self.GetId()))
如果您要发布 wx.EVT_BUTTON
事件。使它成为一个 PyCommandEvent
意味着它将向上传播;默认情况下,其他事件类型不会传播。
wx.PostEvent()
的一般形式:
这是一个小例子代码:
import wx
class MyFrame(wx.Frame):
def __init __(self,parent):
wx.Frame .__ init__(self,parent,id = wx.ID_ANY,title = uTest,pos = wx.DefaultPosition,size = wx.Size(200,200),style = wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL)
sizer_inside = wx.BoxSizer (wx.VERTICAL)
#添加按钮和textCtrl窗口小部件
self.button = wx.Button(self,wx.ID_ANY,u单击我,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.Defau ltposition,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
#绑定事件
self.Bind(wx.EVT_BUTTON,self.on_click)
self.Bind(wx.EVT_CHOICE,self.test_dummy)
#Event处理程序
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(无)
app.MainLoop()
如果你运行这个,请注意, textCtrl
将显示 2 ,点击按钮。第一个事件处理程序手动触发由 test_dummy
处理的第二个事件。
I have a wxPython gui and I want to programmatically generate an event.
I've tried a syntax like this:
e = wx.Event.__init__(grid, eventType=wx.EVT_LEFT_DOWN)
which results in:
TypeError: unbound method __init__() must be called with Event instance as first argument (got Grid instance instead)
or:
e = wx.CommandEvent(commandType=wx.EVT_BUTTON)
TypeError: in method 'new_CommandEvent', expected argument 1 of type 'wxEventType'
So question is simply, what is the exact, literal syntax that I need to use to create an event object? Or, can someone point me to a good resource for making sense of events? I don't know if I'm just missing something simple in my understanding. I haven't been able to find a direct answer to this question yet online. I checked out this question: Generate a custom CommandEvent in wxPython , but I don't want to make a custom event.
Thanks in advance!
You would want to use wx.PostEvent
To programatically generate an event:
wx.PostEvent(self.GetEventHandler(), wx.PyCommandEvent(wx.EVT_BUTTON.typeId, self.GetId()))
if you want to post a wx.EVT_BUTTON
event. Making it a PyCommandEvent
means it will propagate upwards; other event types don't propagate by default.
The general form of wx.PostEvent()
: http://www.wxpython.org/docs/api/wx-module.html#PostEvent
Here's a small example code:
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()
If you run this, notice that the textCtrl
will display 2 after clicking the button. The first event handler manually fires the second event which is handled by test_dummy
.
这篇关于如何在wxPython中以编程方式生成事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!