0

I want to use the main() to create the text and display it in a tkinter widget but I don't know how to make it.

import sys, time, os, random
from tkinter.messagebox import *
from tkinter import * 

filepath = "file.txt"

root = Tk()
root.configure(bg=color_black)
    
def main():
    file = open('file.txt', "r")
    for line in file:
        message = line
        for char in message:
            sys.stdout.write(char)
            sys.stdout.flush()
            my_label.config(text=char)
            time.sleep(random.uniform(0,0.05))
    file.close()

global my_label
my_label = Label(root, text="")
my_label.pack(pady=10)

main()

root.mainloop()
martineau
  • 119,623
  • 25
  • 170
  • 301
  • You could create a `tkinter.Text` widget and repeatedly call its `insert()` method to add successive lines of output to it. – martineau Sep 28 '20 at 00:31
  • output.insert(INSERT,char) output.update_idletasks() this lines work good but when a endline come in from the text file, the program stop – antoine durand Sep 28 '20 at 08:28
  • Seems like you should be using `output.insert(END,char)`. The call to `update_idletasks()` is needed because you're using `time.sleep()`. Use the universal [`after()`](https://web.archive.org/web/20190222214221id_/http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/universal.html) widget method instead and you wouldn't need it. – martineau Sep 28 '20 at 13:41

0 Answers0