python bind()事件疑问

xiaokang007 2010-08-24 02:50:11
以下代码是wxpython的实例源码:wxPIA_book1\Chapter-03\mouse_event.py
#!/usr/bin/env python

import wx

class MouseEventFrame(wx.Frame):

def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, 'Frame With Button',
size=(300, 100))
self.panel = wx.Panel(self)
self.button = wx.Button(self.panel, label="Not Over", pos=(100, 15))
self.Bind(wx.EVT_BUTTON, self.OnButtonClick, self.button)
self.button.Bind(wx.EVT_ENTER_WINDOW, self.OnEnterWindow)
self.button.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeaveWindow)

def OnButtonClick(self, event):
self.panel.SetBackgroundColour('Green')
self.panel.Refresh()

def OnEnterWindow(self, event):
self.button.SetLabel("Over Me!")
event.Skip()

def OnLeaveWindow(self, event):
self.button.SetLabel("Not Over")
event.Skip()


if __name__ == '__main__':
app = wx.PySimpleApp()
frame = MouseEventFrame(parent=None, id=-1)
frame.Show()
app.MainLoop()



问题一:
其中的
self.Bind(wx.EVT_BUTTON, self.OnButtonClick, self.button)
self.button.Bind(wx.EVT_ENTER_WINDOW, self.OnEnterWindow)
self.button.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeaveWindow)

self.Bind 和 self.button.Bind 有什么 不同么?


问题二:
就像上面的假如我定义多个定时器,我是否可以直接使用以下方式进行绑定,是否和self.bind()绑定有区别?

class timer:
def __init__(self....):
self.clocktimer = wx.timer(self)
self.clocktimer.bind(wx.EVT_TIMER, self.OnTimer)
self.testtimer = wx.timer(self)
self.testtimer.bind(wx.EVT_TIMER, self.OnTimer)

def OnTimer(self):
print "this is timer"
...全文
382 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
angel_su 2010-08-24
  • 打赏
  • 举报
回复
貌似一般常见wxCommandEvent类的用self.Bind(wx.EVT_BUTTON, self.OnButtonClick, self.button)
其他类需用控件本身来做self.button.Bind(wx.EVT_ENTER_WINDOW, self.OnEnterWindow)
又wxTimer这类只要知道处理函数的好像可以随便bind...
看demo里说自定义继承wxTimer的好像是不用bind,但要提供名称为Notify的一个方法
xiaokang007 2010-08-24
  • 打赏
  • 举报
回复
想的 和你的差不多 ,但是总觉的 还是有点区别
李察德-泰森 2010-08-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 selecthis 的回复:]
self.Bind 和 self.button.Bind 有什么 不同么?
Bind是用来将控件事件与控件做绑定的,用self.Bind时,在后面的参数中你可以看到加入了控件,self.button.Bind没有,因为是直接使用了控件的Bind。
[/Quote]
self.Bind(wx.EVT_BUTTON, self.OnButtonClick, self.button)
可以这么理解:
self.Bind(事件类型, 事件名, 绑定的控件名)

self.button.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeaveWindow)
self.button.Bind(事件类型, 事件名)
李察德-泰森 2010-08-24
  • 打赏
  • 举报
回复
self.Bind 和 self.button.Bind 有什么 不同么?
Bind是用来将控件事件与控件做绑定的,用self.Bind时,在后面的参数中你可以看到加入了控件,self.button.Bind没有,因为是直接使用了控件的Bind。

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧