Custom widget doesn't move

563 views
Skip to first unread message

Damien Frikha

unread,
Jul 10, 2013, 10:59:34 AM7/10/13
to kivy-...@googlegroups.com
Hello there,

I am new to Kivy and I like it, but I am experiencing some troubles to achieve what I want.

I have drawn a Widget in python, which is some kind of ruler, with canvas and Labels, but once created (as a child or at the root it's the same) I am not able to move it or to edit any single attribute, like size, pos... It remains stuck at the pos and size i gave it in the class definition, whatever way i'm changing it.
I would have gladly written it in my kv file but as i needed to use loops (60 rectangles and labels to draw...) i had no other choice.

Here is my widget :
class AirspeedBar(Widget):
def __init__(self, **kwargs):
super(AirspeedBar, self).__init__(**kwargs)
self.size = (40,615)
with self.canvas:
Color(1,1,1)
for i in range(0,31):
Rectangle(pos=(self.x+25,self.y+i*20+4), size=(self.width-25, 2))
for i in range(0,30):
Rectangle(pos=(self.x+20,self.y+i*20+10+4), size=(self.width-20, 2))
for i in range(0,31):
self.add_widget(Label(text='%s' % int(i*20), pos=(self.x-37,self.y+i*20+4-48), font_size=10, bold=True))

I might have missed something somewhere but i searched in the doc and here and i didn't find any help.

Thanks

Akshay Arora

unread,
Jul 10, 2013, 2:09:19 PM7/10/13
to kivy-...@googlegroups.com
You need to maintain a list of graphics instructions in your canvas and the labels and update their pos and size whenever the widgets pos and size changes. i.e. bind a function to widgets size and pos and in that function update the pos and size of the canvas instructions and the labels.

Regards


--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Adam Pospíšil

unread,
Jul 10, 2013, 9:00:06 PM7/10/13
to kivy-...@googlegroups.com
This might be helpful:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Ellipse
from kivy.clock import Clock
from random import random

class CircleWidget(Widget):
    def __init__(self, **kwargs):
        Widget.__init__(self, **kwargs)
        self.size = (50,50)
        self.circle = Ellipse(pos = self.pos, size = self.size)
        self.canvas.add(self.circle)

    # handle position change
    def on_pos(self, obj, new_pos):
        self.circle.pos = new_pos # when widget moves, so does the graphic instruction

class RootWidget(Widget):
    def __init__(self, **kwargs):
        Widget.__init__(self, **kwargs)
        self.cw = CircleWidget()
        self.add_widget(self.cw)

    def update(self, dt):
        self.cw.pos = (random()*200, random()*200)

class MyApp(App):
    def build(self):
        rw = RootWidget()
        # call update() every second
        Clock.schedule_interval(rw.update, 1.0)
        return rw

MyApp().run()


also, check the Pong tutorial (http://kivy.org/docs/tutorials/pong.html)

Damien Frikha

unread,
Jul 11, 2013, 3:53:04 AM7/11/13
to kivy-...@googlegroups.com
Oh, and you can't just move the widget as a whole like you would move a *.kv-defined widget... I suppose then that these bindings are automatically made in the *.kv parser, am I right ?
Well, Adam's code is working fine for me, my question is answered, thank you both !

By the way, is this explained somewhere in the documentation ? 'Cause I searched it but didn't find anything.

ZenCODE

unread,
Jul 11, 2013, 1:03:50 PM7/11/13
to kivy-...@googlegroups.com
It seems to be pretty well covered here: http://kivy.org/docs/guide/widgets.html

"Unfortunately, this will only draw a rectangle at the layout’s initial position and size. To make sure the rect is drawn inside the layout, when layout size/pos changes, we need to listen to any changes and update the rectangle size and pos like so:"

Thanks for trying to point it out though ;-)

Peace out
Message has been deleted

Adam Pospíšil

unread,
Jul 11, 2013, 6:05:36 PM7/11/13
to kivy-...@googlegroups.com
Might be worth adding that bit to http://kivy.org/docs/api-kivy.graphics.html#the-basics - that's where I was looking for information, when trying to figure that one out...

ZenCODE

unread,
Jul 12, 2013, 2:26:41 AM7/12/13
to kivy-...@googlegroups.com
Thanks for the suggestion. I'll look into putting a note there ;-)

Peace out

ZenCODE

unread,
Jul 16, 2013, 5:26:14 PM7/16/13
to kivy-...@googlegroups.com
Hi

Have added a warning to the graphics docs as suggested  ;-)

https://github.com/kivy/kivy/pull/1375

Thanks

Peace out

Chris Hyde

unread,
Jul 25, 2013, 6:27:04 AM7/25/13
to kivy-...@googlegroups.com
ADAM!

Thanks so much for that code sample!  I was looking for this concept for so long!  I kept thinking I needed to bind the actual canvas to the widget size/pos.
Reply all
Reply to author
Forward
0 new messages