如何按字符显示标签文本?

时间:2020-06-13 06:19:34

标签: python animation widget kivy label

我一直在尝试在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 Clock对象解决了他们对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()

这是我目前能想到的最好的方法,但是再次,这甚至与我真正想要的都不接近。

你们中的任何人是否有更多经验,可以帮助我吗?非常感谢,谢谢!

2 个答案:

答案 0 :(得分: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()

答案 1 :(得分:0)

我成功了!下面的代码正是我想要的。我只是使用Clock.schedule_interval()函数使用字符串索引和列表对其进行迭代。

我在kv中添加了“ valign”和一些填充以使其看起来更好。和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()