Search code examples
pythonpython-3.xvisual-studio-codekivykivy-language

Label text changing with If condition on Kivy 2.1.0


I am new here. I have a question. I want to change the label text with if condition in kv. file, it must be changed the text according to button pressed. But I receive an error.

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.lang import Builder

kv ='''
BoxLayout:
    Label:
        id:counter_text
        text:'0'
    Label:
        text: 'Number increases' if myswitch.on_press else 'number added 1'
    Button:
        id:myswitch
        text:'COUNTER'
        on_press:app.increment()
        
    Button:
        text:'RESET'
        on_press:app.reset()
'''

class CounterApp(App):
    counter = 0
    def build(self):
        return Builder.load_string(kv)

    def increment(self):
        self.counter += 1
        print(self.counter)
        self.root.ids.counter_text.text= str(self.counter)

    def reset(self):
        self.counter = 0
        print(self.counter)
        self.root.ids.counter_text.text = str(self.counter)

CounterApp().run()

error output is here.

Exception has occurred: TypeError
call_fn() missing 1 required positional argument: 'v'
  File "C:\...\counttest.py", line 40, in <module>
    CounterApp().run()

Does anybody have any idea for the solution? thank you.


Solution

  • I think that will help you:

    Label:
        text: "Number increases" if myswitch.state =="down" else "number added 1"