Reputation: 717
I've run into this problem several times, so either my approach is wrong or I missed a critical feature.
Frequently, I end up needing to set initial graphics information in a widgets init method. This doesn't work because when kivy graphics are first created they are at pos:0, 0
and size:100, 100
. So whatever setup in init doesn't look correct.
Is there a way to force kivy to layout in a widgets init? I see the do_layout
, but haven't figured out how to get to the layout to try it.
Here is an example:
Python:
class Main(Widget):
route = ListProperty([])
def __init__(self, *args, **kwargs):
super(Main, self).__init__(**kwargs)
self.route = [0, 0, self.width, self.height]
class RouteBuilderApp(App):
def build(self):
main = Main()
return main
if __name__ == "__main__":
RouteBuilderApp().run()
Kv:
<Main>
canvas:
Color:
rgb: 0, 0, 0
Rectangle:
size: self.size
pos: self.pos
Color:
rgb: 0, 0, 1
Line:
points: root.route
width: 2
Output:
Upvotes: 0
Views: 388
Reputation: 14794
You need to use property binding. This is super simple in kv language, you can just change your kv like so:
canvas:
Color:
rgb: 0, 0, 0
Rectangle:
size: self.size
pos: self.pos
Color:
rgb: 0, 0, 1
Line:
points: 0, 0, root.width, root.height
width: 2
By binding the points of the Line
directly, you don't even need the intermediate route
property.
Upvotes: 1