wx事件处理二

wxPython首先在触发对象中查找匹配事件类型的被绑定的处理器函数,如果找到,刚相应方法被执行。如果没找到,wxPython将检查该事件是否传送到了上一级的容器,如果是,父窗口被检查,如此一级级向上查找,直到找到一个处理函数或到达顶层窗口。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
    Function:常用对话框实例
    Input:NONE
    Output: NONE
    author: socrates
    blog:http://www.cnblogs.com/dyx1024/
    date:2012-07-07
'''  

import wx

class MyFrame(wx.Frame):
    
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, u'测试面板Panel', size = (600, 300))
        
        #创建面板
        self.panel = wx.Panel(self) 
        
        #在Panel上添加Button
        self.button = wx.Button(self.panel, label = u'关闭', pos = (150, 60), size = (100, 60))
        
        #绑定单击事件
        self.Bind(wx.EVT_BUTTON, self.OnCloseMe, self.button)
        
        #绑定鼠标位于按钮上事件
        self.button.Bind(wx.EVT_ENTER_WINDOW, self.OnEnterWindows)
        
        #绑定鼠标离开事件
        self.button.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeaveWindows)
        
    def OnCloseMe(self, event):
        self.panel.SetBackgroundColour('Red')
        self.panel.Refresh()
    
    def OnEnterWindows(self, event):
        self.panel.SetBackgroundColour('Blue')
        self.panel.Refresh()
        self.button.SetLabel(u"鼠标在我上面")
        event.Skip()
    
    def OnLeaveWindows(self, event):
        self.panel.SetBackgroundColour('Green')
        self.panel.Refresh()       
        self.button.SetLabel(u"鼠标离开我了")
        event.Skip()

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

猜你喜欢

转载自www.cnblogs.com/fzuhyj/p/9162891.html