如何在python中打开shell窗口输出打印实时调试信息



我可以在不中断代码执行序列的情况下打开一个shell窗口来打印python中的调试信息吗?

例如:

def foo(b):
    print b
for a in range(0, 100):
    print a       # normally this will be used for on-the-fly simple debugging purpose 
                  # and will mess the console output of foo()          
    foo(a)

我能做点什么吗:

newshell = <new cmd.exe or bash>
for a in range(0, 100):
    newshell.print(a)       # information will be printed to new opened shell  
    foo(a)

提前谢谢。

您可以:

  1. 使用python日志记录。http://docs.python.org/2/library/logging.html
  2. 只需从终端写入文件即可:$tail-f output.txt
  3. 写入您的盒子中已经打开的终端(即linux),写入文件描述符/dev/pts/X(使用X:1,2…,您可以使用$who看到正确的数字)

我们不需要shell来打印一些东西。您可以使用Tkinter文本小部件:

from Tkinter import Tk, Text, END
root = Tk()
text = Text(root)
text.pack()
def foo(b):
    print b
for a in range(0, 100):
    text.insert(END, str(a)+"n")
    text.see(END)
    text.update()
    foo(a)
text.wait_window()  # wait until we close the window

最新更新