How to add circle in kv from python

1,435 views
Skip to first unread message

Luciano Strassacappa

unread,
Jan 15, 2014, 10:30:48 AM1/15/14
to kivy-...@googlegroups.com
I need some help...

I need to draw circles dynamically from Python..

In pure Python: XXX.canvas.add(Line(circle=(X, Y, r))).....but how to add the same circle in a structure already built in a kv language?

Thanks!



Alexander Taylor

unread,
Jan 15, 2014, 1:31:52 PM1/15/14
to kivy-...@googlegroups.com
It sounds like you need a reference to your kv language widget. You can do that with ids (widgets can access ids defined in their kv language rules via self.ids.id_name or self.ids['id_name']), or by linking that widget to an ObjectProperty that you reference in python.

ZenCODE

unread,
Jan 15, 2014, 3:12:32 PM1/15/14
to kivy-...@googlegroups.com
This is how we drew our ovals in Kv, circles would be the same. It draws the fill, then the outline. The "theme" module just defines lists of [r, g, b ,a] values.

#:import theme widgets.theme
<CAMIKeyHintLabel>:
    canvas
.before:
       
Clear
       
Color:
            rgba
: theme.ButtonHotkeyColor
       
Ellipse:
            pos
: self.pos
            size
: self.size
       
Color:
            rgba
: theme.ButtonBorderColor
       
Line:
            ellipse
: self.pos[0], self.pos[1], self.size[0], self.size[1]


Luciano Strassacappa

unread,
Jan 16, 2014, 11:24:22 AM1/16/14
to kivy-...@googlegroups.com
I didn´t understand...something is wrong. Example:

The Python file:
from kivy.app import App
from kivy.uix.scatter import Scatter
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.properties import ListProperty, ObjectProperty, StringProperty, DictProperty

class Second(RelativeLayout):
    ptos
= ObjectProperty([])
    ptos
= [0,0,1000,0,1000,500,0,500]

class First(BoxLayout):
   
pass

class TestApp(App):
   
def build(self):
       
return First()

if __name__ == "__main__":
   
TestApp().run()


And Kivy file:
<Second>:
    Scatter:
        do_rotation: False
        canvas:
            Color:
                rgba: 0,0,1,1
            Line:
                points: root.ptos
                close: True
            Line:
                circle: 50,50,15
<First>:
    orientation: 'vertical'
    Button:
        size_hint: 1, 0.1
        text: 'testando 123'
    BoxLayout:
        orientation: 'horizontal'
        Button:
            size_hint: 0.1, 1
            text: 'funciona'
        Second:
            pos: self.pos
        Button:
            size_hint: 0.1, 1
            text: 'qwerty'
    Button:
        size_hint: 1, 0.1
        text: 'inferior'

For me it´s clear when to use root.variable. What I´m trying to do is to add some circle from Python because the number of circles, position (X, Y) and diameter are variables...

How to correctly reference the Second widget and add some circle from Python.

The id: is only for reference inside de widget, and I couldn´t use self.ids inside the Second class.

Noob Saibot

unread,
Jan 17, 2014, 3:39:33 PM1/17/14
to kivy-...@googlegroups.com
If you want First to reference Second, and vice-versa, the best way to do that is to add a property to something they have in common. In this case, the thing they have in common is the App class. So you could do this:

class Second(RelativeLayout):
    ptos
= ListProperty([0, 0, 1000, 0, 1000, 500, 0, 500]) #This is the proper way

class First(RelativeLayout):
    second_widget
= ObjectProperty(None)

class TestApp(App):
    first
= ObjectProperty(First())
    second
= ObjectProperty(Second())

   
def build(self):
       
return self.first

kv
= """
<First>:
    second_widget: app.second
    ...
"""
Reply all
Reply to author
Forward
0 new messages