stdout to tkinter GUI

0 votes
I want to redirect stdout data to a tkinter Text widget. Can anyone tell me on how to do this?
Apr 10 in Python by ana1504.k
• 7,870 points
236 views

1 answer to this question.

0 votes
You need to make a file-like class whose write method writes to the Tkinter widget instead, and then do sys.stdout = <your new class>.

For example:

class IORedirector(object):
    '''A general class for redirecting I/O to this Text widget.'''
    def __init__(self,text_area):
        self.text_area = text_area

class StdoutRedirector(IORedirector):
    '''A class for redirecting stdout to this Text widget.'''
    def write(self,str):
        self.text_area.write(str,False)
 

and then, in your Tkinter widget:

# To start redirecting stdout:
import sys
sys.stdout = StdoutRedirector( self )
# (where self refers to the widget)

# To stop redirecting stdout:
sys.stdout = sys.__stdout__
answered Apr 10 by SDeb
• 13,160 points

Related Questions In Python

0 votes
1 answer
+1 vote
1 answer

How to use GUI that comes with Python to test your code?

Hey @alex0809, When your testing a website ...READ MORE

answered Sep 24, 2018 in Python by Vardhan
• 12,530 points
73 views
0 votes
1 answer
0 votes
1 answer

How to I clear Tkinter Canvas using Python?

To clear a canvas, use the delete method.  This ensures ...READ MORE

answered Dec 24, 2018 in Python by Nymeria
• 3,520 points
1,555 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 232 views
0 votes
1 answer

Tkinter to android translation

Even for native purposes, most developers would ...READ MORE

answered May 22 in Python by SDeb
• 13,160 points
47 views
0 votes
1 answer

How to convert string into epoch time?

you are passing the parsed datetime object to ...READ MORE

answered Sep 21, 2018 in Python by SDeb
• 13,160 points
137 views