当前位置: 移动技术网 > IT编程>脚本编程>Python > wxPython事件驱动实例详解

wxPython事件驱动实例详解

2019年06月14日  | 移动技术网IT编程  | 我要评论

蕙兰瑜伽视频下载,陌陌haobc,今计划儿子死亡内谋

本文实例讲述了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程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网