我想使用.kv中的按钮更新我的类MainHero
中的值,并且基于我的类中的属性nick
,我需要更新/kv中的标签。我需要自动处理这个问题,因为许多其他属性将自动生成,所以标签需要更新。这是我的部分代码:
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()
这可能是通过属性/EventDispetcher实现的。在我尝试过的许多情况下,引用app.val类似于<StringProperty名称=>。我试过使用(可能是错误的(
class Aaa(EventDispetcher)
;apply_property(**kwargs)
我也想用ObjectProperty
做到这一点,我认为这是不同的,我不知道如何做到。
编辑:
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}
StringProperty
必须在类方法之外定义,如下所示:
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
中直接使用MainHero
类中的值,则它们必须是Properties
。但是,要使这些属性成为Properties
,MainHero
类必须是Widget
或至少是EventDispatcher
,如下所示:
class MainHero(EventDispatcher):
nick = StringProperty()
num = NumericProperty()
def __init__(self):
self.nick = "Player"
self.num = 1
您需要在kv文件中为标签指定一个ID,并将要更改文本的操作引用到回调函数,然后以这种方式更改标签的文本。
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}"