[Tutor] how to display terminal messages indialog windowusingtkinter

Kent Johnson kent37 at tds.net
Sat Feb 23 03:10:18 CET 2008


Alan Gauld wrote:
> I don't know of any way to turn vanilla print statements into GUI 
> visible
> strings. Even redirecting stdout would require the creation of some 
> kind
> of custom widget that wrote to the GUI. (But I'd love to be wrong! :-)

OK I'll bite. You don't need a custom GUI widget, you just need a simple 
adapter. Here is a simple proof of concept that redirects sys.stdout to 
a Text widget:

import sys
from Tkinter import *

root = Tk()

t1 = Text(root)
t1.pack()

class PrintToT1(object):
     def write(self, s):
         t1.insert(END, s)

sys.stdout = PrintToT1()
print 'Hello, world!'
print "Isn't this fun!"

mainloop()


# Kent


More information about the Tutor mailing list