我尝试在窗口中创建文本输入效果,但是它出现了 " TypeError:' Text'对象不可迭代"。这是我目前的代码:
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)
如果我使用'单词'那么文本会出现在shell中。变量,但如果我尝试使用文本变量,则变为TypeError。有没有办法让它可迭代?
答案 0 :(得分:0)
首先,您混淆并混淆变量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)