change label when pushing button

18 views
Skip to first unread message

Luigi Marongiu

unread,
Jul 2, 2019, 3:23:34 PM7/2/19
to Kivy users support
Hello,
Thanks to previous hints, I can now change the icon of a button upon clicking. The page I am building has a button and a label underneath. How can i change the text of the label upon clicking on the kv file? 

The python file is:
```
import kivy
kivy.require('1.10.1')
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.image import Image

class myLayout(BoxLayout):
    pass

class ImageButton(ButtonBehavior, Image):
    pass

class MyApp(App):
    def build(self):
        return myLayout()

if __name__ == "__main__":
    MyApp().run()
```

the kivy file is:
```
#:kivy 1.10.1
<myLayout@BoxLayout>:
  orientation: "vertical"
  ImageButton:
    size_hint_x: 1
    size_hint_y: 0.8
    source: './Actions-media-playback-pause-icon.png'
    on_release:
      self.source = './Actions-media-playback-start-icon.png'
    on_press:
      self.source = './Actions-media-playback-pause-icon.png'
  Label:
    size_hint_x: 1
    size_hint_y: 0.2
    text: "pause"
    on_press:
      text:"pause"
    on_release:
      text: "PLAY"
```
but when I run with python3 I get `AttributeError: 'weakref' object has no attribute 'cline_in_traceback'`
Thank you
Actions-media-playback-pause-icon.png
Actions-media-playback-start-icon.png

Elliot Garbus

unread,
Jul 2, 2019, 4:42:00 PM7/2/19
to kivy-...@googlegroups.com

You need to change self.text

 

  Label:

    size_hint_x: 1

    size_hint_y: 0.2

    text: "pause"

    on_press: self.text = "pause"

    on_release: self.text = "PLAY"

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To post to this group, send email to kivy-...@googlegroups.com.
Visit this group at https://groups.google.com/group/kivy-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/5cd680c9-766c-4f7a-8d5d-fe393898b036%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

 

Kuam

unread,
Jul 3, 2019, 10:35:09 AM7/3/19
to Kivy users support
First off, your Label is just that, a Label that displays text.  It won't have the on_press attribute built into it like a Button does, which is likely where your error is coming from.
Now you could potentially do an on_touch_down event, but that's probably more than you really need.  

If I'm understanding you correctly you want the Label text to change when the button is pressed?  One way to do that would be to give the Label an id and reference it that way, then just add another set of on_press/on_release to handle changing the text.  So your code would be something like this:

ImageButton:
    size_hint_x: 1
    size_hint_y: 0.8
    source: './Actions-media-playback-pause-icon.png'
    on_release:
      self.source = './Actions-media-playback-start-icon.png'
    on_press:
      self.source = './Actions-media-playback-pause-icon.png'
    on_press: playpauselabel.text = 'Pause'
    on_release: playpauselabel.text = 'Play'
Label:
    size_hint_x: 1
    size_hint_y: 0.2
    id: playpauselabel
    text: 'Play'

One other thing, your size hint for your widgets can just be in this format: size_hint: 1,.2   rather than doing both size_hint_x, size_hint_y....save you some typing :)

Elliot Garbus

unread,
Jul 3, 2019, 11:07:17 AM7/3/19
to kivy-...@googlegroups.com

Oops… of course Kuan is correct!

 

From: Kuam
Sent: Wednesday, July 3, 2019 7:35 AM
To: Kivy users support

--

You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To post to this group, send email to kivy-...@googlegroups.com.
Visit this group at https://groups.google.com/group/kivy-users.

Luigi Marongiu

unread,
Jul 3, 2019, 1:58:44 PM7/3/19
to kivy-...@googlegroups.com
Thank you, it worked!
> To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/5d1cc49f.1c69fb81.40a9b.167eSMTPIN_ADDED_MISSING%40gmr-mx.google.com.
> For more options, visit https://groups.google.com/d/optout.



--
Best regards,
Luigi

Prabhat Mohanty

unread,
Jul 4, 2019, 11:52:06 AM7/4/19
to Kivy users support
Give Label an id. then call by id and its text property.
Reply all
Reply to author
Forward
0 new messages