在OSX上隐藏Tkinter应用程序的控制台 [英] Hide console for Tkinter app on OSX

查看:107
本文介绍了在OSX上隐藏Tkinter应用程序的控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我启动基于GUI Tkinter的应用程序时,我试图隐藏终端,但是当我双击OSX上的app.py文件时,将显示终端"窗口.我尝试将扩展名更改为.pyw,并尝试使用/usr/bin/pythonw启动它,但是无论如何,仍然会出现终端"窗口.

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.

我什至尝试添加下面的try/except,但是当我运行它时,出现错误:在出现的终端"窗口中出现无效的命令名称控制台"".

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?

推荐答案

通过双击OS X上的.py文件,您很可能通过OS X Python随附的Python Launcher.app启动Python gui实例.您可以通过在Finder中选择.py文件并在其上执行Get Info来进行验证. Python Launcher是一款非常简单的应用程序,可通过Terminal.app命令启动Python.要直接启动自己的Python GUI应用程序,首选方法是使用 py2app 创建一个简单的应用程序>. 此处.

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.

当然,还有其他方法,但是很可能其中任何一种都将添加更多级别的间接访问.要制作普通的可启动双击"应用程序,您需要某种应用程序结构.这就是py2app允许您直接创建的内容.

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.

一个非常简单的选择是利用AppleScript编辑器创建启动器应用程序的能力.在AppleScript编辑器中:

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

    在OS X 10.6中<​​li>

    /Applications/Utilities/AppleScript Editor.app

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

/Applications/AppleScript/Script Editor.app in 10.5

/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 &"

,然后按Save As..File Format -> Application.然后,您将有一个可双击的应用程序,它将启动另一个应用程序.您可以使用Apple的Automater.app创建类似的内容.但是,在幕后,他们正在做与py2app为您所做的事情类似的事情,只是在顶层有更多层.

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.

这篇关于在OSX上隐藏Tkinter应用程序的控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆