redirect console output to widget?

Fredrik Lundh fredrik at pythonware.com
Wed Feb 13 15:00:38 EST 2002


Lei Chen wrote:
> I think you are right.  Is there an alternative to Tkinter that is
> thread safe?

Tkinter?

just run all UI code in the main thread, and let the writers
write to a Queue object; e.g.

from Tkinter import *

import thread # should use the threading module instead!
import Queue

import os

class ThreadSafeConsole(Text):
    def __init__(self, master, **options):
        Text.__init__(self, master, **options)
        self.queue = Queue.Queue()
        self.update_me()
    def write(self, line):
        self.queue.put(line)
    def clear(self):
        self.queue.put(None)
    def update_me(self):
        try:
            while 1:
                line = self.queue.get_nowait()
                if line is None:
                    widget.delete(1.0, END)
                else:
                    widget.insert(END, str(line))
                widget.see(END)
                widget.update_idletasks()
        except Queue.Empty:
            pass
        self.after(100, self.update_me)

#this function pipes input to an widget
def pipeToWidget(input, widget):
    widget.clear()
    while 1:
        line = input.readline()
        if not line:
            break
        widget.write(line)

def funcThread(widget):
    input = os.popen('dir', 'r')
    pipeToWidget(input, widget)

#uber-main
root = Tk()
widget = ThreadSafeConsole(root)
widget.pack(side=TOP, expand=YES, fill=BOTH)
thread.start_new(funcThread, (widget,))
thread.start_new(funcThread, (widget,))
thread.start_new(funcThread, (widget,))
thread.start_new(funcThread, (widget,))
thread.start_new(funcThread, (widget,))
root.mainloop()

</F>

<!-- (the eff-bot guide to) the python standard library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->





More information about the Python-list mailing list