wxPython:事件處理介紹二


事件處理流程,如下:

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()
    
#    #消息對話框    
#    def OnCloseMe(self, event):
#        dlg = wx.MessageDialog(None, u"消息對話框測試", u"標題信息", wx.YES_NO | wx.ICON_QUESTION)
#        if dlg.ShowModal() == wx.ID_YES:
#            self.Close(True)
#        dlg.Destroy()
#    
#    #文本輸入對話框    
#    def OnCloseMe(self, event):
#        dlg = wx.TextEntryDialog(None, u"請在下面文本框中輸入內容:", u"文本輸入框標題", u"默認內容")
#        if dlg.ShowModal() == wx.ID_OK:
#            message = dlg.GetValue() #獲取文本框中輸入的值
#            dlg_tip = wx.MessageDialog(None, message, u"標題信息", wx.OK | wx.ICON_INFORMATION)
#            if dlg_tip.ShowModal() == wx.ID_OK:
#                self.Close(True)
#            dlg_tip.Destroy()
#        dlg.Destroy()  
     #列表選擇對話框
#    def OnCloseMe(self, event):
#        dlg = wx.SingleChoiceDialog(None, u"請選擇你喜歡的水果:", u"列表選擇框標題",
#                                    [u"蘋果", u"西瓜", u"草莓"])
#        if dlg.ShowModal() == wx.ID_OK:
#            message = dlg.GetStringSelection() #獲取選擇的內容
#            dlg_tip = wx.MessageDialog(None, message, u"標題信息", wx.OK | wx.ICON_INFORMATION)
#            if dlg_tip.ShowModal() == wx.ID_OK:
#                self.Close(True)
#            dlg_tip.Destroy()
#        dlg.Destroy()
         
           
            
if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = MyFrame(parent = None, id = -1)
    frame.Show()
    app.MainLoop()
        

測試一下:

1、初始運行:

2、鼠標移動到按鈕上:

3、鼠標左鍵單擊:

4、鼠標離開按鈕:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM