您的位置:首页 > 编程语言 > Python开发

[wxPython] event.Skip()的用法

2016-07-03 16:28 441 查看

wxPython笔记 - event.Skip()的用法

wxPyWiki上学wxPython时,看到event.Skip()有点不能理解,使用过后发现其特性对于理解wxPython的多事件触发过程有所帮助,便记录下来。

以下是基于事件绑定前做的demo的修改,主要结构是”Test”菜单中的两个按钮分别绑定了一个和两个事件,这两个事件都是在控制台里输出不同的东西。

import wx

class MyFrame(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self,parent,title=title, size=(400,500))
self.control = wx.TextCtrl(self, style = wx.TE_MULTILINE)
self.CreateStatusBar()  #the status bar in the bottom

menuBar = wx.MenuBar()
testMenu = wx.Menu()
menuOnce = testMenu.Append(wx.ID_ANY, "Once", "print something")
menuTwice = testMenu.Append(wx.ID_ANY, "Twice", "print something and another thing")
menuBar.Append(testMenu, "Test")

self.Bind(wx.EVT_MENU, self.First, menuOnce)

self.Bind(wx.EVT_MENU, self.First, menuTwice)
self.Bind(wx.EVT_MENU, self.Second, menuTwice)

self.SetMenuBar(menuBar)

self.Show(True)

def First(self, event):
print "something"
def Second(self, event):
print "another thing"

app = wx.App(False)  #what does False mean?
frame = MyFrame(None, 'Small editor')
app.MainLoop()


如果直接这么运行,点击”twice”时,会发生只有Second()响应的情况,说明对菜单点击这个事件的处理只到Second()结束为止。因此,想要让First()和Second()都响应”twice”,那么需要在Second()中最后加上event.Skip()。

另外,根据这个demo也可以发现,绑定在同一个事件上的不同函数,其执行的顺序是从下往上,也就是后绑定的先运行。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: