0

Is there a way to redirect all IDLE output to a tkinter GUI, or do something with the IDLE outputs? I want to show all IDLE output from the http.server module in a tkinter GUI, to monitor all things happening to a webserver. Is this doable with a few simple functions/commands or will this involve editing the IDLE lib files?

Asked something similair before, I wanted to embed the whole IDLE/python shell in my monitoring program, but this seems impossible with the newest python version.

EDIT:

from tkinter import *
import sys, time, threading
global TXT
root = Tk()
TXT = Text(root, font="Arial")
TXT.pack(fill=X)

class RedirectText(object):

    def __init__(self, TXT):
        self.out = TXT

    def write(self, string):
        TXT.insert(END, string)
sys.stdout = RedirectText(TXT)

for i in range(10):
    print("test", i+1)

def thread():
    print("Delayed")
    threading.Timer(1, thread).start()

threading.Timer(1, thread).start()
root.mainloop()

Removed all faulty code, now have this. Will not write anything system related, just what is inside a print() function. Still looking for a way to insert all system output into a text widget.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
IPDGino
  • 706
  • 1
  • 8
  • 17
  • 1
    See [this answer][1] for a Tkinter example of redirecting stdout to a text widget. [1]: http://stackoverflow.com/a/12352237/7432 – Bryan Oakley Jul 02 '13 at 19:40
  • @BryanOakley Once again, a wonderful answer! Exactly what I needed. Somehow didn't find it and I did a lot of googling. – IPDGino Jul 02 '13 at 19:41

2 Answers2

1

See this answer for an example of redirecting stdout to a Tkinter text widget.

Community
  • 1
  • 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
0

You don't want to redirect IDLE output, you want to redirect the system standard output. Here's a little something I've used before for WXPython:

class RedirectText(object):
    def __init__(self, TextCtrl):
        self.out = TextCtrl

    def write(self, string):
        wx.CallAfter(self.out.WriteText, string)

And then to initialize:

 import sys
 sys.stdout = RedirectText(log)

Where log is a WX TextCtrl widget.

Dan Doe
  • 1,146
  • 3
  • 14
  • 25
  • looks pretty clear, but unfortunately wxPython is not available for python 3.3. I think it's doable though with tkinter. Is `TextCtrl` a widget? And `CallAfter`'s use seems pretty vague to me. But basically what I interpret is that you make a text widget, and redirect the system standard output to that widget? – IPDGino Jul 02 '13 at 19:10
  • I imagine that Tkinter has a similar system that you can use. When you print something in your Python script, the print function calls sys.stdout.write, so if you overwrite that with a new object that also has a write function, it'll result in a redirected print statement. – Dan Doe Jul 02 '13 at 19:13
  • [This answer](http://stackoverflow.com/a/12352237/7432) has a Tkinter example of redirecting stdout to a tkinter text widget – Bryan Oakley Jul 02 '13 at 19:38