0

I'm trying to create a text typing effect in a window, however it comes up as "TypeError: 'Text' object is not iterable". Here's my current code:

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

The text comes up in the shell if I use the 'words' variable, however becomes a TypeError if I try using the text variable. Is there a way of making it iterable?

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
Rebecca
  • 3
  • 3

1 Answers1

0

First, you are getting mixed up and confusing your variables text and words; second, your Text object is not iterable, but you can create several ones to be displayed in succession while iterating over `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)

In python 3, you can replace the calls to sys.stdout with standard print calls:

# this prints to console
for char in words:
    sleep(0.1)
    print(char, end='', flush=True)
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80