简体   繁体   English

当 time.sleep 在 python 中运行时,如何更新 .kv 文件中文本的值?

[英]How do I update the value of text in a .kv file while time.sleep is running in python?

I'm trying to make the text of a button change like a typewriter effect but ran into a problem.我试图让按钮的文本像打字机效果一样改变,但遇到了问题。 While the program is sleeping, the value of the text doesn't actually change until it's done.当程序处于休眠状态时,文本的值在它完成之前实际上并没有改变。 After 12 seconds (the text im passing over has 12 characters, and the program sleeps for 1 second after each one) the text for the button updates, but I'm trying to get it to update every character. 12 秒后(我传递的文本有 12 个字符,程序在每个字符后休眠 1 秒)按钮的文本更新,但我试图让它更新每个字符。 Is there a fix for this?有解决办法吗?

import time
import asyncio
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.floatlayout import FloatLayout
from kivy.config import Config
from kivy.core.window import Window
from kivy.uix.screenmanager import FadeTransition

Builder.load_file("test.kv")


class FirstScreen(Screen):
    pass


class SecondScreen(Screen):
    pass

class CandLApp(App):

    def build(self):
        sm = ScreenManager(transition=FadeTransition())
        sm.add_widget(FirstScreen(name="first"))
        sm.add_widget(SecondScreen(name="second"))
        return sm

    def text_wait(self, text, idforbutton):
        anything = getattr(self.root.get_screen("second").ids, idforbutton)
        anything.text = ""
        for i in text:
            anything.text += i
            print(anything.text)
            time.sleep(1) #      the value of text for the button in the second screen doesn't change until 
                          #      this entire for loop is done running


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

.kv file: .kv 文件:

<FirstScreen>:
    Button:
        size_hint: (.274, .2)
        pos_hint: {"x":.067, "y":.049}
        font_size: 45
        color: 200/255, 0/255, 70/255, 1
        text: "Test"
        on_release: root.manager.current = "second"

<SecondScreen>:
    FloatLayout:
        Button:
            id: buttonfortext
            text: "button"
            pos_hint: {"center_x": .5, "center_y": .5}
            size_hint: .2, .2
            on_release: app.text_wait("sending this", "buttonfortext") 

If you want to print the text in a typewriter way you can use:如果要以打字机方式打印文本,可以使用:

import sys
from time import sleep

def typewriter_print(word)
    for char in word:
    sleep(1)
    sys.stdout.write(char)
    sys.stdout.flush()

then you can call the typewriter_print(word) like this然后你可以像这样调用 typewriter_print(word)

def text_wait(self, text, idforbutton):
        anything = getattr(self.root.get_screen("second").ids, idforbutton)
        anything.text = ""
        typewriter_print(text)

Expanding on my answer above:扩展我上面的答案:

Create a callback function that goes through the characters of the text one by one, and returns False when it's done创建一个回调 function 逐个遍历文本的字符,完成后返回 False

    def handle_char(self, iterator, anything ):
        try: 
            anything += next(iterator)
            print(anything)
            return True
        except StopIteration:
            return False

and then call this callback function every second.然后每秒调用这个回调 function 。

   def text_wait(self, text, idforbutton):
       ...
       Clock.schedule_interval(lambda dt: self.handle_char(iter(text), anything), 1.0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM