[wxPython] (newbie) Button event question

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[wxPython] (newbie) Button event question

Robin Barendregt-2
Hi all,
 
I started playing around with wxPython last weekend (great stuff btw). After struggling a bit with the layout constraints, I looked around for an event to the wxBitmapButton that would be triggered as long as the button is pressed (like in a slider, but for my purpose it would scroll something as long as it's pressed). Where should I look?
Also I noticed that when I press on a button continuously, the button doesn't quite keep up with my speed of clicking. Is there a way to modify that behaviour?
 
Thanks in advance,
Robin.
Reply | Threaded
Open this post in threaded view
|

Re: [wxPython] (newbie) Button event question

Robin Dunn
> I started playing around with wxPython last weekend (great
> stuff btw). After struggling a bit with the layout constraints,
> I looked around for an event to the wxBitmapButton that would
> be triggered as long as the button is pressed (like in a slider,
> but for my purpose it would scroll something as long as it's
> pressed). Where should I look?
>

So what you want is a way to get a continuous stream of events while a
button is help down, right?  Well, there isn't anything built in to do that,
but it can be done.  You'll have to think of the button as it's wxWindow
base class and then it will make sense.  First break it down into the
component events.  You want something to start happening when the mouse
button is pressed down (EVT_LEFT_DOWN,) and you want it to stop when the
mouse button is released, (EVT_LEFT_UP.)  This is the easy part.

        btn = wxBitmapButton(self, mID, theBMP)
        EVT_LEFT_DOWN(btn, self.OnMouseDown)
        EVT_LEFT_UP(btn, self.OnMouseUp)
        EVT_BUTTON(self, mID, self.OnClick)

There are two ways to get arbitrary repeating events in wxPyton, timers and
idle events.  For this situation, I think a timer would be the best.  In
that case, here are the event hanndling mothods:


    def OnMouseDown(self, event):
        print 'OnMouseDown'
        self.timer = wxPyTimer(self.OnNotify)
        self.timer.Start(250)  # quarter of a second
        event.Skip()

    def OnMouseUp(self, event):
        print 'OnMouseUp'
        self.timer.Stop()
        del self.timer
        event.Skip()

    def OnClick(self, event):
        print 'OnClick'

    def OnNotify(self):
        print 'OnNotify'

Notice the event.Skip in the mouse down and up methods.  This is so the
button is still able to act like a button and send the click event.


> Also I noticed that when I press on a button continuously, the
> button doesn't quite keep up with my speed of clicking. Is
> there a way to modify that behaviour?

There is a fair about of overhead in dealing with Bitmap buttons, and from
what I've seen in the wxWindows code, most of it is in Microsoft code.
Can't do much about that.

--
Robin Dunn
Software Craftsman
[hidden email]
http://AllDunn.com/robin/
http://AllDunn.com/wxPython/  Check it out!




_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users