Event handling for widgets

7 views
Skip to first unread message

Aras

unread,
Nov 3, 2010, 4:03:53 AM11/3/10
to pymt-dev
Hi,
I am quiet confused about event handling. I am trying to create a
bunch of individual objects called "Item" and place them in an
"MTScatter". I want to handle events such as touch_down or touch_move
separately for each Item. So I defined event handler functions inside
the Item class. However, now all I get is the touch_up event and it
only seems to effect the last Item object I create. Whats more
confusing is that the touch_up seems to be fired wherever I click
(even on the scatter plane). But I cant seem to be able to capture the
other events at all.

Here is a link to a stripped down version of my code:
http://web.uvic.ca/~arasbm/EventTest.py
This code is basically "trying" to capture the touch_down" event on
each Item and change its color. Obviously it is failing right now.
Please try running it to see for yourself.

Please let me know if you need more information or want me to explain
more about my problem.

Thanks,
Aras

Mathieu Virbel

unread,
Nov 3, 2010, 9:55:32 AM11/3/10
to pymt...@googlegroups.com
http://pymt.eu/wiki/DevGuide/EventDispatching

You have a little information about how working the event dispatching.

Everytime you are inherith from a MTWidget, and you want to take
on_touch_down() event, you must know if you want to call the parent
on_touch_down or not.

The MTWidget on_touch*, on_draw, on_update is dispatched accross
children + on_touch* can grab the event.
Maybe you miss to call:

class Item(...):
...
def on_touch_down(self, touch):
if not super(Item, self).on_touch_down(touch):
# not for me
return False
# got it
return True


+ ensure you are doing collision detection if you are not using super()


2010/11/3 Aras <ara...@gmail.com>:

> --
> You received this message because you are subscribed to the Google Groups "pymt-dev" group.
> To post to this group, send email to pymt...@googlegroups.com.
> To unsubscribe from this group, send email to pymt-dev+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/pymt-dev?hl=en.
>
>

Aras Balali Moghaddam

unread,
Nov 3, 2010, 2:36:29 PM11/3/10
to pymt...@googlegroups.com
Mathieu,
Is it possible that my Item location does not match its event handling location? For example I do:
        self.item1 = Item(name = "Item One", value = 50, pos = (100, 50), size = (20,100))
Then I add widgets to the item. And then I handle touch up event like you said:

def on_touch_up(self, touch):
        if not super(Item, self).on_touch_up(touch):
            print "touch up but not me"
            return False
        else:
            print "For ME!"
            if self.selected:
                self.selected = False
            else:
                self.selected = True
            return True

But there seem to be an offset. I touch the rectangle I draw inside the object and nothing happens. But if I touch an specific area outside the widget then the touch up event gets fired. Do you know what could cause this offset?

Thank you,
Aras

Aras Balali Moghaddam

unread,
Nov 4, 2010, 2:53:08 AM11/4/10
to pymt...@googlegroups.com
Hi,
I found what was wrong. The issue is entirely with my code. I made some stupid assumptions about positioning. Now I understand how PyMT position widgets relative to their parent. It makes so much more sense now. I think it is worth while to have a little paragraph in the documentation explaining how relative positioning works, just to help out newbies like me :)

Thanks again for your help Mathieu,
Aras

Mathieu Virbel

unread,
Nov 4, 2010, 10:24:28 AM11/4/10
to pymt...@googlegroups.com
Position are relative only for MTList and MTScatter.
Both have their own transformation matrix, so their own coordinate space.

So the position "restart" from 0, 0.

2010/11/4 Aras Balali Moghaddam <ara...@uvic.ca>:

Reply all
Reply to author
Forward
0 new messages