Kivy标签文本开始更新太晚



我想要的是显示按钮被按下了多少次的标签文本,但由于某种原因,它仅在我点击 2 后才开始工作,我似乎无法弄清楚为什么它在第一次按下按钮时没有更新。

这是我的python文件:

import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty
from kivy.clock import Clock
class Testime(Widget):
pass
class loendtest(App):
tulemus = NumericProperty()
loend = [0]
def update(self, *args):
self.loend = self.loend
def build(self):
Clock.schedule_interval(self.update, 0.2)
return Testime()
if __name__ == "__main__":
loendtest().run()

这是 kv:

<Testime>:
GridLayout:
cols:2
size: root.size
Button:
text: "lisa tulemus"
on_press:
app.tulemus += 1
app.loend.append(app.tulemus)
print(app.tulemus)
print(app.loend)

Label:
text: str(app.loend[app.tulemus-1]+1) if len(app.loend) > 1 else "0"

下面是您的代码的简化版本,可以执行您想要的操作。为了方便起见,我添加了kv作为Builder.load_string()。其他内容可以添加到代码中,但以下是满足您的要求所需的全部内容:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty
class Testime(Widget):
pass
Builder.load_string('''
<Testime>:
GridLayout:
cols:2
size: root.size
Button:
text: "lisa tulemus"
on_press:
app.tulemus += 1
Label:
text: str(app.tulemus)
''')
class loendtest(App):
tulemus = NumericProperty(0)
def build(self):
return Testime()
if __name__ == "__main__":
loendtest().run()

相关内容

  • 没有找到相关文章

最新更新