views:

474

answers:

4

How to get rid of the console that shows up as standard output when running wxPython programs in Windows?

+3  A: 

I don't know wxPython but the solution might be as simple as using pythonw.exe to run the program instead of python.exe.

Dave Webb
and similarly, rename your file to whatever.pyw (instead of whatever.py)so that when you click on it, it will launch pythonw.exe instead of python.exe
Moe
+6  A: 

Not familiar with wxPython, but if you invoke your script with pythonw.exe rather than python.exe, the console window shouldn't appear. I believe saving the script as script.pyw also works.

AlexJReid
If it is .pyw file and your python is associated in windows you can just double-click the file and it will start without console
Perica Zivkovic
I have all my GUI code in a module and I have a main script with the extension PY. DO i have to rename all the files to PYW? Even the __init__.py file?
Mridang Agarwalla
No. Just the "main" script.
AlexJReid
+4  A: 

Others have already suggested of renaming from py to pyw. If you instead refer to Output redirection pass redirect=True when creating the wx.App class.

See for instance http://www.wxpython.org/docs/api/wx.App-class.html

The signature of __init__ is

__init__(self, redirect=False, filename=None, useBestVisual=False, clearSigInt=True)

If you set redirect=True and filename different from None, sys.stdout and sys.stderr will be redirect to filename. Note that on Windows and Mac redirect default value is True. If redirect==True and filename is None, the output will be printed in a popup window different from your other frames. This can be very useful so that while debugging you can follow what is happening, while not cluttering the user interface with the internals of your app in release mode.

Francesco
+4  A: 

The easiest way to do this:

if __name__ == "__main__":
    app = wx.App(0) #<--- or False
    frame = MyFrame('My Frame')
    frame.Show(True)
    app.MainLoop()

This prints to stdout instead of the wxPython window.

DrBloodmoney