2017-11-12 3 views
1

kivyで一度に1文字ずつ文字列を表示したかったのです。私はこれをやろうとしましたが、完了したら文字列全体(一度に1文字ではない)だけを表示します。kivyを使用して一度に1つの文字列を印刷するにはどうすればいいですか?

from kivy.app import App 
from kivy.uix.textinput import TextInput 
from kivy.uix.label import Label 
from kivy.uix.boxlayout import BoxLayout 
from time import sleep 

class someclass(BoxLayout): 
    def __init__(self): 
     super(someclass, self).__init__() 
     l = Label(text= '',size_hint_y= 0, height='40dp', pos_hint={'x':0, 'y':0.5}) 
     self.add_widget(l) 
     s = 'Hello hello hello hello' 
     for c in s: 
      sleep(0.5) 
      l.text += c 

class TestApp(App): 
    def build(self): 
     return someclass() 

TestApp().run() 

答えて

0

ブロッキングタスクは、GUIには適していません、彼らは彼らが適切にコンポーネントをアップデートすることができますlife cycleを持っています。解決方法はClockです。

from kivy.app import App 
from kivy.uix.textinput import TextInput 
from kivy.uix.label import Label 
from kivy.uix.boxlayout import BoxLayout 
from kivy.clock import Clock 

class someclass(BoxLayout): 
    s = 'Hello hello hello hello' 
    counter = 0 
    def __init__(self): 
     super(someclass, self).__init__() 
     self.l = Label(text= '',size_hint_y= 0, height='40dp', pos_hint={'x':0, 'y':0.5}) 
     self.clock = Clock.schedule_interval(self.onTimeout, 0.5) 
     self.add_widget(self.l) 

    def onTimeout(self, dt): 
     self.l.text = self.s[self.counter] 
     self.counter += 1 
     if self.counter == len(self.s): 
      self.clock.cancel() 

class TestApp(App): 
    def build(self): 
     return someclass() 

TestApp().run() 
関連する問題