[Tutor] another quickie

Alan Gauld alan.gauld at btinternet.com
Fri Oct 5 09:46:52 CEST 2007

"max baseman" <dos.fool at gmail.com> wrote

> do is open a text file, write to it, than save it. so far i have a
> GUI with the ability to right text (nothing amazing), but i don't
> know how to have it display the text from a text file any help would
> be great
> from Tkinter import *
> top=Tk()
> F=Frame(top)
> F.pack()
> F.txtBox=Text(top)
> F.txtBox.pack()
> F.pack()

Ok This gets you the basic text box. Now you need to open
the file, read it into a variable and insert that into the text box.
I assume you already know how to open the file and read it
into a variable. (If not see File handling in my tutorial)

To insert it into the text box use the insert method as
shown in my case study:

self.txtBox.insert(END, resultStr)

If you want to insert it at a specific location(like the start)
you can replace END with 1.0 as also shown in the
case study for deleting the content:

self.txtBox.delete(1.0, END)

Did you check the Tkinter reference link:


It shows how to manipulate the contents of the Text
widget in various ways. Including inserting text.

PS. If you don't want to hard code the file name you can use
a Tkinter FileOpen standard dialog to get the filename(and path)
from the user prior to opening it.


To save your changes use the filesaveas dialog to get the target
filename(optional) and then Text.select to get the text out of the 
and then the usual Python file methods to write the text out to file.

All the information you need should be in the links in this message.


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list