如何逐字显示Label文本?

问题描述 投票:0回答:1

我一直想在Kivy上编写一个基于文本的小冒险游戏。这意味着,会有大量的文本供用户阅读,如果Label中的所有文本不是一次性显示,而是以一个简单的 "动画 "的形式逐字显示,那将会更容易看清。在这个 "动画 "结束时,整个文本将被显示出来。

在常规的 Python 中,我想要的是这样的效果。

text1 = "Now tell me, what is your astrological sign?\n"
    for character in text1:
        sys.stdout.write(character)
        sys.stdout.flush()
        time.sleep(0.05)

在Kivy中,这似乎要难得多,因为睡眠函数只是让整个应用程序 "睡眠"。我在周围搜索了一下,并没有找到任何关于这个特殊问题的资料。有些人通过使用Kivy时钟对象来解决他们对time.sleep的需求,但即使在这里,我也只能让文本一次只显示一个字符(见下面的代码),这就是 非我所愿. 我希望所有的字符加起来,在这个 "动画 "结束时,整个Label文本都站在那里。

from kivy.app import App
from kivy.lang import Builder
from kivy.properties import StringProperty
import time
from kivy.clock import Clock

kv = '''

BoxLayout:
    orientation: 'vertical'
    Label:
        text: app.text
    Button:
        text: 'click me'
        on_press: app.clicked()

'''


class MyApp(App):
    text = StringProperty("hello world")
    lst = [1,2,3,4,5,6,7,8,9,10]

    def build(self):
        return Builder.load_string(kv)

    def clicked(self):
        Clock.schedule_interval(self.clicked2, 0.5)

    def clicked2(self, count):
        if len(self.lst) == 0:
            return False
        self.text = "clicked!" + str(self.lst[len(self.lst)-1])
        self.lst.pop()


if __name__ == '__main__':
    MyApp().run()

这是我目前能想到的最好的办法,但同样,这离我真正想要的还差得远。

你们谁有更多的经验,可以帮我解决这个问题吗?这将是非常感激的,谢谢

python animation widget kivy label
1个回答
1
投票

我知道你已经,回答你的问题。但是,我会告诉你更短的代码,使用 scope variable. 要使用它,请声明 nonlocal [var]

例子。

def A(i=0):
  def c():
    nonlocal i; i += 1
  print(i)        #0
  c(); print(i) #1
  c(); print(i) #2
  c(); print(i) #3
A()

我在下面的代码中用了这样的方法

from kivy.lang import Builder
from kivy.properties import StringProperty
import time
from kivy.clock import Clock
from kivy.app import App

kv = '''

BoxLayout:
    orientation: 'vertical'
    Label:
        text: app.text
        text_size: self.size
        valign: "middle"
        padding: root.width/20, root.height/20
    Button:
        text: 'click me'
        on_press: app.clicked()

'''


class MyApp(App):
    text = StringProperty("hello world")
    blabla = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum"
    def build(self): return Builder.load_string(kv)

    def clicked(self, i=0):
      def clicked2():
        nonlocal i; i += 1
        if i > len(self.blabla): return False
        self.text = self.blabla[:i]
      Clock.schedule_interval(lambda count: clicked2(), 0.05)


if __name__ == '__main__': MyApp().run()

0
投票

我让它工作了!下面的代码正是我想要的。我只是使用字符串索引和列表来使用Clock.schedule_interval()函数进行迭代。

我在kv中添加了一个 "valign "和一些padding,以使它看起来更漂亮。text_size: self_sice是为了使文本在蔓延到多行时被包裹。

from kivy.lang import Builder
from kivy.properties import StringProperty
import time
from kivy.clock import Clock

kv = '''

BoxLayout:
    orientation: 'vertical'
    Label:
        text: app.text
        text_size: self.size
        valign: "middle"
        padding: root.width/20, root.height/20
    Button:
        text: 'click me'
        on_press: app.clicked()

'''


class MyApp(App):
    text = StringProperty("hello world")
    blabla = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum"
    lst = []
    for x in range(len(blabla)+1):
        print(x)
        lst.append(x)
    lst.reverse()

    def build(self):
        return Builder.load_string(kv)

    def clicked(self):
        Clock.schedule_interval(self.clicked2, 0.05)

    def clicked2(self,count):
        if len(self.lst) == 0:
            return False
        self.text = str(self.blabla[:self.lst[len(self.lst)-1]])

        self.lst.pop()


if __name__ == '__main__':
    MyApp().run()
© www.soinside.com 2019 - 2024. All rights reserved.