This paper describes the event-driven mechanism of wxpython and shares it for everyone's reference. Here's how:
Let's take a look at the following code:
#!/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= (+)) #窗口大小为 WX. Statictext (self,-1, ' x: ', (10,10)) #parent, ID, title, point wx. Statictext (self,-1, ' Y: ', (10,30)) self.st1 = wx. Statictext (self,-1, ", (+)) Self.st2 = wx. Statictext (self,-1, ", (+)) 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))
The program works as shown in the following:
The official documentation for the two constructors of Wxstatictext is as follows:
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 was an object specific to a particular event type. 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. The Event object is the WX. Frame widget. We can find out the current position by calling the GetPosition () method of the event.
The event parameter in the OnMove () method is a special type of events, in our case, WX. An instance of the Moveevnet class. This object holds some information about the event, such as the event object or the location of the window. In our example, the event object is a WX. The frame control. We can get the current location information by invoking the GetPosition () of the event object.
Vetoing events
Sometimes we need to the stop processing an event. To does 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= (+)) Self . Bind (WX. Evt_close, self. OnClose) Self . Centre () Self . Show (True) def OnClose (Self, event): dial = wx. Messagedialog (None, ' 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 ()
Hopefully this article will help you with Python programming.