如何使用 .py 中的属性更新 .kv 中的标签文本?

How to update label text in .kv using properties in .py?

我想使用 .kv 中的按钮更新 class MainHero 中的值,并基于 [=] 中的属性 nick 38=] 我需要更新 .kv 中的标签。 我需要自动处理这个,因为许多其他属性会自动生成,所以 labels 需要更新。 这是我的部分代码:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import *
from kivy.lang import Builder
from kivy.clock import Clock


class MainHero():
     def __init__(self):
          self.nick = "Player"

class FightScreen(Screen):
    def __init__(self, **kwargs):
        super(FightScreen, self).__init__(**kwargs)
        self.af_init = Clock.create_trigger(self._init)
        self.af_init()

    def _init(self, dt):
         self.app = App.get_running_app()
    
    def rename(self):
        self.app.main_hero.nick = "Renamed"


class ScreenManagement(ScreenManager):
    pass



class Design(App):
    def __init__(self, **kwargs):
        super(Design, self).__init__(**kwargs)
        self.main_hero = MainHero() # Tried main_hero = MainHero() too
        self.val = StringProperty(self.main_hero.nick)  # string

    # Construct app
    def build(self):
        # design constructor
        kv = Builder.load_file('AppDesign.kv')
        return kv

if __name__ == "__main__":
    Design().run()
# Using kv 2.0
ScreenManagement:
    id: screen_manager
    FightScreen:
        name: 'FightScreen'
        manager: screen_manager
        id: fight_screen

<FightScreen>

FloatLayout:
     Label:
          text: app.val
          size_hint: .2, .2
          pos_hint: {'center_x': .5, 'center_y': .35}
     Button:
          size_hint: .2, .2
          pos_hint: {'center_x': .5, 'center_y': .5}
          on_press:
               root.rename()
          

通过properties/EventDispetcher可能是可能的。 在我尝试的许多情况下,引用 app.val 类似于 。 我尝试使用(可能是错误的)

class Aaa(EventDispetcher); apply_property(**kwargs) I want to do that with ObjectProperty too, it's different I think and I have no idea how to do it.

编辑:

     def rename(self):
          self.app.main_hero.nick = "Renamed"
          self.app.main_hero.num += 1
          self.app.val = self.app.main_hero  # not working need replace
          # probably something here (replacement)

class MainHero():
     def __init__(self):
          self.nick = "Player"
          self.num = 1
.
.
.
class Design(App):
     val = ObjectProperty()

     def __init__(self, **kwargs):
        super(Design, self).__init__(**kwargs)
        self.main_hero = MainHero() # Tried main_hero = MainHero() too
        self.val = self.main_hero  # object
.
.
.
Label:
     text: app.val.nick
     size_hint: .2, .2
     pos_hint: {'center_x': .5, 'center_y': .35}
Label:
     text: str(app.val.num)
     size_hint: .2, .2
     pos_hint: {'center_x': .7, 'center_y': .7}

您需要在kv文件中为label指定一个ID,将要更改文本的action引用到一个回调函数中,然后通过这种方式更改label的文本。

kv:

MDLabel:
        id: number
        text: "1"
        halign: "center"
        pos_hint: {"center_x": .5, "center_y": .5}

py:

class Screen2(Screen, n):
    def set_number(self):
        self.ids.number.text = f"{n}"

StringProperty 必须在 class 方法之外定义,如下所示:

class Design(App):
    val = StringProperty()  # defines the val property
    def __init__(self, **kwargs):
        super(Design, self).__init__(**kwargs)
        self.main_hero = MainHero()  # Tried main_hero = MainHero() too
        self.val = self.main_hero.nick  # set value of the property

然后,在您的 rename() 方法中,您只需更改 app.val

的值
def rename(self):
    self.app.main_hero.nick = "Renamed"
    self.app.val = self.app.main_hero.nick

如果要直接在kv中使用MainHeroclass中的值,它们必须是Properties。但是要使这些属性成为 PropertiesMainHero class 必须是 Widget 或至少是 EventDispatcher,像这样:

class MainHero(EventDispatcher):
    nick = StringProperty()
    num = NumericProperty()

    def __init__(self):
        self.nick = "Player"
        self.num = 1