我使用 NppExec/Notepad++ 编写 Python 脚本。如何在执行每一行 Python 代码时更新控制台窗口?例如,以下定时器脚本:

#!usr/bin/env python
import time
import threading

class Timer(threading.Thread):
    def __init__(self, seconds):
        self.runTime = seconds
        threading.Thread.__init__(self)
    def run(self):
        counter = self.runTime
        for sec in range(self.runTime):
            print counter
            time.sleep(1.0)
            counter -= 1
        print "Done."

if __name__ == '__main__':
    t = Timer(10)
    t.start()

当我在命令提示符窗口中运行它时,它每秒都会实时更新。但在 NppExec 控制台中,它仅在退出后更新。有没有办法让 NppExec 控制台充当命令提示符并持续更新?

有帮助吗?

解决方案 2

所有需要做的就是调用 python -u 运行脚本的命令,即 python -u timer.py 代替 python timer.py

默认情况下,这会以无缓冲模式打印输出。对于更详细的程序中的自定义输出刷新, stdout 定义必须是 重写.

其他提示

不熟悉特定控制台应用程序如何处理输出,但是 stdout 默认情况下是缓冲的,这可能是这里的问题。看 这个问题 了解禁用它的方法。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top