我正在独立地尝试在一个Kivy标签上创建一个“打字机”效果。下面的文本“That is my sample Text”(这是我的示例文本)应以0.5的间隔逐字填充我的标签。就像有人用打字机打字一样。你知道吗
然而,我得到的结果很奇怪:没有得到想要的效果,整个字符串被添加到0.5之后,就这样了。似乎我的for循环被完全忽略了。你知道吗
你知道我能做什么吗?你知道吗
这是我的类TestScreen(Screen):
def __init__ (self,**kwargs):
super(TestScreen, self).__init__(**kwargs)
my_box = FloatLayout()
self.mylabel = Label(
text='',
font_size=26,
pos_hint={'center_x': 0.5, 'center_y': 0.05})
my_box.add_widget(self.mylabel)
self.add_widget(my_box)
for letter in 'That is my sample text':
Clock.schedule_once(partial(self.setLetterByLetter, letter=letter), 1)
def setLetterByLetter(self, dt, letter):
self.mylabel.text += letter
return True
不需要使用
kwargs
要创建打字机效果,只需按照下面的代码操作。你知道吗
我编写的代码在字符串中的每个字母之间提供了0.5秒的等待时间,并且只需要6行代码。你知道吗
问题
解决方案-左上角的文本
在构造函数中添加以下内容,
__init__()
方法。你知道吗Text alignment and wrapping
输出-左上角的文本
解决方案-文本居中
用
Clock.create_trigger()
来模拟打字机。你知道吗Triggered Events
你知道吗主.py你知道吗
输出-文本居中
相关问题 更多 >
编程相关推荐