2017-10-03 1 views
0

ウィンドウ内にテキスト入力エフェクトを作成しようとしていますが、 "TypeError: 'Text'オブジェクトはiterableではありません。ここに私の現在のコードがあります:私は言葉 '変数を使用する場合GraphWinのタイピングエフェクト

from graphics import * 
import sys 
from time import sleep 

window = GraphWin('Test', 1000, 700) 

text = Text(Point(500, 150), "This is just a test :P") 
words = ("This is just a test :P") 
for char in text: 
    sleep(0.1) 
    sys.stdout.write(char) 
    sys.stdout.flush() 
word.draw(window) 

Source for typing effect

テキストはシェルで起動します私は、テキスト変数を使用してみた場合、ただし例外TypeErrorになります。 iterableにする方法はありますか?

答えて

0

まず、変数textwordsが混乱して混乱しています。二、あなたのTextオブジェクトが反復可能ではありませんが、Pythonで言葉

from graphics import * 
import sys 
from time import sleep 

window = GraphWin('Test', 1000, 700) 

text = Text(Point(500, 150), "This is just a test :P") 
words = "This is just a test :P" 

# this prints to console 
for char in words: 
    sleep(0.1) 
    sys.stdout.write(char) 
    sys.stdout.flush() 

# that displays on canvas 
for idx, t in enumerate(words): 
    text = Text(Point(300+idx*20, 150), t) 
    text.draw(window) 
    sleep(0.1) 

`上の3を反復しながら、連続して表示されるように、いくつかのものを作成することができ、あなたは標準print呼び出しにsys.stdoutへの呼び出しを置き換えることができます:

# this prints to console 
for char in words: 
    sleep(0.1) 
    print(char, end='', flush=True) 
関連する問題