really simple question from newbie - change button text on press

1,667 views
Skip to first unread message

MFD

unread,
Jan 14, 2016, 4:17:17 PM1/14/16
to Kivy users support
I'm trying to learn Kivy and am still relatively new to python. I'm trying to just make the text on a button change after it's pressed. I found this old entry


but the code doesn't work. 

I feel like the original line the responder suggested should be in there

a.bind(on_press=lambda *args: args[0].text = 'Opened')

At least something that tells the text to change.

Here is the code:

#button press

import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label

class MyApp(App):
def build(self):
a = Button(text='unpressed')

a.bind(on_press=lambda *args: (args[0].setter('text'))

return a

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

I get:

SyntaxError: invalid syntax
user$ kivy button_press.py 
  File "button_press.py", line 14
    return a
         ^

SyntaxError: invalid syntax


Thank you!!

ZenCODE

unread,
Jan 15, 2016, 1:23:38 AM1/15/16
to Kivy users support
Think you have a tabs and spaces/indentation there issue. Either way, this is what I would suggest :-)


import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label

class MyApp(App):
   
def build(self):
        a
= Button(text='unpressed')


       
def change_text(button):
            button
.text = 'Changed'

        a
.bind(on_press=change_text)
Reply all
Reply to author
Forward
0 new messages