我需要一次更新一组标签,但我还需要在功能完成之前查看更改的效果。期望的结果是一种加载条。
目前,我的代码在函数末尾一次性应用所有更改。
(简化了易于阅读的代码)
main.py
def TextAnimation(self):
#self.ids.??? are labels
self.ids.x1y1.text = "-"
self.ids.x2y1.text = "-"
self.ids.x3y1.text = "-"
self.ids.x1y1.texture_update()
self.ids.x2y1.texture_update()
self.ids.x3y1.texture_update()
time.sleep(0.2)
self.ids.x4y1.text = "-"
self.ids.x5y1.text = "-"
self.ids.x6y1.text = "-"
self.ids.x4y1.texture_update()
self.ids.x5y1.texture_update()
self.ids.x6y1.texture_update()
time.sleep(0.2)
我的印象是labelName.texture_update()
立即调用下一帧,而不是等待函数结束,但似乎没有像文档中描述的那样工作;
Warning
The texture update is scheduled for the next frame. If you need the texture immediately after changing a property, you have to call the texture_update() method before accessing texture:
l = Label(text='Hello world')
# l.texture is good
l.font_size = '50sp'
# l.texture is not updated yet
l.texture_update()
# l.texture is good now.
答案 0 :(得分:1)
您应该使用Clock来安排标签文字更改。请考虑以下代码:
test.kv:
#:kivy 1.9.0
Root:
cols: 1
Label:
id: my_label
Button:
text: 'animate text'
on_press: root.animate_text()
main.py:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.clock import Clock
class Root(GridLayout):
string = ''
def animate_text(self):
Clock.schedule_interval(self.update_label, 0.1)
def update_label(self, dt):
self.string += '>'
self.ids.my_label.text = self.string
if len(self.string) > 20:
Clock.unschedule(self.update_label)
self.string = ''
self.ids.my_label.text = 'DONE'
class Test(App):
pass
Test().run()