sys.sdout.flush() 的替代版本是什么,除了它在 kivy ui 文本上?

what is an alternative version of sys.sdout.flush() except its on kivy ui texts?

问题很简单,但是找不到答案,我之前也问过,但是没有回复甚至没有意见,所以这次我的目标更加明确 例如,这不起作用。它将等到添加到文本的完整字符完成,然后显示文本

class buttonex(BoxLayout):
    mytext=StringProperty("helloyes")
    hello=ObjectProperty(None)
    def on_button_click(self):
        self.hello.text=""
        for char in "hi noob lol hahaha":
            sleep(0.07)
            self.hello.text=self.hello.text+char

解决问题,

what is an alternative version of sys.sdout.flush() except its on kivy ui texts?

这是一个使用 Clock 的简单实现。

from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.properties import (NumericProperty, StringProperty)
from kivy.uix.boxlayout import BoxLayout



Builder.load_string("""

<TextBox>:
    orientation: "vertical"
    padding: "10dp"

    Label:
        id: label

    TextInput:
        multiline: 0
        hint_text: "Enter text and hit enter"
        on_text_validate: root.type_text(self.text)

""")



class TextBox(BoxLayout):

    cache_text = StringProperty("") # For storing the entered text.
    index = NumericProperty(0) # For iteration over cache_text.

    def type_text(self, txt, *args):
        self.cache_text = txt # Store the entered text here.
        # For more control you may use method Clock.create_trigger.
        self.ev = Clock.schedule_interval(self.update_text, 0.25) # Update text after every 0.25 sec.

    def update_text(self, *args):
        if self.index < len(self.cache_text):
            val = self.cache_text[self.index]
            self.ids.label.text += val # Appending to existing text.
            self.index += 1
        else:
            self.ids.label.text = "" # Comment it out to retain the text.
            self.index = 0 # Reset index.
            self.cache_text = "" # Clear cache.
            self.ev.cancel() # Cancel text updation.



class TestApp(App):

    def build(self):
        return TextBox()



TestApp().run()