wxPython事件驱动实例详解


Posted in Python onSeptember 28, 2014

本文实例讲述了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()#应用程序事件循环

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

wxPython事件驱动实例详解

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程序设计有所帮助。

Python 相关文章推荐
零基础写python爬虫之神器正则表达式
Nov 06 Python
python3实现TCP协议的简单服务器和客户端案例(分享)
Jun 14 Python
jupyter notebook引用from pyecharts.charts import Bar运行报错
Apr 23 Python
解决Django数据库makemigrations有变化但是migrate时未变动问题
May 30 Python
python代码 输入数字使其反向输出的方法
Dec 22 Python
树莓派动作捕捉抓拍存储图像脚本
Jun 22 Python
Python中关于浮点数的冷知识
Sep 22 Python
python实现百度OCR图片识别过程解析
Jan 17 Python
Python + selenium + crontab实现每日定时自动打卡功能
Mar 31 Python
python调用私有属性的方法总结
Jul 24 Python
python super()函数的基本使用
Sep 10 Python
详细介绍python操作RabbitMq
Apr 12 Python
python中的多重继承实例讲解
Sep 28 #Python
python错误处理详解
Sep 28 #Python
python中实现定制类的特殊方法总结
Sep 28 #Python
python之wxPython菜单使用详解
Sep 28 #Python
python中lambda函数 list comprehension 和 zip函数使用指南
Sep 28 #Python
python之wxPython应用实例
Sep 28 #Python
Python实现从url中提取域名的几种方法
Sep 26 #Python
You might like
php 带逗号千位符数字的处理方法
2012/01/10 PHP
老司机传授Ubuntu下Apache+PHP+MySQL环境搭建攻略
2016/03/20 PHP
mysql desc(DESCRIBE)命令实例讲解
2016/09/24 PHP
PHP中include和require的区别实例分析
2017/05/07 PHP
使用PHPExcel导出Excel表
2018/09/08 PHP
使用swoole 定时器变更超时未支付订单状态的解决方案
2019/07/24 PHP
Node.js插件的正确编写方式
2014/08/03 Javascript
js实例属性和原型属性示例详解
2014/11/23 Javascript
jquery加载图片时以淡入方式显示的方法
2015/01/14 Javascript
Jquery判断radio、selelct、checkbox是否选中及获取选中值方法总结
2015/04/15 Javascript
动态加载jQuery的两种方法实例分析
2015/07/17 Javascript
深入理解jQuery中的事件冒泡
2016/05/24 Javascript
JQuery的常用选择器、过滤器、方法全面介绍
2016/05/25 Javascript
JS弹出窗口的运用与技巧大全
2016/11/01 Javascript
Bootstrap基本插件学习笔记之标签切换(17)
2016/12/08 Javascript
微信小程序webview 脚手架使用详解
2019/07/22 Javascript
webpack 处理CSS资源的实现
2019/09/27 Javascript
不刷新网页就能链接新的js文件方法总结
2020/03/01 Javascript
[56:00]2018DOTA2亚洲邀请赛 4.6 淘汰赛 VP vs TNC 第二场
2018/04/10 DOTA
怎样使用Python脚本日志功能
2016/08/14 Python
解决Python pandas df 写入excel 出现的问题
2018/07/04 Python
Python+Pandas 获取数据库并加入DataFrame的实例
2018/07/25 Python
浅谈python在提示符下使用open打开文件失败的原因及解决方法
2018/11/30 Python
学习python分支结构
2019/05/17 Python
pytorch之inception_v3的实现案例
2020/01/06 Python
python+opencv边缘提取与各函数参数解析
2020/03/09 Python
python中如何设置代码自动提示
2020/07/15 Python
回门宴父母答谢词
2014/01/26 职场文书
2014春晚主持词
2014/03/25 职场文书
文明寄语大全
2014/04/11 职场文书
个人担保书范文
2014/05/20 职场文书
党员干部公开承诺书范文
2015/04/27 职场文书
2016继续教育研修日志
2015/11/13 职场文书
浅谈Golang 切片(slice)扩容机制的原理
2021/06/09 Golang
MySQL RC事务隔离的实现
2022/03/31 MySQL
vue实现列表垂直无缝滚动
2022/04/08 Vue.js