Wxpython Event Handling

Source: Internet
Author: User

Http://www.yiibai.com/wxpython/wxpython_event_handling.html

Unlike a console-mode application, a GUI-based application is event-driven, which is executed in a sequential fashion. A function or method responds to a button like a click, from a collection or mouse click, and so on, invokes the event to select the item handler function, and the user's action is executed.

Data about an event that occurs during the runtime of an application is stored as from WX. The object of the subclass that the event derives from. A display control (such as a button) is the source of a particular type of event and produces an object with its associated event class. For example, click a button to issue the Wx.commandevent event. The event data is dispatched to the method of program event handling. There are many pre-defined event binders in the Wxpython. An event binding encapsulates the relationship between a specific widget (control), its associated event type, and an event-handling method.

For example, to invoke the OnClick () method on the program of a button's click event, the following statement is required-
Self.b1.Bind (Evt_button, OnClick)

The bind () method is passed from WX. All display object inheritance for the Evthandler class. Evt_. button here is the OnClick () method of the binder, which associates the button click event.

Instance

In the following example, the Moveevent event is caused by dragging the top-level window – in this case, a wx. The frame object is connected to the OnMove () method using the Wx.evt_move binder. The code displays a window. If you use the mouse to move, its instantaneous coordinates are displayed on the console.

Import WX  class Example (WX. Frame):                def __init__ (self, *args, **kw):       super ("Example, Self"). __init__ (*args, **kw) self        . Initui ()               def initui (self): self       . Bind (WX. Evt_move, self. OnMove) Self       . SetSize (()) Self       . Settitle (' Move event ') Self       . Centre () Self       . Show (True)      def OnMove (self, E):       x, y = e.getposition ()       print "Current window position x =", X, "y=", y
   
    ex = wx. APP () Example (None) ex. Mainloop ()   
   
The above code produces the following output-

Current Window Position x = 562 y = 309

Current Window Position x = 562 y = 309

Current Window Position x = 326 y = 304

Current Window Position x = 384 y = 240

Current Window Position x = 173 y = 408

Current window Position x = 226 y = 30

Current window Position x = 481 y = 80

Some from WX. The event inheritance subclass is listed in the following table-
S.N. Events and Descriptions
1

Wxkeyevent

Occurs when a key is pressed or released
2

Wxpaintevent

Generated when a window's content needs to be redrawn
3

Wxmouseevent

Contains information about the event due to mouse activity, such as pressing the mouse button or dragging
4

Wxscrollevent

Associated like Wxscrollbar and wxslider scrolling control
5

Wxcommandevent

Contains event data from many artifacts, such as buttons, dialogs, clipboard, etc.
6

Wxmenuevent

Different menu-related events but not including menu command button clicks
7

Wxcolourpickerevent

Events generated by Wxcolourpickerctrl
8

Wxdirfilepickerevent

Events generated through FileDialog and Dirdialog

There are two types of events in Wxpython. Basic events and command events. A basic event stays in the window where it originated. Most wxwidgets generate command events. Command events can propagate to one or more windows, and the class hierarchy comes from above the window.

Example below is a simple example of event propagation. The complete code is  -
 Import WX class Mypanel (WX. Panel): Def __init__ (self, parent): Super (Mypanel, self). __init__ (parent) b = wx. Button (self, label = ' Btn ', pos = (100,100)) B.bind (WX. Evt_button, SELF.BTNCLK) self. Bind (WX. Evt_button, self. onbuttonclicked) def onbuttonclicked (self, e): print ' Panel received click event. Propagated to Frame class ' E.skip () def btnclk (self,e): Print "button received click event. Propagated to panel class "E.skip () class Example (WX. Frame): Def __init__ (self,parent): Super (Example, self). __init__ (parent) self. Initui () def initui (self): MPNL = Mypanel (self) self. Bind (WX. Evt_button, self. onbuttonclicked) self. Settitle (' Event propagation demo ') self. Centre () self. Show (True) def onbuttonclicked (self, e): print ' Click event received by Frame class ' E.skip () ex = W X.app () Example (None) ex. Mainloop ()   

There are two classes in the code above. Mypanel is a subclass of Wx.panel as well as an instance of WX. The frame subclass is the top-level window for the program. A button is placed on the panel.

This button object is bound to the event handler Btnclk (), which propagates to its parent class (Mypanel in this case). Click the button to generate a commandevent that can be propagated to its parent by the skip () method.

The Mypanel class object also binds the received event to another processing method, onbuttonclicked (). In turn, this function is passed to its parent, the example class. The above code produces the following output?

Button received Click event. Propagated to Panel class. Panel received Click event. Propagated to Frame class. Click event received by frame class.

Wxpython Event Handling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.