0

I am using tkinter. I have the following code printing out the console output to GUI in live mode. Now it simple prints the numbers (delay is needed so that I can see that it is in real-life).

import tkinter as tk
import time
import sys

class Display(tk.Frame):
    def __init__(self):
       tk.Frame.__init__(self)
       self.doIt = tk.Button(self,text="Start", command=self.start, background = 'black', fg='white')
       self.doIt.pack()
       self.output = tk.Text(self, width=100, height=15, background = 'black', fg='white')
       self.output.pack()
       sys.stdout = self
       self.configure(background='black')
       self.pack()


    def start(self):
        for i in range (1, 1000):
            print (i)
            time.sleep(2)


    def write(self, txt):
        self.output.insert(tk.END,str(txt))
        self.update_idletasks()


if __name__ == '__main__':
    Display().mainloop()

However, when I try to fold the window or switch to another application it freezes. Another thing is that I want it to scroll down and show new output as it reaches the end of the window. I would appreciate any help on these issues.

4
  • I don't see how you can get any output from the posted code, but maybe am I missing something? Commented Apr 22, 2016 at 13:50
  • well, I took this code from the web and modified it a bit and it works. It prints the output of the console to the text widget Commented Apr 22, 2016 at 13:56
  • Good for you, I could not make it work... but I see that your question was edited, maybe the first paste was not good...edit No luck :( Commented Apr 22, 2016 at 13:57
  • I am not stick to this one, so if you can suggest any other way of putting output to the console in real-time, I would be very grateful Commented Apr 22, 2016 at 13:58

2 Answers 2

2

I recommend you don't use time.sleep() in the Tkinter event loop as it will cause the application to go unresponsive during the sleep call. Because of this I suggest you use the .after() call.

Regarding the scrolling, you simply need to add a scrollbar to your text widget. Here's an example:

import tkinter as tk
import time
import sys

class Display(tk.Frame):
    def __init__(self):
       tk.Frame.__init__(self)
       self.doIt = tk.Button(self,text="Start", command=self.start, background = 'black', fg='white')
       self.doIt.pack()

       self.output = tk.Text(self, width=100, height=15, background = 'black', fg='white')
       self.output.pack(side=tk.LEFT)

       self.scrollbar = tk.Scrollbar(self, orient="vertical", command = self.output.yview)
       self.scrollbar.pack(side=tk.RIGHT, fill="y")

       self.output['yscrollcommand'] = self.scrollbar.set

       self.count = 1
       self.configure(background='black')
       self.pack()


    def start(self):
        if self.count < 1000:
            self.write(str(self.count) + '\n')
            print (self.count)
            self.count += 1
            self.after(2000, self.start)


    def write(self, txt):
        self.output.insert(tk.END,str(txt))
        self.update_idletasks()


if __name__ == '__main__':
    Display().mainloop()
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I was using sleep() just to test if it works and could never think that it will cause issues haha
1

This now outputs both in the console and in the output tk window: What was missing is a call to self.write(). I also suppressed the diverting of sysout to the window.

import tkinter as tk
import time
import sys

class Display(tk.Frame):
    def __init__(self):
       tk.Frame.__init__(self)
       self.doIt = tk.Button(self,text="Start", command=self.start, background = 'black', fg='white')
       self.doIt.pack()
       self.output = tk.Text(self, width=100, height=15, background = 'black', fg='white')
       self.output.pack()
#       sys.stdout = self
       self.configure(background='black')
       self.pack()


    def start(self):
        for i in range (1, 1000):
            self.write(str(i) + '\n')
            print (i)
            time.sleep(2)


    def write(self, txt):
        self.output.insert(tk.END,str(txt))
        self.update_idletasks()


if __name__ == '__main__':
    Display().mainloop()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.