【问题标题】:TKinter redirect terminal outputTKinter 重定向终端输出
【发布时间】:2019-03-01 23:21:35
【问题描述】:

我使用 Tkinter 在 Python 中制作了一个软件,用于图形和 ctypes,以便与公司给我的一些 C 库接口。 我在终端上打印输出的很多操作,所以我想知道:有没有一种简单的方法可以将这些输出消息重定向到另一个 tkinter 窗口? 例如,我想打印一个 cout

【问题讨论】:

    标签: python python-2.7 tkinter


    【解决方案1】:

    你可以覆盖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()
    

    现在写入 stdout 的所有内容,包括对 print 的调用,都将发送到您的 WriteToWindow 类。

    【讨论】:

    • 你能用这段代码做一个例子吗? libC = CDLL('/usr/lib/stuff.so', mode = RTLD_GLOBAL) def func(): libC.Show('TEST') #这是一个简单的函数 cout
    • 经过一番思考,感谢您的评论,我解决了这个问题。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2023-03-03
    • 2022-11-11
    • 2017-12-01
    • 2014-07-05
    • 2021-01-27
    • 2013-11-29
    • 2014-10-11
    • 2017-03-14
    相关资源
    最近更新 更多