Getting x,y coordinates from breadcrumb widgets on a FloatLayout

36 views
Skip to first unread message

David Taylor

unread,
May 1, 2015, 8:50:43 PM5/1/15
to kivy-...@googlegroups.com
Hi all,
I am creating widgets on-the-fly using Factory.MyWidget() and positioning them on a FloatLayout using pos_hint
These widgets are small circular images used as breadcrumbs as I fly across a terrain.
I want to connect these breadcrumbs with lines.
Line: takes a list of pixel coordinates (and not a pos_hint) so I need to get the actual x and y coordinates of the breadcrumb images.

I can easily get the x and y from image widgets that are explicitly defined in the .kv file by using their id
For example:
self.rStartx = self.ids.GraphicStart.x + self.ids.GraphicStart.width / 2

...but I can't get them for the widgets created on-the-fly.

When I create my breadcrumbs I give them a unique id using the date and time:
        MyImageWidget = Factory.MyImage()
        MyImageWidget.id = time.strftime("%Y%m%d%H%M%S")
        ...
        self.ids.MainFloatLayout.add_widget(MyImageWidget,3) 
        self.WidgetListImage.append(MyImageWidget)


Then I would expect to be able to get the x and y coordinates like this:
        for breadcrumb in self.WidgetListImage:
            pointx = self.ids[breadcrumb.id].x
            pointy = self.ids[breadcrumb.id].y

and from this, construct a list of tuples to build the line.

The problem is that self.ids[breadcrumb.id].x throws a KeyError exception

   File "main.py", line 3013, in updateGFD
     pointx = self.ids[breadcrumb.id].x
 KeyError: '20150502014155'

I've been wrestling with this problem for hours. Can anyone show me where I am going wrong???

Thanks in advance,
Dave





Alexander Taylor

unread,
May 2, 2015, 9:20:58 AM5/2/15
to kivy-...@googlegroups.com
The ids dictionary is for accessing widgets defined in kv, it isn't automatically updated just because you set the id attribute of your widget. You could update it manually, or store the information in any other data structure.

David Taylor

unread,
May 3, 2015, 12:20:10 PM5/3/15
to kivy-...@googlegroups.com
Thanks Alexander, That explains it.
Is there any way to get feedback from a factory created widget that was placed using pos_hint as to what it's x & y coordinates are?

Thanks,
Dave

David Taylor

unread,
May 3, 2015, 5:32:50 PM5/3/15
to kivy-...@googlegroups.com
I've solved it.
The problem was that I was setting the new position using pos_hint then reading .x and .y afterwards expecting to get the new x,y coordinates but actually only getting the pos_hint position that I had only just set.
What I needed to do was read the widget's .x and .y BEFORE setting its new pos_hint values, that way I had allowed kivy to complete a graphic update cycle.
We learn some new kivy every day...
Thanks,
Dave
Reply all
Reply to author
Forward
0 new messages