Kivy 标签未更新
Posted
技术标签:
【中文标题】Kivy 标签未更新【英文标题】:Kivy Label not updating 【发布时间】:2021-12-15 11:08:46 【问题描述】:所以我是 kivy 的新手,我构建了这个,但我不明白为什么它不会更新。打印时,列表正在更新,而不是标签本身。
main.py
class MasukTol(Screen):
ubahintsaldo = [str(int) for int in nyimpen.saldoawal]
textsaldo = ''.join(ubahintsaldo)
n = 0
def masukkansaldo(self):
self.instruksimasuk = "Silakan masukan \n saldo Anda!"
def updatesaldo(self):
self.ubahintsaldo = [str(int) for int in nyimpen.saldoawal]
self.ids.textsaldo = ''.join(str(nyimpen.saldoawal))
return self.ids.textsaldo, self.ubahintsaldo
def pencetsatu(self):
if self.n < 6:
nyimpen.saldoawal[self.n]=1
self.n +=1
self.updatesaldo()
print(nyimpen.saldoawal)
print(self.n)
return nyimpen.saldoawal, self.n
kv文件
<MasukTol>:
Label:
id: Saldo
text: root.textsaldo
# Nomor 1
Button:
background_normal: "photos/atm_1.png"
background_down: "photos/atm_down_1.png"
allow_stretch: True
keep_ratio: False
size_hint: 0.11, 0.141
pos_hint:"x":root.kiri_atm, "y":root.atas_atm
on_release:
root.pencetsatu()
我制作了一个单独的 python 文件来跟踪每件事。
nyimpen.py
saldoawal = [0,0,0,0,0,0]
【问题讨论】:
更好地创建我们可以简单地复制和运行的最小工作代码。int
是函数 int()
的保留名称,所以最好不要将其用作变量。
您需要将MasukTol.textsaldo
设置为kivy.properties.StringProperty
才能创建自动更新标签的绑定
【参考方案1】:
阿尔弗莱达
我稍微修改了你的代码:
alfreda.py
>from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivy.properties import ListProperty, StringProperty, NumericProperty
class nyimpen:
saldoawal = [0, 0, 0, 0, 0, 0]
class MasukTol(Screen):
counter = NumericProperty(0)
instruksimasuk = StringProperty()
ubahintsaldo = StringProperty()
saldo_value = StringProperty()
def __init__(self):
super(MasukTol, self).__init__()
self.ubahintsaldo = str([str(int) for int in nyimpen.saldoawal])
self.saldo_value = ''.join(self.ubahintsaldo)
def masukkansaldo(self):
self.instruksimasuk = "Silakan masukan \n saldo Anda!"
def updatesaldo(self):
self.ubahintsaldo = str([str(int) for int in nyimpen.saldoawal])
self.saldo_value = ''.join(str(nyimpen.saldoawal))
return self.saldo_value, self.ubahintsaldo
def pencetsatu(self):
if self.counter < 6:
nyimpen.saldoawal[self.counter] = 1
self.counter += 1
self.updatesaldo()
print(nyimpen.saldoawal)
print(self.counter)
return nyimpen.saldoawal, self.counter
def on_counter(self, *args):
print('counter', self.counter)
def on_ubahintsaldo(self, *args):
print('ubahintsaldo', self.ubahintsaldo)
def on_saldo_value(self, *args):
print('saldo_value', self.saldo_value)
class AlfredaApp(App):
def build(self):
Builder.load_file('./alfreda.kv')
return MasukTol()
pass
if __name__ == '__main__':
AlfredaApp().run()
pass
alfreda.kv
<MasukTol>:
id: masuk_tol
Label:
id: Saldo
text: root.saldo_value
# Nomor 1
Button:
# background_normal: "photos/atm_1.png"
# background_down: "photos/atm_down_1.png"
text: str(root.counter)
allow_stretch: True
keep_ratio: False
size_hint: 0.11, 0.141
# pos_hint:"x":root.kiri_atm, "y":root.atas_atm
on_release:
root.pencetsatu()
正如之前提到的:感谢 kivy StringProperty,代码现在可以达到您的预期。 https://kivy.org/doc/stable/api-kivy.properties.html
【讨论】:
以上是关于Kivy 标签未更新的主要内容,如果未能解决你的问题,请参考以下文章