Kivy:通过间隔一个字母一个字母地更新LabelText是不可能的

2024-09-28 22:00:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在独立地尝试在一个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

Tags: sampletext文本selfboxforthatis
2条回答

不需要使用kwargs

要创建打字机效果,只需按照下面的代码操作。你知道吗


def anything(str):

for letter in str:
    sys.stdout.write(letter)
    sys.stdout.flush()
    time.sleep(0.5)

anything("That is my sample text.")

我编写的代码在字符串中的每个字母之间提供了0.5秒的等待时间,并且只需要6行代码。你知道吗

问题

get the text starting in the upper left corner of the label

解决方案-左上角的文本

在构造函数中添加以下内容,__init__()方法。你知道吗

def __init__(self, string, **kwargs):
    super(TestScreen, self).__init__(**kwargs)
    self.bind(size=self.setter('text_size'))
    self.halign = 'left'
    self.valign = 'top'

Text alignment and wrapping

The Label has halign and valign properties to control the alignment of its text. However, by default the text image (texture) is only just large enough to contain the characters and is positioned in the center of the Label. The valign property will have no effect and halign will only have an effect if your text has newlines; a single line of text will appear to be centered even though halign is set to left (by default).

In order for the alignment properties to take effect, set the text_size, which specifies the size of the bounding box within which text is aligned. For instance, the following code binds this size to the size of the Label, so text will be aligned within the widget bounds. This will also automatically wrap the text of the Label to remain within this area.

Label:
    text_size: self.size
    halign: 'right'
    valign: 'middle'

输出-左上角的文本

Img01 - text in top-left cornerImg02 - text in top-left corner

解决方案-文本居中

Clock.create_trigger()来模拟打字机。你知道吗

Triggered Events

A triggered event is a way to defer a callback. It functions exactly like schedule_once() and schedule_interval() except that it doesn’t immediately schedule the callback. Instead, one schedules the callback using the ClockEvent returned by it. This ensures that you can call the event multiple times but it won’t be scheduled more than once. This is not the case with Clock.schedule_once()

你知道吗主.py你知道吗

from kivy.app import App
from kivy.uix.label import Label
from kivy.clock import Clock


class TestScreen(Label):

    def __init__(self, string, **kwargs):
        super(TestScreen, self).__init__(**kwargs)
        self.font_size = 26
        self.string = string
        self.typewriter = Clock.create_trigger(self.typeit, 1)
        self.typewriter()

    def typeit(self, dt):
        self.text += self.string[0]
        self.string = self.string[1:]
        if len(self.string) > 0:
            self.typewriter()


class TestApp(App):
    title = "Kivy Typewriter"

    def build(self):
        return TestScreen("That is my Kivy Typewriter demo")


if __name__ == "__main__":
    TestApp().run()

输出-文本居中

Img01 - text in centerImg02 - text in center

相关问题 更多 >