[FRPythoneers] Simple GUI Output

Evelyn Mitchell efm at tummy.com
Fri Jun 1 20:06:35 MDT 2001


Wayde, this should do the trick. It opens up a Text window. If you
run it standalone (or as a pipe), you can pass in the name of
the file to display on the commandline.

I haven't tested it on Windows.

This is (slightly modified) from Mark's wonderful book (p376-377):

#!/usr/bin/python

from Tkinter import *

class ScrolledText(Frame):
	def __init__(self, parent=None, text='', file=None):
		Frame.__init__(self, parent)
		self.pack(expand=YES, fill=BOTH)
		self.makewidgets()
		self.settext(text,file)
		self.master.title("Wayde's Program Output")
		self.master.iconname("WaydePy")

	def makewidgets(self):
		sbar = Scrollbar(self)
		text = Text(self, relief=SUNKEN)
		sbar.config(command=text.yview)
		text.config(yscrollcommand=sbar.set)
		sbar.pack(side=RIGHT, fill=Y)
		text.pack(side=LEFT, expand=YES, fill=BOTH)
		self.text = text

	def settext(self, text='', file=None):
		if file:
			text = open(file, 'r').read()
		self.text.delete('1.0', END)
		self.text.insert('1.0', text)
		self.text.mark_set(INSERT, '1.0')
		self.text.focus()

	def gettext(self):
		return self.text.get('1.0', END+'-1c')

if __name__ == '__main__':
	root = Tk()
	try: 
		st = ScrolledText(file = sys.argv[1])
	except IndexError:
		st = ScrolledText(text = 'Words\ngo here')
	def show(event):
		print repr(st.gettext())
	root.bind('<Key-Escape>', show)
	root.mainloop()



More information about the FRPythoneers mailing list