views:

394

answers:

2

I'm trying to hide the Terminal when I launch a GUI Tkinter based app, but when I double click the app.py file on OSX, the Terminal window appears. I've tried changing the extension to .pyw and tried launching it with /usr/bin/pythonw, but no matter what, the Terminal window still appears.

I've even tried adding the try/except below, but when I run it I get the error: 'invalid command name "console"' in the Terminal window that appears.

from Tkinter import *

class MainWindow(Tk):
    def __init__(self):
        Tk.__init__(self)
        try:
            self.tk.call('console', 'hide')
        except TclError, err:
           print err

win = MainWindow()
win.mainloop()

I haven't been able to find any way to hide the Terminal window from appearing. Anybody got any ideas?

+4  A: 

By double-clicking a .py file on OS X, you are likely launching a Python gui instance via the Python Launcher.app supplied with OS X Pythons. You can verify that by selecting the .py file in the Finder and doing a Get Info on it. Python Launcher is a very simple-minded app that starts Python via a Terminal.app command. To directly launch your own Python GUI app, the preferred approach is to create a simple app using py2app. There's a brief tutorial here.

EDIT:

There are other ways, of course, but most likely any of them would be adding more levels of indirection. To make a normal launchable, "double-clickable" application, you need some sort of app structure. That's what py2app lets you create directly.

A very simple-minded alternative is to take advantage of the AppleScript Editor's ability to create a launcher app. In the AppleScript editor:

  • /Applications/Utilities/AppleScript Editor.app in OS X 10.6

  • /Applications/AppleScript/Script Editor.app in 10.5

make a new script similar to this:

do shell script "/path/to/python /path/to/script.py &> /dev/null &"

and then Save As.. with File Format -> Application. Then you'll have a double-clickable app that will launch another app. You can create something similar with Apple's Automater.app. But, under the covers, they are doing something similar to what py2app does for you, just with more layers on top.

Ned Deily
Thanks for the link. So is it safe to say that there is no way of simply suppressing the Terminal from launching without using py2app?
cdwilson
See EDIT above.
Ned Deily
A: 

'console hide' doesn't hide the Terminal in OS X. It hides Tk's built-in console, which is really a relic from the MacOS Classic days (and which is still commonly used on Windows).

Kevin Walzer