Wxpython Custom Event implementation and usage analysis, wxpython custom
This article describes how to implement and use custom events in wxpython. We will share this with you for your reference. The details are as follows:
To create a custom event, follow these steps:
① Define the event class. The event class must inherit from wx. PyCommandEvent and define the get and set methods to get and set event parameters.
② Create an event type and a binder object to bind the event to a specific object.
③ Create a custom event object, set event parameters, and use the ProcessEvent () method to introduce this instance to the event processing system.
④ Bind the event handler of the Custom event.
⑤ Respond to the event in event handler.
Sample Code:
#! /Usr/bin/env python # coding = utf-8import wxclass MyTestEvent (wx. pyCommandEvent): #1 Define the event def _ init _ (self, evtType, id): wx. pyCommandEvent. _ init _ (self, evtType, id) self. eventArgs = "" def GetEventArgs (self): return self. eventArgs def SetEventArgs (self, args): self. eventArgs = argsmyEVT_MY_TEST = wx. newEventType () #2 create an event type EVT_MY_TEST = wx. pyEventBinder (myEVT_MY_TEST, 1) #3 create a biner object class MyFrame (wx. frame): def _ init _ (self): wx. frame. _ init _ (self, None,-1, "My Frame", size = (300,300), pos = (300,300) panel = wx. panel (self,-1) self. button1 = wx. button (panel, id =-1, pos = (40, 40), label = "button1") self. bind (wx. EVT_BUTTON, self. onButton1Click, self. button1) self. bind (EVT_MY_TEST, self. onHandle) #4 bind the event handler def OnButton1Click (self, event): self. onDoTest () def OnHandle (self, event): #8 event processing function dlg = wx. messageDialog (self, event. getEventArgs (), 'A Message box', wx. OK | wx. ICON_INFORMATION) dlg. showModal () dlg. destroy () def OnDoTest (self): evt = MyTestEvent (myEVT_MY_TEST, self. button1.GetId () #5 create a custom event object evt. setEventArgs ("test event") #6 add data to event self. getEventHandler (). processEvent (evt) #7 process the event if _ name _ = '_ main _': app = wx. pySimpleApp () frame = MyFrame () frame. show (True) app. mainLoop ()
Note:
1. the MyTestEvent class is defined as wx. subclass of PyCommandEvent, wx. pyCommandEvent is a specific structure of wxPython. It can be used to create a new event class and connect the C ++ class with your Python code.
2. wx. NewEventType () is similar to wx. NewId (); it returns a unique event type ID.
3. creates a binder object. The second parameter is located between [0, 2] and represents the wxId ID, which is used for wx. evtHandler. bind () method to determine which object is the event source.
4. Bind the event processor.
5. Create a Custom Event object and pass the ID of the control that triggers the event as a parameter to the constructor of MyTestEvent.
6. add data to the event. You can pass the information you need in this way.
7. The ProcessEvent () call introduces this new event to the event processing system. The GetEventHandler () call returns an instance of wx. EvtHandler, that is, the window object itself, that is, MyFrame.
8. Bind the event handler function. The event processing method here is to display the input event parameters through MessageDialog.