wxPython事件驱动实例详解
作者:shichen2014 发布时间:2021-09-21 18:25:11
本文实例讲述了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 methodVeto().
#!/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程序设计有所帮助。
猜你喜欢
- 本文实例讲述了php实现比较全的数据库操作类。分享给大家供大家参考。具体如下:<?php class database {  
- 一:数据源介绍本篇文章介绍的是使用python实现对葵花8号卫星数据进行自动下载。葵花8号卫星是日本的一颗静止轨道气象卫星,覆盖范围为60S
- python logging 重复写日志问题用Python的logging模块记录日志时,遇到了重复记录日志的问题,第一条记录写一次,第二条
- 1.使用 urllib2 实现#! /usr/bin/env python# -*- coding=utf-8 -*- import url
- 详解python里使用正则表达式的分组命名方式分组匹配的模式,可以通过groups()来全部访问匹配的元组,也可以通过group()函数来按
- 代码如下:--建立数据表createtable TestData ( ID int identity(1,1) primary key, D
- <% If Err.Number <> 0 Th
- 在我们python中输入输出函数在程序中运用较为广泛,运算符常用于if判断的条件中,今天我来给大家讲解这两项概念.input输入和print
- 熟悉css的开发者一定知道图像替换技术,也深知它的意义,Dave Shea 曾在他的一篇文章对此做了详细的总结,参看 Dave Shea’s
- 二次移动平均法逻辑二次移动平均法是一种重要的数学工具,用于处理时间序列数据,它的主要目的是通过平滑序列中的噪音数据来更好地捕捉趋势。具体实现
- 首先进入python官网https://www.python.org/通过Downloads选项,选择需要的版本进行下载,此处我以 Wind
- 前言:什么是anaconda??Anaconda指的是一个开源的Python发行版本,其包含了conda、Python等180多个科学包及其
- location是javascript里边管理地址栏的内置对象,比如location.href就管理页面的url,用location.hre
- 一.gb2312,gbk,utf8等支持多字节编码的字符集都可以储存汉字,gb2312中的汉字数量远少于gbk,而gb2312,gbk等都可
- 什么是钩子之前有转一篇关于回调函数的文章钩子函数、注册函数、回调函数,他们的概念其实是一样的。 钩子函数,顾名思义,就是把我们自己实现的ho
- def sa_obj_to_dict(obj, filtrate=None, rename=None):
- python除了关键字(keywords)和内置的类型和函数(builtins),更多的功能是通过libraries(即modules)来提
- 俗话说‘人生苦短,我有Python',但是如果初学Python的过程中碰到包和Python版本的问题估计会让你再苦一会,我在学习Py
- 摘要:主要介绍一些python的文件读取功能,文件内容修改,文件名后缀更改等操作。批处理文件功能import ospath1 = '
- 在迁移学习finetune时我们通常需要冻结前几层的参数不参与训练,在Pytorch中的实现如下:class Model(nn.Module