Pavan
Pavan

Reputation: 391

How to make a label as button in kivy

I want to make a label behave as button and code for it in .kv file. I referred to the documentation, in which it was mentioned we have to use @ in the rule to inherit properties of button behavior. I tried the following code but it is giving errors. kindly help

tut12.py

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

class Box_3(BoxLayout):
    pass

class Demo_11(App):

    def build(self):
        return Builder.load_file("kv\Design10.kv")

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

Design10.kv

<MYLabel@ButtonBehavior + Label>:

Box_3:

    MYLabel:
        text: 'Hi'
        on_press: self.text = "hello"

The error msg is:

cls = Factory_get(cname) File "C:\Users\pavan m sunder\envs\kivy\lib\site-packages\kivy\factory.py", line 153, in getattr rootwidgets.append(Factory.get(basecls)) File "C:\Users\pavan m sunder\envs\kivy\lib\site-packages\kivy\factory.py", line 131, in getattr raise FactoryException('Unknown class <%s>' % name)

kivy.factory.FactoryException: Unknown class <ButtonBehavior>

Upvotes: 0

Views: 1424

Answers (1)

John Anderson
John Anderson

Reputation: 38822

Eliminate the blank spaces in

<MYLabel@ButtonBehavior + Label>:

to make it:

<MYLabel@ButtonBehavior+Label>:

Odd that spaces matter, but here they do.

Upvotes: 2

Related Questions