Manufacturing an event

In my program, several routines get called on a mouse left click on a
certain widget. However, it is also necessary for the same routines to get
called to initialize the display when the program starts. I could call the
event handlers manually, but I would need to pass an event. It looks as if
it is possible to manufacture an event, but I'm having trouble getting it
to work. When I tried

evt = wx.MouseEvent(wx.EVT_LEFT_DOWN)

I got the complaint that the argument is a PyEventBinder where a number is
expected. According to the documentation for MouseEvent, I am supposed to
pass "mouseType" to the constructor. One of the choices for mouseType is
"wxEVT_LEFT_DOWN". I translated that choice to wx.EVT_LEFT_DOWN as the
original is not defined, but that guess was obviously wrong. What is the
correct parameter to put here -- or am I completely off course?

I am thinking that the most elegant approach, once I get the event
manufactured, is to call

self.GetEventHandler().ProcessEvent(evt)

next (self is a Panel) so that the program behaves exactly as it would on an
actual mouse left click.

···

--
Jeffrey Barish

There are a wide variety of solutions.

Firstly, you don't need to synthesize an event to call the event
handler directly - you simply need to construct an object that quacks
sufficently to pretend it's an event. In other words, as long as it
has the methods and properties you access from within the event
handler, Python won't care if it's a wx.Event or not.

Now, if you want to synthesize the event and post it to the control
and let the normal event plumbing happen, you can do that (but I don't
suggest it unless you're creating a framework and using custom events,
which you can synthesize easily anyway), but it's going to be slower
(event overhead, plus at least 2 levels of C++/Python marshalling) and
as you see it's more annoying.

You can also factor out the meat code into common methods and make
your event handlers one liners (perhaps even lambdas) and call those
common methods instead.

···

On 1/19/06, Jeffrey Barish <jeff_barish@earthlink.net> wrote:

In my program, several routines get called on a mouse left click on a
certain widget. However, it is also necessary for the same routines to get
called to initialize the display when the program starts. I could call the
event handlers manually, but I would need to pass an event. It looks as if
it is possible to manufacture an event, but I'm having trouble getting it
to work. When I tried

evt = wx.MouseEvent(wx.EVT_LEFT_DOWN)

I got the complaint that the argument is a PyEventBinder where a number is
expected. According to the documentation for MouseEvent, I am supposed to
pass "mouseType" to the constructor. One of the choices for mouseType is
"wxEVT_LEFT_DOWN". I translated that choice to wx.EVT_LEFT_DOWN as the
original is not defined, but that guess was obviously wrong. What is the
correct parameter to put here -- or am I completely off course?

I am thinking that the most elegant approach, once I get the event
manufactured, is to call

self.GetEventHandler().ProcessEvent(evt)

next (self is a Panel) so that the program behaves exactly as it would on an
actual mouse left click.
--
Jeffrey Barish

There's an even simpler way. Just pass the event as 'None'.

Example, a typical event handler routine:

    def OnMouseEvent(self, event):

or:

def OnMouseEvent(self,event=None)

Just call it any time you want:

    OnMouseEvent(None) # Pretend someone clicked the mouse

or OnMouseEvent()

Stani

···

On 1/20/06, Michael Hipp <Michael@hipp.com> wrote:

--

http://pythonide.stani.be/screenshots
http://pythonide.stani.be/manual/html/manual.html

Or change the routines so that the event parameter is optional?

···

On 2006-01-20, Don Dwiggins <ddwiggins@advpubtech.com> wrote:

Jeffrey Barish wrote:

In my program, several routines get called on a mouse left click on a
certain widget. However, it is also necessary for the same routines to get
called to initialize the display when the program starts.

I may be missing something here; why not just factor out the routines
into a normal method, then call that method both from the initialization
code and from the mouse event handler? That way you don't need to mess
around trying to simulate a click. (Although it could be fun figuring
out how to do it. 8^)

--
Grant Edwards grante Yow! My mind is making
                                  at ashtrays in Dayton....
                               visi.com