Home Backend Development Python Tutorial wxPython事件驱动实例详解

wxPython事件驱动实例详解

Jun 06, 2016 am 11:32 AM
wxpython event drive

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

Copy after login

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

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()

Copy after login

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

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Does Logitech ghub driver not support win7? -Why can Logitech ghub driver only be installed on the c drive? Does Logitech ghub driver not support win7? -Why can Logitech ghub driver only be installed on the c drive? Mar 18, 2024 pm 05:37 PM

Does Logitech ghub driver not support win7? Not compatible. Since Windows 7 has stopped updating and is no longer Microsoft's main operating system, many new software no longer supports it, such as Logitech ghub. The main interface of the Logitech driver: 1. The main software interface is on the left. The three buttons are lighting, buttons, and sensitivity settings. 2. In the settings of the lighting interface, the general special effects are relatively conventional, and the audio visual effects are the highlight. They can change color according to the sound frequency, and can be set according to the high, middle and bass bands, with different colors and effects. 3. In button settings, users can edit them here according to their special requirements. 4. In the sensitivity settings, many users will have some of their own settings. They can add the DPI speed switching point by themselves, but

Steps to restore Logitech driver to default configuration Steps to restore Logitech driver to default configuration Feb 28, 2024 am 11:04 AM

Facing the Logitech driver that cannot be used normally, it can only be solved by restoring the factory settings. However, many friends do not know how to restore the factory settings. The following will provide you with detailed steps to restore the factory settings. I hope it can help you. Steps to restore the Logitech driver to the default configuration: 1. First download the Logitech dedicated driver software GHub, and open it after the download is complete. 2. Then open the mouse to set up, and click the settings (gear) in the upper right corner. 3. At the bottom, click "Restore default settings" , click directly and reopen the software to restore factory settings. How to connect the Logitech driver to the device 1. Open the back cover of the mouse and take out the wireless mouse socket. 2. Select the mouse socket next to the computer. 3. Set the corresponding parameter information for the mouse. 4. Bluetooth pair the device and long press Bluetooth.

How to download Razer mouse driver How to download Razer mouse driver Mar 11, 2024 pm 03:40 PM

Steps to download the Razer mouse driver: 1. Open the browser and enter the Razer official website; 2. On the official website page, find and click "Technical Support" or a similar option; 3. On the technical support page, select "Mouse" or the specific subcategory; 4. On the mouse driver download page, you can see various mouse models and their corresponding drivers; 5. Click the download link for the selected driver; 6. After the download is completed, check whether the downloaded file is complete , make sure nothing is damaged or missing.

How to install win11 driver without digital signature_Tutorial on how to deal with win11 driver without digital signature How to install win11 driver without digital signature_Tutorial on how to deal with win11 driver without digital signature Mar 20, 2024 pm 04:46 PM

Some users have encountered some problems when installing drivers for win11 computers. The computer prompts that the digital signature of this file cannot be verified, resulting in the inability to install the driver. How to solve this problem? Please see the following introduction for details. 1. Press the [Win + [Ctrl+Shift+Enter] Open the Windows Powershell window with administrator rights; 3. User Account Control window, do you want to allow this application to make changes to your device? Click [Yes]; 4. Administrator: Windows Powers

How to solve the problem that win11 driver does not have digital signature? Tutorial to solve the problem that win11 driver does not have digital signature How to solve the problem that win11 driver does not have digital signature? Tutorial to solve the problem that win11 driver does not have digital signature Feb 19, 2024 pm 11:20 PM

When we install the win11 system or run the program, we sometimes need a digital signature to use it normally. However, many users are asking how to solve the problem that the win11 driver does not have a digital signature? Users can open a Windows Powershell window with administrator rights to perform operations. Let this site carefully introduce to users the solution to the problem that the win11 driver does not have a digital signature. Solution to the problem that the win11 driver does not have a digital signature: 1. Press the [Win+X] key combination, or [right-click] click the [Windows logo] on the taskbar, and select [Run] in the menu item that opens. 2. In the run window, enter [powershel

Python GUI programming: Get started quickly and easily create interactive interfaces Python GUI programming: Get started quickly and easily create interactive interfaces Feb 19, 2024 pm 01:24 PM

A brief introduction to python GUI programming GUI (Graphical User Interface, graphical user interface) is a way that allows users to interact with computers graphically. GUI programming refers to the use of programming languages ​​to create graphical user interfaces. Python is a popular programming language that provides a rich GUI library, making Python GUI programming very simple. Introduction to Python GUI library There are many GUI libraries in Python, the most commonly used of which are: Tkinter: Tkinter is the GUI library that comes with the Python standard library. It is simple and easy to use, but has limited functions. PyQt: PyQt is a cross-platform GUI library with powerful functions.

Teach you how to quickly install the printer driver (detailed tutorial) Teach you how to quickly install the printer driver (detailed tutorial) Mar 14, 2024 am 11:30 AM

After the computer is connected to the printer, the corresponding printer driver needs to be installed for normal operation. Some printer faults also need to be solved through the printer driver. So how do we install the printer driver? There are mainly two methods, one is manual and the other is using third-party software. Method 1: For the manual installation method, make sure you know the brand and model of the printer you are using. You can look it up on the details page or manual at the time of purchase. You can also look for the relevant logo on the printer itself. Enter the brand name in the search engine, look for the official website, enter [Software and Driver Download], select [Printer], enter the product model, and download the printer driver. After the download is completed, find the corresponding program and double-click to install it. Wait until the installation is completed.

Detailed information on the location of the printer driver on your computer Detailed information on the location of the printer driver on your computer Jan 08, 2024 pm 03:29 PM

Many users have printer drivers installed on their computers but don't know how to find them. Therefore, today I bring you a detailed introduction to the location of the printer driver in the computer. For those who don’t know yet, let’s take a look at where to find the printer driver. When rewriting content without changing the original meaning, you need to The language is rewritten to Chinese, and the original sentence does not need to appear. First, it is recommended to use third-party software to search. 2. Find "Toolbox" in the upper right corner. 3. Find and click "Device Manager" below. Rewritten sentence: 3. Find and click "Device Manager" at the bottom 4. Then open "Print Queue" and find your printer device. This time it is your printer name and model. 5. Right-click the printer device and you can update or uninstall it.

See all articles