简体   繁体   中英

wxPython method to handle EVT_MOVE event

I am trying to learn wxPython and have come across the following code (when the user moves the window it prints their current window position):

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((250, 180)) 
      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() 

My problem specifically is with the OnMove method, I understand that it is used to handle EVT_MOVE events but where is the parameter e coming from? What object is this?

It is the event that you bound to, in this case EVT_MOVE .

In essence you used self.Bind(wx.EVT_MOVE, self.OnMove) to bind the EVT_MOVE event of the Frame (self) to the execution of the function OnMove . The result being that whenever the event is fired, ie the frame is moved, the code in OnMove is executed.

See: https://wxpython.org/Phoenix/docs/html/events_overview.html

if you altered your code to be:

   def OnMove(self, event): 
      x, y = event.GetPosition() 
      print "current window position x = ",x," y= ",y 

it would be more obvious.

Bind(self, event, handler, source=None, id=wx.ID_ANY, id2=wx.ID_ANY) Bind an event to an event handler.

Parameters:
event – One of the EVT_ event binder objects that specifies the type of event to bind.

handler – A callable object to be invoked when the event is delivered to self. Pass None to disconnect an event handler.

source – Sometimes the event originates from a different window than self, but you still want to catch it in self. (For example, a button event delivered to a frame.) By passing the source of the event, the event handling system is able to differentiate between the same event type from different controls.

id – Used to specify the event source by ID instead of instance.

id2 – Used when it is desirable to bind a handler to a range of IDs, such as with EVT_MENU_RANGE.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM