Search code examples
pythonkivykivy-language

Python : How to set id of button


I am using python-2.7 and kivy-1.9.0. Can someone tell me how to set id of button? I am trying to set idtest of button using this code.

btn1 = Button(text="Close",id="test")

But it gives error 'Alert' object has no attribute 'test'

test.py

from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty

Window.size = (500, 150)


class Alert(Popup):

    def __init__(self, title, text):
        super(Alert, self).__init__()

        box = BoxLayout(orientation='vertical', padding=(5))
        box.add_widget(Label(text=text))
        btn1 = Button(text="Close",id="test")
        box.add_widget(btn1)

        self.title = title
        self.title_size = 30
        self.title_align = 'center'
        self.content = box
        self.size_hint = (None, None)
        self.size = (300, 200)
        self.auto_dismiss = False

        self.open()
        self.test.background_color = [0, 0, 1, 0.5]


class Test(App):
    def build(self):
        Alert(title='yeah!', text='inputs are invalid')
        return


if __name__ == '__main__':
    Test().run()

Solution

  • Define Button id

    You have the correct way of assinging an id to button in Python script.

    btn1 = Button(text="Close") 
    self.ids['test'] = btn1 
    

    Note

    The id declared in Python script is different from ids defined in kv file.

    Error

    The error that you have encountered is not because the button's id is not set correctly. The keyword self references the “current widget instance” i.e. Alert/Popup and it does not an attribute test.

       File ".../main.py", line 33, in __init__
         self.test.background_color = [0, 0, 1, 0.5]
     AttributeError: 'Alert' object has no attribute 'test'
    

    Solution

    When the button was defined, it was assigned to an object btn1. Therefore, if you want to change the button's background colour, use

    btn1.background_color or sef.ids.test.background_colour or self.ids['test'].background_color
    

    Example

    main.py

    from kivy.uix.screenmanager import Screen
    from kivy.app import App
    from kivy.lang import Builder
    from kivy.uix.popup import Popup
    from kivy.uix.label import Label
    from kivy.uix.button import Button
    from kivy.core.window import Window
    from kivy.uix.boxlayout import BoxLayout
    from kivy.properties import ObjectProperty
    
    Window.size = (500, 150)
    
    
    class Alert(Popup):
    
        def __init__(self, title, text):
            super(Alert, self).__init__()
    
            box = BoxLayout(orientation='vertical', padding=(5))
            box.add_widget(Label(text=text))
            btn1 = Button(text="Close")
            self.ids['test'] = btn1
            box.add_widget(btn1)
    
            self.title = title
            self.title_size = 30
            self.title_align = 'center'
            self.content = box
            self.size_hint = (None, None)
            self.size = (300, 200)
            self.auto_dismiss = False
    
            self.open()
            self.ids.test.background_color = [0, 0, 1, 0.5]
    
    
    class Test(App):
        def build(self):
            Alert(title='yeah!', text='inputs are invalid')
            return
    
    
    if __name__ == '__main__':
        Test().run()
    

    Output

    Img01