问题描述
我想双击我的 Python 脚本(它使用 Tkinter GUI),我只想打开 Tkinter 窗口,而不是控制台窗口.
I want to double click on my Python script (which uses a Tkinter GUI) and I just want it to open the Tkinter window, not the console window.
为此,我将扩展名从 .py 更改为 .pyw,这在 Windows 上似乎可以正常工作,但是当我在 Linux 机器上双击我的 .pyw 文件时,它不起作用.它只是冻结了,我不得不重新启动系统.
To do this, I changed the extension from .py to .pyw and this seems to work fine on Windows but when I double click my .pyw file on a Linux machine, it doesn't work. It simply froze and I had to restart my system.
请提出一个独立于平台的解决方案,以帮助我在不打开终端/命令提示符的情况下运行 Python 脚本.
Please suggest a platform-independent solution that would help me to run the Python script without opening the terminal/command prompt.
推荐答案
您可以使用 pyinstaller
为您想要的不同平台创建可执行文件.
You can use pyinstaller
to create the executables for the different platforms you want.
例如
pyinstaller 命令的语法是:
The syntax of the pyinstaller command is:
pyinstaller [options] script [script ...] | specfile
在最简单的情况下,将当前目录设置为您的程序 myscript.py 所在的位置并执行:
In the most simple case, set the current directory to the location of your program myscript.py and execute:
pyinstaller myscript.py
PyInstaller 分析 myscript.py 并:
PyInstaller analyzes myscript.py and:
Writes myscript.spec in the same folder as the script.
Creates a folder build in the same folder as the script if it does not exist.
Writes some log files and working files in the build folder.
Creates a folder dist in the same folder as the script if it does not exist.
Writes the myscript executable folder in the dist folder.
在 dist 文件夹中,您可以找到分发给用户的捆绑应用.
In the dist folder you find the bundled app you distribute to your users.
通常您在命令行上命名一个脚本.如果您命名更多,则会分析所有内容并将其包含在输出中.但是,第一个名为的脚本提供了规范文件和可执行文件夹或文件的名称.它的代码在运行时最先执行.
Normally you name one script on the command line. If you name more, all are analyzed and included in the output. However, the first script named supplies the name for the spec file and for the executable folder or file. Its code is the first to execute at run-time.
对于某些用途,您可以编辑 myscript.spec 的内容(在使用规范文件中描述).执行此操作后,将规范文件命名为 PyInstaller 而不是脚本:
For certain uses you may edit the contents of myscript.spec (described under Using Spec Files). After you do this, you name the spec file to PyInstaller instead of the script:
pyinstaller myscript.spec
例如,您可以提供脚本或规范文件的路径
You may give a path to the script or spec file, for example
pyinstaller options... ~/myproject/source/myscript.py
或者,在 Windows 上,
or, on Windows,
pyinstaller "C:\Documents and Settings\project\myscript.spec"
这篇关于如何在不打开控制台窗口的情况下运行 Python 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!