1

I want to create a little animation where text appears on an old monochrome terminal like screen as if someone is typing it. However I constantly run into problems with trying to run code after the GUI window opens up. The text is always already there when the window opens or doesnt appear at all. Any help is very appreaciated:)

string = "Hello World this is a Test String"

import random
import time
import tkinter as tk
from tkinter import *

vid = tk.Tk()
vid.title('Terminal')

text = Text( vid, width = 100, height = 50,  highlightthickness=1, bg='black', highlightbackground="black", font=('Courier', 14), fg='green')
text.pack()

def main():
for i in string:
    text.insert(END, i)
    time.sleep(0.2)

text.after(10, main)
vid.mainloop()

This is what I came up with so far:/

maclaren
  • 11
  • 2
  • Have you tried testing it with larger argument of ```sleep```? It might be the case that it takes so little time for ```main``` to run that it has result ready before main window renders. Try something like 5 seconds and see what happens. – NotAName Dec 29 '19 at 04:10
  • @pavel that won’t work. `sleep` prevents all rendering from happening. – Bryan Oakley Dec 29 '19 at 04:20

2 Answers2

1

You need to use update_idletasks()after your sleep. The following works for me, let me know if you have any other questions:

string = "Hello World this is a Test String"

import random
import time
import tkinter as tk
from tkinter import *

vid = tk.Tk()
vid.title('Terminal')

text = Text( vid, width = 100, height = 50,  highlightthickness=1, bg='black', highlightbackground="black", font=('Courier', 14), fg='green')
text.pack()

def main():
    for i in string:
        text.insert(END, i)
        time.sleep(0.2)
        vid.update_idletasks()

vid.after(10, main)
vid.mainloop()
3ddavies
  • 546
  • 2
  • 19
  • unfortunatly, the window still only appears after the text has been fully written... could the problem lay in the text editor im using? I run my code on the IDLE Console? – maclaren Dec 29 '19 at 09:53
  • I finally resolved it. Just writing the update() method fixed it. Thank you so much for helping^^ – maclaren Dec 29 '19 at 09:59
1

It is generally not a good idea to use sleep for an event-driven program (tkinter is event-driven, as it is the case for most GUI libraries). Here, it is better to base your animation on the after method:

import random
import tkinter as tk

string = "Hello World this is a Test String"

def animate(n=0):
  text.insert(tk.END, string[n])
  n += 1
  if n == len(string): # reached end of string 
    text.insert(tk.END, '\n') # insert newline
    n = 0 # and reset current char index
  text.after(200, lambda:animate(n)) # call function again in 200ms

vid = tk.Tk()
vid.title('Terminal')

text = tk.Text(vid, width=100, height=50, highlightthickness=1, bg='black',
            highlightbackground="black", font=('Courier', 14), fg='green')
text.pack()

vid.after(10, animate)
vid.mainloop()

Note: I've slightly changed your animation to create an infinite loop where the string is endlessly printed char by char, inserting a new line when the end is reached. Just for the fun...

sciroccorics
  • 2,357
  • 1
  • 8
  • 21