Sam
Sam

Reputation: 53

it's possible put a variable then deal with ids in kivy and python?

This is a simple code just to understand. Imagine in kivy file I have three labels with different id like below code:

Label:
   id: L1
   text:"ex1"
Label:
   id: L2
   text:"ex2"
Label:
   id: L3
   text:"ex3"
Button:
   text:"ok"
   on_relase:
      root.btn_ok()

I want when click on Button "ok" [ call btn_ok function ] the text of label update to " Label update "

Python file

class a (App):
   steps=0
   def btn_ok(self):
      y=self.steps + 1
      update="L"+str(y)
      self.ids.update.text=" Label updated "

problem is that how I can put variable after .ids ex[ self.ids.variable ] it is possible ? what is the correct way.

Upvotes: 0

Views: 437

Answers (1)

John Anderson
John Anderson

Reputation: 39082

Yes, you can do that. You only need to use the ids[update] format instead of ids.update. Something like this:

   def btn_ok(self):
      self.steps += 1
      update="L"+str(self.steps)
      self.root.ids[update].text=" Label updated "

Upvotes: 1

Related Questions