Return to Home Page
      Blog     Consulting     Seminars     Calendar     Books     CD-ROMS     Newsletter     About     FAQ      Search
 

Here's a more comcrete example, that 'adapts' a tk text widget into a Python file onject. It then replaces sys.stdout using this class, and demonstrates that 'print' still works.


import Tkinter as tk
import sys

class TkWriteFile:
    def __init__(self, text):
        self._text = text
        self.closed = 0
        self.mode = 'w'
        self.softspace = 0

    def write(self, data):
        if self.closed:
            raise ValueError('I/O operation on closed TkFile')
        self._text.insert('end', data)

    def writelines(self, lines):
        self.write(''.join(lines))

    def close(self):
        self.closed = 1


mw = tk.Tk()
t = tk.Text(mw)
t.pack()
sys.stdout = TkWriteFile(t)
print 'spam'
mw.mainloop()
f  e  x



Add your comment below. Use an empty line between each paragraph. Paragraphs will be automatically formatted, and single carriage returns will be respected. Use <code> to begin a code block, and </code> to end a code block. Your email address will not be visible to spam harvesters or used in any way except to contact you with further questions.

Your Email Address:

Search     Home     WebLog     Consulting     Seminars     Calendar     Books     CD-ROMS     Newsletter     About     Contact     Site Design     Server Maintenance     Powered by Zope
©2007 MindView, Inc.