[英]How Can I Animate a Label in Kivy?
Ok, My question goes this way How Can I Animate a Label in Kivy , My apologies if this question is annoyingly way too easy :)好的,我的问题是这样的,我如何在 Kivy 中为标签设置动画,如果这个问题太简单了,我很抱歉:)
Here is the Label
in which I want animation.这是我想要动画的
Label
。 Can I have edits of the code in the comments?我可以在评论中编辑代码吗?
Actually, I am obsessed with this script, racking my brains off to know the way to animate this thingy!!实际上,我对这个脚本很着迷,绞尽脑汁想知道如何为这个东西制作动画!! Pls Help!..
请帮助!...
import kivy
from kivy.app import App
from kivy.uix.label import Label
class MyApp(App):
def build(self):
lu = Label(text = "This is a label! Pls help me with Animation!")
return lu
if __name__ == '__main__':
MyApp().run()
If you want to update text on Label
every few seconds then you can use Clock and Clock.schedule_interval(function_name, seconds)
to execute function_name(dt)
every seconds
and in this function you should change text in Label
如果您想每隔几秒更新一次
Label
文本,那么您可以使用Clock和Clock.schedule_interval(function_name, seconds)
function_name(dt)
每隔seconds
执行一次function_name(dt)
并且在此函数中您应该更改Label
文本
Minimal example which displays current time.显示当前时间的最小示例。
from kivy.app import App
from kivy.uix.label import Label
from kivy.clock import Clock
import datetime
def update_label(dt):
new_text = datetime.datetime.now().strftime('%H:%M:%S')
label.text = new_text
#print(new_text)
label = None # create global variable to access the same `label` in two different functions
class MyApp(App):
def build(self):
global label # inform function to assign `Label` to global variable
label = Label(text="???")
Clock.schedule_interval(update_label, 1)
return label
#Clock.schedule_interval(update_label, 1)
if __name__ == '__main__':
MyApp().run()
EDIT:编辑:
Another example which scroll text滚动文本的另一个例子
from kivy.app import App
from kivy.uix.label import Label
from kivy.clock import Clock
import datetime
label = None
text = 'Hello World of Python!'
text_length = len(text)
index = 0
temp_text = text + ' ' + text
def update_label(dt):
global index
label.text = temp_text[index:index+15]
index += 1
if index >= text_length:
index = 0
class MyApp(App):
def build(self):
global label
label = Label(text="???")
Clock.schedule_interval(update_label, 0.20)
return label
if __name__ == '__main__':
MyApp().run()
EDIT:编辑:
For numerical values you can use Animation .对于数值,您可以使用Animation 。
Here is blinking text.这是闪烁的文字。
It changes color to black
(in 0.2 second) and next it change back to white
(in 0.2 second).它将颜色变为
black
(在 0.2 秒内),然后又变回white
(在 0.2 秒内)。 And it repeats it.它重复它。
from kivy.app import App
from kivy.uix.label import Label
from kivy.animation import Animation
class MyApp(App):
def build(self):
label = Label(text='Hello World of Python!')
anim = Animation(color=(0, 0, 0, 1), duration=.2) + Animation(color=(1, 1, 1, 1), duration=.2)
anim.repeat = True
anim.start(label)
return label
if __name__ == '__main__':
MyApp().run()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.