text widget example?

W

wanwan

I need my GUI to open and display a text file. I'm thinking of using a
text widget but it looks so complicated in the tkinter manual.

question I have is:

is there an example anyone can find on the internet?
 
J

jmdeschamps

Well, in the 'give me a free example' type questions, I guess I'm a bit
blue tonight, so here goes: ( no personal pun intended, just look up
some recent post in this newsgroup...)

# Start the example Python code to show some text file in a text widget
from Tkinter import * # import Tkinter in __main__ namespace

root = Tk() # initialize Tkinter

myTextWidget= Text(root) # set up a text widget as a root (window)
child

myFile=file("myTextFile.txt") # get a file handle
myText= myFile.read() # read the file to variable
myFile.close() # close file handle

myTextWidget.insert(0.0,myText) # insert the file's text into the text
widget

myTextWidget.pack(expand=1, fill=BOTH) # show the widget

root.mainloop() #run the events mainloop
# End the example here

You need a text file *myTextFile.txt* in your current working directory
for this to work.

Read-up on the different objects and properties for detail.

Good luck!

BTW, now that this is posted, I guess one could say that it's an
example on the Internet
 
J

James Stroud

I need my GUI to open and display a text file. I'm thinking of using a
text widget but it looks so complicated in the tkinter manual.

question I have is:

is there an example anyone can find on the internet?

Couldn't find a good one, so here's a 5 minute version (your exercise is to
put in a scroll bar). See:

http://www.pythonware.com/library/tkinter/introduction/

James

from Tkinter import *
import tkFileDialog
import tkMessageBox

class MyText(Frame):
def __init__(self, parent=None, *args, **kwargs):
Frame.__init__(self, parent)
self.pack()
self.text = Text(self, *args, **kwargs)
self.text.config(background='white')
self.text.pack(expand=YES, fill=BOTH)
def get(self):
return self.text.get(1.0,"%s-1c" % END)
def set(self, astr):
self.text.delete(1.0, END)
self.text.insert(INSERT, astr)
def set_from_file(self):
afilename = tkFileDialog.askopenfilename()
if afilename:
try:
afile = open(afilename)
self.set(afile.read())
afile.close()
except Exception, e:
tkMessageBox.showwarning("File Problem",
"Couldn't read file '%s': %s" % (afilename, str(e)))

def main():
tk = Tk()
tk.title('Text Reader App')
atext = MyText(tk)
atext.pack()
open_button = Button(tk, text="Open File",
activeforeground='blue',
command=atext.set_from_file)
open_button.pack()
tk.mainloop()

if __name__ == "__main__":
main()




--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,270
Messages
2,571,341
Members
48,031
Latest member
rashmi16

Latest Threads

Top