简体   繁体   中英

Python script displays output in command window but nothing shows in Windows

I've written a script that works great at a command prompt, but it only displays the last line if I double click on the script.py icon on my desktop.

The function in the script runs perfectly but once it finds a match it's supposed to display the output on the screen. At the end, I have an os.pause statement and THAT displays when the script is done, but nothing else displays on the screen.

I AM executing it using pythonw.exe. What else should I check?

Thank you.

pythonw supresses the console window that is created when a python script is executed. Its intended for programs that open their own GUI. Without pythonw, a python gui app would have its regular windows plus an extra console floating around. I'm not sure what pythonw does to your stdout, but os.isatty() returns False and I assume stdout/err are just dumped into oblivion. If you run a DOS command like os.system("pause") , Windows creates a new console window for that command only. That's what you see on the screen.

If you want to see your script output, you should either run with python.exe and add an input prompt at the end of the script as suggested by @GaryWalker, or use a gui toolkit like tcl/tk, Qt or wxPython.

I've used a script like before and it seemed ok to me

print("Hello world")
input("Press return to exit")

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