How to create a dynamic widget in canvas?

22 views
Skip to first unread message

Prabhat K.C.

unread,
Feb 21, 2020, 12:13:11 PM2/21/20
to Kivy users support
I am trying to make a rectangle that would enlarge or shrink at the run time. Either by taking a value from a slider, or manual input. I am not quite sure what to do. I would appreciate if someone guided me what to do with my Python file, and my Kivy file.

Prabhat K.C.

unread,
Feb 21, 2020, 12:25:19 PM2/21/20
to Kivy users support
<InterLength>
FloatLayout:
canvas:

Rectangle:
size: self.size
pos: self.pos
source: 'bgnd.jpg'
Color:
rgba: 0,0.5,0.5,1
Rectangle:
id: rect1
size: (15,50)
pos: (50,450)
Color:
rgba: 0.2,1,0.2,1
Rectangle:
id: rect2
size: (105,50)
pos:(50,250)
FloatLayout:
Slider:
id: slider
min: 0
max: 50
step: 1
orientation: 'horizontal'
pos_hint: {"x": 0.004, "top": 0.73}

Label:
text: str(slider.value)

Elliot Garbus

unread,
Feb 21, 2020, 2:20:42 PM2/21/20
to kivy-...@googlegroups.com

There are a number of things gong on here that are not quite correct. 

You have a label in a slider.  You have assigned ids to drawing instructions in the canvas.  Ids are for widgets.

 

I put the canvas in the Slider widget and use self.value to change the size of the drawn objects.

 

from kivy.app import App
from kivy.lang import Builder

kv =
"""
<InterLength@BoxLayout>:
    orientation: 'vertical'
    canvas:
        Color:
            rgba: .5, .5, .5, 1

        Rectangle:
            size: self.size
            pos: self.pos
            source: 'bgnd.jpg'
    Slider:
        size_hint_y: .25

        id: slider
        min: 0
        max: 50
        step: 1
        orientation: 'horizontal'
        canvas:
            Color:
                rgba: 0,0.5,0.5,1
            Rectangle:
                size: (15 + self.value * 20, 50)
                pos: (50 ,450)
            Color:
                rgba: 0.2,1,0.2,1
            Rectangle:
                size: (105, 50 + self.value * 5)
                pos:(50,250)
    Label:
        text: str(slider.value)
        font_size: 40
    Label:
        text: 'Slider Value Test'
                   
   
InterLength:

"""


class SizeBoxApp(App):
   
def build(self):
       
return Builder.load_string(kv)


SizeBoxApp().run()

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/a9f3b420-65a2-4980-920d-4e5a31ce8fdd%40googlegroups.com.

 

Prabhat K.C.

unread,
Mar 4, 2020, 9:49:15 PM3/4/20
to Kivy users support
Late thank you, had to get away from my laptop for a bit. but much appreciated.
Reply all
Reply to author
Forward
0 new messages