how to access the ids in the custom widget

105 views
Skip to first unread message

husam alkdary

unread,
May 22, 2020, 8:59:12 AM5/22/20
to Kivy users support
the code below contain  custom button and custom label I want to use display the button text on the custom label I'm using this 

on_press: MyCustomlabel_id.text = self.text

I'm getting this error

NameError: name 'MyCustomlabel_id' is not defined

I tried to print app.root.ids but o get {} nothing so it's possible to access the ids of the custom widget

<Custombutton@Button>:
 background_color: 0,1,0,1
 background_normal: ''
 bold: True
 color: 1,0,1,1
 on_press: MyCustomlabel_id.text = self.text



<MyCustomlabel@Label>:
 color: 1,0,0,1
 font_size: self.width * .1
 italic : True
 id: MyCustomlabel_id








BoxLayout:
 MyCustomlabel:
 id : MyCustomlabel_id
 Custombutton:
 text: 'text1'
 Custombutton:
 text: 'text2'
 Custombutton:
 text: 'text3'




Elliot Garbus

unread,
May 22, 2020, 10:13:04 AM5/22/20
to kivy-...@googlegroups.com

The id must refer to a widget instance.  You can also use them to refer to widgets within a compound widget.

Each kv-rule defines a different ids name space.

 

In the example below the class TwoLabels has two id’s, top and bottom.  An instance of TwoLabels is created and given the id two_labels.  The id dict of the two_labels instance is accessed and used to address the sub widgets in the class instance.

two_lables.ids.top.text

 

 

 

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


kv =
"""
<CustomButton@Button>:

    background_color: 0,1,0,1
    background_normal: ''
    bold: True
    color: 1,0,1,1

<MyCustomLabel@Label>:

    color: 1,0,0,1
    font_size: self.width * .1
    italic : True
    
<TwoLabels@BoxLayout>
    orientation: 'vertical'
    Label:
        id: top
        text: 'top: Initial text'
    Label:
        id: bottom
        text: 'bottom: Initial text'

BoxLayout:
    MyCustomLabel:
        id: mcl_1
    CustomButton:
        text: 'text 1'
        on_release: mcl_1.text = self.text
    CustomButton:
        text: 'text 2'
        on_release: mcl_2.text = self.text
    CustomButton:
        text: 'text 3'
        on_release: two_labels.ids.top.text = self.text
    CustomButton:
        text: 'text 4'
        on_release: two_labels.ids.bottom.text = self.text   
    TwoLabels:
        id: two_labels
    MyCustomLabel:
        id: mcl_2
   
        
"""

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

TestIDSApp().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/d0de7aac-2b39-4b6a-9ad9-c6b3814d69bd%40googlegroups.com.

 

Reply all
Reply to author
Forward
0 new messages