user3046997
user3046997

Reputation: 1

Reading text files in GUI entry fields

I'd like to know of a way to read a text file and add its contents into a text box. I am doing this in tkinter, so I'd need to get the text in Python, and put it into the text box using tkinter, that would be great. Thanks in advance!

Upvotes: 0

Views: 1999

Answers (1)

user2555451
user2555451

Reputation:

You would use with and open to open the file and then Text.insert to put its contents in the textbox.

Below is a basic demonstration:

from Tkinter import Text, Tk
r = Tk()
t = Text()
t.grid()
with open("/path/to/file") as myfile:
    t.insert("1.0", myfile.read())
r.mainloop()

Upvotes: 2

Related Questions