A noob question about kv language

21 views
Skip to first unread message

me2 beats

unread,
Aug 18, 2018, 4:10:55 AM8/18/18
to Kivy users support
I think it's a noob question, but I have not found a solution at the moment.

Suppose I have an app like this:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder

kv
= '''

<MyLayout>:
    canvas:
        Color
            rgb: (1, 0, 1)

        Rectangle
            pos: (self.pos[0]+10,self.pos[1]+10)
            size: (self.size[0]-20,self.size[1]-20)


BoxLayout
    MyLayout
        Label
            text:'
123'
    MyLayout
        Label
            text:'
456'

'''



class MyLayout(BoxLayout):
   
pass


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

TestApp().run()




Can I write in the kv something like:


<MyLayout>:
    canvas:
        Color
            rgb: (1, 0, 1)

        Rectangle
            pos: (self.pos[0]+10,self.pos[1]+10)
            size: (self.size[0]-20,self.size[1]-20)
        Label
            id:l

BoxLayout
    MyLayout
        l.text:'
123'
    MyLayout

        l.text:'456'



(not to write each time the word Label inside MyLayout)?

ZenCODE

unread,
Aug 18, 2018, 1:52:27 PM8/18/18
to Kivy users support
Not like that. The "id: l" just gives you a way to reference that instance of the widget. But here, each "MyLayout" has it's own instance of a label, so that won't work. But what you can do is give the "MyLayout a string property that the label reads from. Like so.

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder

kv = '''

<MyLayout>:
    text: ''
    canvas:
Color
rgb: (1, 0, 1)

Rectangle
pos: (self.pos[0]+10,self.pos[1]+10)
size: (self.size[0]-20,self.size[1]-20)

    Label:
text: root.text

BoxLayout
MyLayout
text:'123'
MyLayout

me2 beats

unread,
Aug 18, 2018, 3:09:17 PM8/18/18
to Kivy users support
Yeah, that's what I was looking for. Thanks, it works :)

суббота, 18 августа 2018 г., 22:52:27 UTC+5 пользователь ZenCODE написал:
Reply all
Reply to author
Forward
0 new messages