[Tkinter-discuss] Taking a Text() widget and saving it to a .txt file

John McMonagle jmcmonagle at velseis.com.au
Fri Jul 4 04:42:51 CEST 2008


Alexnb wrote:
> I don't really know if this is possible, but it seems like there should be an
> easy way. If I have a text widget:
> 
> text = Text(parent)
> 
> and I want to take what is in there and save it has a text file. How would I
> go about doing this. Also, if it matters, I would like to keep it in the
> same format as the textbox. 

As Cameron Laird has pointed out, the get method of the Text widget
returns a range of characters selected by line and character position to
a string.

You then need to open a file for writing, write the string to the file,
and close the file.


As for keeping it in the same format as the textbox, that does not make
any sense.  Plain text files don't really have formatting, apart from
spaces, tabs or carriage returns.  These will be preserved by the get
method.  However other formatting such as font type, font height,
colour, etc will not.

Regards,

John


More information about the Tkinter-discuss mailing list