首頁 後端開發 Python教學 wxPython事件驱动实例详解

wxPython事件驱动实例详解

Jun 06, 2016 am 11:32 AM
wxpython 事件 驅動

本文实例讲述了wxPython的事件驱动机制,分享给大家供大家参考。具体方法如下:

先来看看如下代码:

#!/usr/bin/python 
 
# moveevent.py 
 
import wx  #导入wx库 
 
class MoveEvent(wx.Frame): 
  def __init__(self, parent, id, title): 
    wx.Frame.__init__(self, parent, id, title, size=(250, 180)) #窗口大小为(250, 180) 
 
    wx.StaticText(self, -1, 'x:', (10,10))#parent, id, title, point 
    wx.StaticText(self, -1, 'y:', (10,30)) 
    self.st1 = wx.StaticText(self, -1, '', (30, 10)) 
    self.st2 = wx.StaticText(self, -1, '', (30, 30)) 
 
    self.Bind(wx.EVT_MOVE, self.OnMove)  #绑定Frame的move事件 
 
    self.Centre() 
    self.Show(True) 
 
  def OnMove(self, event): 
    x, y = event.GetPosition() 
    self.st1.SetLabel(str(x)) 
    self.st2.SetLabel(str(y)) 
     
app = wx.App()#生成应用程序 
MoveEvent(None, -1, 'move event')#调用自己的类,三个参数为:parent, id , title 
app.MainLoop()#应用程序事件循环 

登入後複製

程序运行效果如下图所示:

wxStaticText的两个构造函数官方文档如下:
wxStaticText ()
Default constructor.
wxStaticText (wxWindow *parent, wxWindowID id, const wxString &label, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=0, const wxString&name=wxStaticTextNameStr)

Constructor, creating and showing a text control.

The event parameter in the OnMove() method is an object specific to a particular event type. In our case it is the instance of a wx.MoveEvent class. This object holds information about the event. For example the Event object or the position of the window. In our case the Event object is the wx.Frame widget. We can find out the current position by calling the GetPosition() method of the event.

OnMove()方法中的event参数是一种特殊的事件类型,在我们的例子中,它是wx.MoveEvnet类的一个实例.这个对象保存了事件的一些信息,比如这个事件对象或者窗口的位置.在我们例子中事件对象是一个wx.Frame控件.我们可以通过调用事件对象的GetPosition()得到当前位置信息.

Vetoing events

Sometimes we need to stop processing an event. To do this, we call the method Veto().

#!/usr/bin/python 
 
# veto.py 
 
import wx 
 
class Veto(wx.Frame): 
  def __init__(self, parent, id, title): 
    wx.Frame.__init__(self, parent, id, title, size=(250, 200)) 
 
 
    self.Bind(wx.EVT_CLOSE, self.OnClose) 
 
    self.Centre() 
    self.Show(True) 
 
  def OnClose(self, event): 
 
    dial = wx.MessageDialog(None, 'Are you sure to quit?', 'Question', 
      wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION) 
    ret = dial.ShowModal() 
    if ret == wx.ID_YES: 
      self.Destroy() 
    else: 
      event.Veto() 
 
app = wx.App() 
Veto(None, -1, 'Veto') 
app.MainLoop()

登入後複製

希望本文所述对大家的Python程序设计有所帮助。

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章標籤

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

羅技ghub驅動不支援win7嗎? -羅技ghub驅動為什麼只能安裝在c盤? 羅技ghub驅動不支援win7嗎? -羅技ghub驅動為什麼只能安裝在c盤? Mar 18, 2024 pm 05:37 PM

羅技ghub驅動不支援win7嗎? -羅技ghub驅動為什麼只能安裝在c盤?

羅技驅動恢復預設配置步驟 羅技驅動恢復預設配置步驟 Feb 28, 2024 am 11:04 AM

羅技驅動恢復預設配置步驟

雷蛇滑鼠驅動怎麼下載 雷蛇滑鼠驅動怎麼下載 Mar 11, 2024 pm 03:40 PM

雷蛇滑鼠驅動怎麼下載

win11驅動沒有數位簽章怎麼解決? win11驅動沒有數位簽章的解決教學 win11驅動沒有數位簽章怎麼解決? win11驅動沒有數位簽章的解決教學 Feb 19, 2024 pm 11:20 PM

win11驅動沒有數位簽章怎麼解決? win11驅動沒有數位簽章的解決教學

win11沒有數位簽章的驅動程式怎麼安裝_Win11驅動沒有數位簽章的處理教學課程 win11沒有數位簽章的驅動程式怎麼安裝_Win11驅動沒有數位簽章的處理教學課程 Mar 20, 2024 pm 04:46 PM

win11沒有數位簽章的驅動程式怎麼安裝_Win11驅動沒有數位簽章的處理教學課程

教你如何快速安裝印表機驅動(詳細教學) 教你如何快速安裝印表機驅動(詳細教學) Mar 14, 2024 am 11:30 AM

教你如何快速安裝印表機驅動(詳細教學)

如何解決無法開啟AMD驅動的問題 如何解決無法開啟AMD驅動的問題 Jan 10, 2024 am 12:01 AM

如何解決無法開啟AMD驅動的問題

win10驅動安裝路徑詳解 win10驅動安裝路徑詳解 Jan 04, 2024 pm 07:56 PM

win10驅動安裝路徑詳解

See all articles