2013-12-17 58 views
1

我可以在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) 

感謝。

+0

讓我直說吧?你想要控制檯打開?一個真正的輸出和另一個錯誤的權利?你在使用什麼操作系統? –

+0

@Paulo Bu,我想要一個跨平臺的解決方案,win32是首選,因爲* nix會更容易一些,因爲它有/ dev/tty vty pty ...但是win32沒有這樣的設備。 –

回答

0

您可以:

  1. 使用Python的日誌記錄。 http://docs.python.org/2/library/logging.html
  2. 只需寫入文件並從終端執行:$ tail -f output.txt
  3. 寫入已打開的終端(即linux),寫入文件描述符/ dev/pts/X(與X:1,2 ...,你可以看到正確的編號與$ who)
+0

謝謝,可以'記錄'發送日誌到另一個shell窗口?我不想涉及另一個臨時文件。 –

+0

是的,它可以。但這很奇怪。只要做logging.FileHandler('/ dev/pts/X')即可。 X是開放終端的號碼。 – Robert

+0

礦僅適用於* nix系統。 「pts」可能是「tty」,這取決於。看命令$誰。 – Robert

0

我們不需要只是爲了打印一些東西。您可以使用Tkinter文本widget

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