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, 2019 in Python by ana1504.k
• 7,910 points
4,804 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, 2019 by SDeb
• 13,300 points

Related Questions In Python

0 votes
1 answer

How to invoke method on GUI thread but without have that method in QMainWindow class (Pyqt)

It is possible to handle this by ...READ MORE

answered Sep 12, 2018 in Python by Priyaj
• 58,020 points
2,644 views
+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
• 13,150 points
963 views
0 votes
1 answer

How to invoke method on GUI thread but without have that method in QMainWindow class (Pyqt)

It is possible to handle this by ...READ MORE

answered Sep 24, 2018 in Python by Priyaj
• 58,020 points
3,125 views
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,560 points
15,816 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,463 views
0 votes
1 answer
0 votes
1 answer

Tkinter to android translation

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

answered May 22, 2019 in Python by SDeb
• 13,300 points
2,051 views
0 votes
1 answer

How to convert string into epoch time?

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

answered Sep 22, 2018 in Python by SDeb
• 13,300 points
6,832 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP