简体   繁体   中英

TKinter redirect terminal output

I made a software in Python using Tkinter, for the graphics, and ctypes in order to interface to some C libraries the firm gave me. A lot of operations I do print an output on the terminal, so I was wondering: is there an easy way for redirecting these output messages to another tkinter window? For instance, I'd like to print a cout << "Hello World!" in a new tkinter winter, linked to the root one, instead of the terminal, is this possible?

You can override sys.stdout :

class WriteToWindow():
    def write(self, text):
        ...  # write text to your window

    def flush(self):
        pass  # this method should exist, but doesn't need to do anything

import sys
sys.stdout = WriteToWindow()

Now everything written to stdout, which includes calls to print , will be sent to your WriteToWindow class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM