[Tutor] Display standard output in Tk widget

alan.gauld@bt.com alan.gauld@bt.com
Sat, 8 Sep 2001 18:20:30 +0100


Here's a short script to demonstrate stdout going to 
a Text widget, as requested by somebody recently. 
Its very basic but shows the principle...
Rob, if you want it for Useless python feel free to grab it...

Alan Gauld
BT computing partners
Tel : 0141 220 8795
Fax : 0141 248 1284 

#######################
# File: tkoutput.py
# Author: A.J. Gauld
# Date: September 2001
#
from Tkinter import *
import sys

class Display(Frame):
    ''' Demonstrate python interpreter output in Tkinter Text widget

type python expression in the entry, hit DoIt and see the results
in the text pane.'''
    
    def __init__(self,parent=0):
       Frame.__init__(self,parent)
       self.entry = Entry(self)
       self.entry.pack()
       self.doIt = Button(self,text="DoIt", command=self.onEnter)
       self.doIt.pack()
       self.output = Text(self)
       self.output.pack()
       sys.stdout = self
       self.pack()

    def onEnter(self):
        print eval(self.entry.get())

    def write(self, txt):
        self.output.insert(END,str(txt))

if __name__ == '__main__':
    Display().mainloop()