Josiah Survance
Josiah Survance

Reputation: 15

How to update button text after pressing button in python?

I'm trying to make an app that will eventually be able to score cards. One problem I'm trying to solve is how to update the text of a button after pressing or updating it somehow. I'm newer to python/kivy. I've found a couple similar questions, dealing with functions and such, but none dealt with .kv files and separate windows. My code is below.

main.py

import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen

class MainWindow(Screen):
    pass

class ScoreWindow(Screen):
    pass

class WindowManager(ScreenManager):
    pass

kv = Builder.load_file("mainkv.kv")


class MyMainApp(App):
    def build(self):
        return kv

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

mainkv.kv

WindowManager:
    MainWindow:
    ScoreWindow:

<MainWindow>:
    name: "main"

    GridLayout:
        cols: 1

        Button:
            text: "Quick Score"
            on_release:
                app.root.current = "score"
                root.manager.transition.direction = "left"

<ScoreWindow>:
    name: "score"

    GridLayout:
        cols: 7
        Button:
            name: "three"
            text: "Press to Update"
            on_release:
                three.text = "Updated"

        Button:
            text: ""
        Button:
            text: ""
        Button:
            text: ""
        Button:
            text: ""
        Button:
            text: ""
        Button:
            text: "Score"
            on_release:
                app.root.current = "main"
                root.manager.transition.direction = "right"
        Button:
            text: ""
        Button:
            text: ""
        Button:
            text: ""
        Button:
            text: ""
        Button:
            text: ""
        Button:
            text: ""
        Button:
            text: "Go Back"
            on_release:
                app.root.current = "main"
                root.manager.transition.direction = "right"

Upvotes: 1

Views: 47

Answers (1)

Erik
Erik

Reputation: 1226

Your Buttons can refer to themselves in kvlang using the self keyword. So just add another line that says self.text = 'foo' in the on_release: area of your Buttons.

Upvotes: 1

Related Questions