Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9249757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:16:43+00:00 2026-06-18T10:16:43+00:00

I am trying to redirect stdout into a Tkinter Text widget in real time

  • 0

I am trying to redirect stdout into a Tkinter Text widget in real time using Python 2.7.2 on Windows 7. In my code below, I intended to have a print statement appear in the Text widget every 2 seconds. What happens, instead, is that all 3 lines

Received sleep-command
Going to sleep for 2 s
Just woke up

appear simultaneously as soon as both sleep() and mod1.main() have been evaluated.

Surprisingly (to me, at least) the problem disappears when I do not redirect stdout. That is, if I comment out the line

sys.stdout = StdoutRedirector(outputPanel)

IDLE displays the print statements in real time. Can somebody explain this to me and suggest a way to get around it? Thanks in advance!

Here is my example code:

mod1.py

import time
import sys

def main():
    print('Going to sleep for 2 s')
    time.sleep(2)
    print('Just woke up')

text_GUI.py

from Tkinter import *
import sys
import time
import mod1

old_stdout = sys.stdout

class StdoutRedirector(object):

    def __init__(self, text_area):
        self.text_area = text_area

    def write(self, str):
        self.text_area.insert(END, str)
        self.text_area.see(END)

def sleep():
    print('Received sleep-command')
    time.sleep(2)
    mod1.main()

root = Tk()

# Textbox
outputPanel = Text(root, wrap='word', height = 11, width=50)
outputPanel.grid(column=0, row=0, columnspan = 2, sticky='NSWE', padx=5, pady=5)
sys.stdout = StdoutRedirector(outputPanel)

# Sleep button
sleepBtn = Button(root, text='Sleep', command=sleep)
sleepBtn.grid(row=1, column=1, sticky='E', padx=5, pady=5)

root.mainloop()

sys.stdout = old_stdout
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T10:16:45+00:00Added an answer on June 18, 2026 at 10:16 am

    Call sleepBtn.update_idletasks() before each time.sleep(2) command. Otherwise the view will not be updated before the end of the sleep procedure.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to redirect output of stdout and stderr using a file. I
I'm trying to redirect all output (stdout + stderr) of a Windows command to
Iam trying to redirect Console.Out to two text files using Console.SetOut . Console.SetOut(File.CreateText(c:\\del1.txt)); Console.WriteLine(string1);
I am trying to redirect stdout to a Label widget. The goal is to
I am trying to redirect pages using mod_rewrite to the pages with some variables
im trying to redirect users to another page if they are using internet explorer
am trying to redirect the page using a header location, but it seems using
I am trying to redirect STDOUT AND STDERR to a socket. I did: if(fork()
I'm trying to redirect stdin and stdout of a console application, so that I
I'm trying to redirect a page using javascript output using php. The problem I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.