GraphWin 中的打字效果
Typing effect in GraphWin
我正在尝试在 window 中创建文本输入效果,但它显示为
"TypeError: 'Text' object is not iterable"。这是我当前的代码:
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
如果我使用 'words' 变量,文本会出现在 shell 中,但是如果我尝试使用文本变量,则会变成 TypeError。有没有办法让它可迭代?
首先,您混淆了变量 text
和 words
;其次,您的 Text
对象不可迭代,但您可以创建多个对象,以便在迭代 `words' 时连续显示
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)
在 python 3 中,您可以将对 sys.stdout
的调用替换为标准 print
调用:
# this prints to console
for char in words:
sleep(0.1)
print(char, end='', flush=True)
我正在尝试在 window 中创建文本输入效果,但它显示为 "TypeError: 'Text' object is not iterable"。这是我当前的代码:
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
如果我使用 'words' 变量,文本会出现在 shell 中,但是如果我尝试使用文本变量,则会变成 TypeError。有没有办法让它可迭代?
首先,您混淆了变量 text
和 words
;其次,您的 Text
对象不可迭代,但您可以创建多个对象,以便在迭代 `words' 时连续显示
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)
在 python 3 中,您可以将对 sys.stdout
的调用替换为标准 print
调用:
# this prints to console
for char in words:
sleep(0.1)
print(char, end='', flush=True)