在 OSX 上隐藏 Tkinter 应用程序的控制台
当我启动基于 GUI Tkinter 的应用程序时,我试图隐藏终端,但是当我双击 OSX 上的 app.py 文件时,会出现终端窗口。我尝试将扩展名更改为 .pyw 并尝试使用 /usr/bin/pythonw 启动它,但无论如何,终端窗口仍然出现。
我什至尝试添加下面的 try/ except ,但是当我运行它时,我在出现的终端窗口中收到错误:“无效的命令名称“控制台””。
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'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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通过在 OS X 上双击
.py
文件,您可能会通过 OS X Python 提供的Python Launcher.app
启动 Python gui 实例。您可以通过在 Finder 中选择.py
文件并对其执行获取信息
来验证这一点。 Python Launcher 是一个非常简单的应用程序,它通过 Terminal.app 命令启动 Python。要直接启动您自己的 Python GUI 应用程序,首选方法是使用 py2app。 这里有一个简短的教程。编辑:
当然还有其他方法,但很可能其中任何一种都会增加更多的间接级别。要制作一个正常的可启动、“可双击”的应用程序,您需要某种应用程序结构。这就是
py2app
让您直接创建的内容。一个非常简单的替代方案是利用 AppleScript 编辑器创建启动器应用程序的功能。在
AppleScript
编辑器中:/Applications/Utilities/AppleScript
OS X 10.6 中的 Editor.app
/Applications/AppleScript/Script
10.5 中的 Editor.app
创建一个与此类似的新脚本:
然后使用
File Format ->
。然后您将拥有一个可双击的应用程序,该应用程序将启动另一个应用程序。您可以使用 Apple 的Save As..
应用程序Automater.app
创建类似的内容。但是,在幕后,他们所做的事情与py2app
为您做的事情类似,只是顶部有更多层。By double-clicking a
.py
file on OS X, you are likely launching a Python gui instance via thePython Launcher.app
supplied with OS X Pythons. You can verify that by selecting the.py
file in the Finder and doing aGet 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
in OS X 10.6Editor.app
/Applications/AppleScript/Script
in 10.5Editor.app
make a new script similar to this:
and then
Save As..
withFile Format -> Application
. Then you'll have a double-clickable app that will launch another app. You can create something similar with Apple'sAutomater.app
. But, under the covers, they are doing something similar to whatpy2app
does for you, just with more layers on top.添加到 Ned Deily 的答案中,就我而言,当我尝试使用 AppleScript 应用程序启动 Python 应用程序时,它最初不起作用。我发现它与某种编码错误有关(我使用的是 UTF-8,过去我觉得需要将其配置为 UTF-8)。
因此,经过进一步调查,我发现可以通过使用以下代码创建 AppleScript 应用程序来实现此目的(根据需要调整 python3 和 Python 应用程序的路径):
do shell script "export LC_ALL=en_US.UTF -8; 导出 LANG=en_US.UTF-8; /usr/local/bin/python3 '/Users/USER/FOLDER/SCRIPT.py' &> /dev/null &"
没有任何终端窗口的 Python 应用程序。然后,AppleScript 应用程序可以像往常一样使用自定义图标进行个性化,并且可以放置在 Dock 中。单击时,它将启动 Python 解释器,该解释器仍显示在 Dock 中,但没有可见窗口。
我认为这可能对其他用户有用。
Adding to the answer by Ned Deily, im my case when I tried to launch the Python application using an AppleScript application, it did not work initially. I discovered that it has something to to with some kind of encoding error (I am using UTF-8 and in the past I had felt need to configure it to UTF-8).
So, after further investigation, I discovered that I can accomplish this by creating an AppleScript application with the following code (adjusting the paths of python3 and of the Python application as needed):
do shell script "export LC_ALL=en_US.UTF-8; export LANG=en_US.UTF-8; /usr/local/bin/python3 '/Users/USER/FOLDER/SCRIPT.py' &> /dev/null &"
It launches the Python application without any Terminal windows. The AppleScript application can then be personalised with a custom icon as usual, and can be placed in the Dock. When clicked, it will launch the Python intepreter, that still shows up in Dock, but with no visible windows.
I think this may be useful to other users.
“console hide”不会隐藏 OS X 中的终端。它隐藏了 Tk 的内置控制台,这实际上是 MacOS Classic 时代的遗物(并且在 Windows 上仍然常用)。
'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).