如何更新按钮中的文本值

How to update a text value in a button

我是 python 和 kivy 的新手,我无法显示变量的更新值

Class DButton(Button):
    def on_press(self):
        MyApka.moneyy = MyApka.moneyy - 10
        Button(text=str(MyApka.moneyy))
        if(MyApka.moneyy < 10):
            print('you dont have money')

目前只显示起始值100。 有一个代码,当我单击时,我将“MyApka.moneyy”的值减少 10,我希望在每次单击时显示和更新此变量“MyApka.moneyy”。

class MyApka(App):
    #moneyy = StringProperty('100')
    moneyy = 100     
    

    def build(self):        
        self.load_kv('my.kv')
        return
<MyTApka>
      DButton:
            size_hint_x: 0.1
            size_hint_y: 0.1
            pos_hint: {'right': 0.3}
            text: str(app.moneyy)

如果您将 moneyy 属性设置为 Property,当您更改 moneyy 值时,Button 将自动更新。

因此,在您的 App 代码中:

class MyApka(App):
    moneyy = NumericProperty(100)
    # moneyy = 100

然后您的 DButton class 可以通过访问 App:

修改 Property
class DButton(Button):
    def on_press(self):
        app = App.get_running_app()
        app.moneyy = app.moneyy - 10
        # Button(text=str(MyApka.moneyy))
        if(app.moneyy < 10):
            print('you dont have money')