我应该如何在 Windows 上启动可移植的 Python Tkinter 应用程序而不显得丑陋?

发布于 10-11 11:28 字数 1294 浏览 9 评论 0 原文

我使用 Tkinter 在 python 中编写了一个简单的 GUI 程序。我们将此程序称为“gui.py”。我的用户使用 Portable Python 在 Windows 计算机上通过 USB 密钥运行“gui.py”;在主机上安装任何东西都是不可取的。

我希望我的用户通过双击 USB 密钥根目录下的图标来运行“gui.py”。我的用户不关心 python 是什么,如果不需要,他们也不想使用命令提示符。我不希望他们关心 USB 密钥分配的驱动器号。我希望它可以在 XP、Vista 和 7 上运行。

我的第一个丑陋的解决方案是在 USB 密钥的根目录中创建一个快捷方式,并将快捷方式的“目标”属性设置为“(root) \App\pythonw.exe (root)\App\gui.py",但我无法弄清楚如何在 Windows 快捷方式中执行相对路径,并且使用像“E:”这样的绝对路径似乎很脆弱。

我的下一个解决方案是在 USB 密钥的根目录中创建一个 .bat 脚本,如下所示:

@echo off
set basepath=%~dp0
"%basepath%App\pythonw.exe" "%basepath%\App\gui.py"

这似乎并不关心 USB 密钥分配的驱动器号,但它确实在我的程序时使 DOS 窗口保持打开状态运行。实用,但丑陋。

接下来我尝试了这样的 .bat 脚本:(

@echo off
set basepath=%~dp0
start "" "%basepath%App\pythonw.exe" "%basepath%\App\gui.py"

参见 这里解释了有趣的引用)

现在,在我的 GUI 打开之前,DOS 窗口在屏幕上短暂闪烁。少一点丑!还是丑啊

真正的男人如何处理这个问题呢?从 USB 记忆棒在 Windows 机器上启动 python Tkinter GUI 的最不丑陋的方法是什么?

编辑: 下面的所有答案都非常好(py2exe、pyinstaller、小.exe、.wsf 脚本。).wsf 解决方案是最简单的,所以我现在使用它。如果我想要更漂亮的图标和标准的 .exe 扩展名,我可能最终会切换到其他三种解决方案之一。谢谢大家!

I've written a simple GUI program in python using Tkinter. Let's call this program 'gui.py'. My users run 'gui.py' on Windows machines from a USB key using Portable Python; installing anything on the host machine is undesirable.

I'd like my users to run 'gui.py' by double-clicking an icon at the root of the USB key. My users don't care what python is, and they don't want to use a command prompt if they don't have to. I don't want them to have to care what drive letter the USB key is assigned. I'd like this to work on XP, Vista, and 7.

My first ugly solution was to create a shortcut in the root directory of the USB key, and set the "Target" property of the shortcut to something like "(root)\App\pythonw.exe (root)\App\gui.py", but I couldn't figure out how to do a relative path in a windows shortcut, and using an absolute path like "E:" seems fragile.

My next solution was to create a .bat script in the root directory of the USB key, something like this:

@echo off
set basepath=%~dp0
"%basepath%App\pythonw.exe" "%basepath%\App\gui.py"

This doesn't seem to care what drive letter the USB key is assigned, but it does leave a DOS window open while my program runs. Functional, but ugly.

Next I tried a .bat script like this:

@echo off
set basepath=%~dp0
start "" "%basepath%App\pythonw.exe" "%basepath%\App\gui.py"

(See here for an explanation of the funny quoting)

Now, the DOS window briefly flashes on screen before my GUI opens. Less ugly! Still ugly.

How do real men deal with this problem? What's the least ugly way to start a python Tkinter GUI on a Windows machine from a USB stick?

EDIT:
All the answers below were very good (py2exe, pyinstaller, small .exe, .wsf script.) The .wsf solution was the simplest, so I'm using it for now. I'll probably end up switching to one of the other three solutions if I want a prettier icon and the standard .exe extension. Thanks, everyone!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

余生一个溪 2024-10-18 11:28:18

可以使用此 Windows Scripting Host 脚本(文件扩展名 .wsf)代替批处理文件:

<job>
<script language="VBScript">
set WshShell = WScript.CreateObject("WScript.Shell")
CMDFile = "App\\pythonw.exe App\\gui.py"
WshShell.Run CMDFile, 1
</script>
</job> 

更新:或者编译此 C 程序并链接图标资源:

#include <windows.h>
#include <process.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
                   LPSTR lpCmdLine, int nCmdShow)
{
    return _spawnl(_P_NOWAIT, "App/pythonw.exe", " App/gui.py", lpCmdLine, NULL);
}

更新 2:要构建带有图标的 App.exe,请将 C 代码保存到app.c,创建 Windows 图标文件 app.ico,并将以下行保存到 app.rc:

appicon ICON "app.ico"

使用 Visual Studio 2008 Express,运行以下命令:

"C:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat"
rc.exe app.rc
cl.exe app.c /FeApp.exe /link app.res

或者使用“Visual Studio 2010 Express”或“Microsoft Windows SDK v7.0 for Windows 7 和 .NET Framework 3.5 Service Pack 1”并相应地调整命令。

请注意,该图标仅用于 App.exe 启动程序,而不用于您的 Python 程序。

This Windows Scripting Host script (file extension .wsf) can be used instead of the batch file:

<job>
<script language="VBScript">
set WshShell = WScript.CreateObject("WScript.Shell")
CMDFile = "App\\pythonw.exe App\\gui.py"
WshShell.Run CMDFile, 1
</script>
</job> 

Update: Alternatively compile this C program and link an icon resource:

#include <windows.h>
#include <process.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
                   LPSTR lpCmdLine, int nCmdShow)
{
    return _spawnl(_P_NOWAIT, "App/pythonw.exe", " App/gui.py", lpCmdLine, NULL);
}

Update 2: To build an App.exe with icon, save the C code to app.c, create an Windows icon file app.ico, and save the following line to app.rc:

appicon ICON "app.ico"

Using Visual Studio 2008 Express, run these commands:

"C:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat"
rc.exe app.rc
cl.exe app.c /FeApp.exe /link app.res

Alternatively use "Visual Studio 2010 Express" or "Microsoft Windows SDK v7.0 for Windows 7 and .NET Framework 3.5 Service Pack 1" and adjust the commands accordingly.

Note that the icon will only be used for the App.exe starter program, not your Python program.

吃兔兔 2024-10-18 11:28:18

使用 pyinstaller 来压缩你的发行版(相对于 py2exe 的优点是它知道不同的版本)第三方库并且通常是最新的)。

然后,您可以创建一个 .exe 供用户单击以启动您的应用程序。如果您只是将 pyinstaller 构建的结果复制到 USB 驱动器上,应该没问题。

Use pyinstaller to zip up your distribution (the advantage over py2exe is that it knows different third-party libraries and is generally more up-to-date).

You can then create a .exe for your users to click upon to start your application. If you just copy the results of the pyinstaller build onto your USB drive you should be fine.

初心 2024-10-18 11:28:18

使用 py2exe 将其变成单个可执行文件。

Make it to a single executable using py2exe.

我偏爱纯白色 2024-10-18 11:28:18

您可以通过编写自己的小 C 应用程序来调用 system('start "" "%basepath%App\pythonw.exe" "%basepath%\App\gui.py"' 以一种 hacky 的方式来完成此操作)。接下来,您无需控制台即可编译它,并将其用作“快捷方式”。

You could do this in a hacky manner by writing you're own little C application that calls system('start "" "%basepath%App\pythonw.exe" "%basepath%\App\gui.py"'). Next you compile it without console and use it as a "shortcut".

野生奥特曼 2024-10-18 11:28:18

简短回答:

这个问题是几年前提出的,但我最近为我正在开发的一个程序找到了一个解决方案,该解决方案可能对其他人仍然有用。通过这种方法,您将能够创建一个独立的 exe 程序启动器,它可以放置在任何地方并引用同一文件夹或子目录中的文件,同时具有您选择的漂亮图标并且不会弹出 DOS 屏幕。换句话说,一个真正好看的相对路径可传输快捷方式文件:)

即使对于非程序员来说,该解决方案也应该易于遵循和执行,如下所示:

  1. 打开记事本
  2. 写入:%windir%\system32\cmd.exe / c 开始 "" "%CD%\可选
    subfolder\mainpy2exeGUI.exe”
  3. 另存为“whatever.bat”
  4. 使用名为“BAT to EXE converter”的程序将bat文件转换为exe文件
    勾选“隐藏应用程序”选项,然后选择
    您想要的图标文件位于“版本信息”选项卡下。你可以命名
    输出的exe文件可以是你想要的任何文件。转换器链接
    程序可以在以下位置找到:
    http: //www.freewaregenius.com/how-to-create-shortcuts-with-a-relative-path-for-use-on-usb-drives/
    转换器程序下载包含 32 位和 64 位版本,使用 32 位版本使旧版和新版 PC 都可以使用该快捷方式。

(请注意,此解决方案与 http://www.freewaregenius.com/how-to-create-shortcuts-with-a-relative-path-for-use-on-usb-drives/然而,当前的解决方案在步骤 2 中使用的代码方面有所不同,它允许启动程序放置在计算机上的任何位置,而不仅仅是 USB 记忆棒的顶部目录,并且新强调了不可见选项。应该检查这些差异。)

更多详细信息(可选):

最初的问题是:“从 USB 记忆棒在 Windows 计算机上启动 python Tkinter GUI 的最不丑陋的方法是什么?”

需要什么可以分为四件事:
1. exe程序启动器。
2. 适用于任何计算机和任何目录(即支持相对路径)。
3. 有一个图标。
4. 这不会打开“丑陋的”DOS 窗口。

建议了几种可能的解决方案,但到目前为止还没有一个能够满足所有标准。最初的海报选择了“.wsf”选项,该选项允许相对路径并且没有丑陋的 DOS 窗口,但不允许自定义图标或可识别的 exe 文件。

先前建议的解决方案的部分问题包括:

  1. 您没有 C/VB 编程技能或软件。
  2. 您想要启动程序的图标。使用执行“cmd”并使用它打开 GUI 文件的快捷方式文件将允许您设置图标文件,但图标文件引用是绝对的,并且在除您创建快捷方式文件的计算机之外的任何其他计算机上都会失败。
  3. 你不想要“丑陋”的 DOS 窗口闪烁。上一点提到的 cmd 快捷方式解决方案会创建一个 DOS 窗口,该窗口在打开 GUI 之前会闪烁。
  4. 将 py2exe 主可执行文件作为程序启动器几乎是一个完美的解决方案,因为它满足所有标准,但它的缺点是 py2exe 可执行文件需要将一个丑陋的“tlc”文件夹放置在同一顶级目录中。因此,最好将主 py2exe 启动器隐藏在一个命名良好的子文件夹中。此外,在很多情况下,人们希望将程序启动器和程序本身保留为单独的 exe 文件,例如,如果您仅使用主 py2exe 程序作为 python 运行程序运行,可以启动开放式可编辑 python您可以随时编辑这些脚本,而无需在每次更改脚本之一时创建新的 py2exe 文件。

The Short Answer:

This question was asked a few years ago, but I recently found a solution for a program I was working on that may still be useful for others. With this method, you will be able to create a standalone exe program launcher that can be placed anywhere and refer to a file in its same folder or subdirectory, while having a pretty icon of your choice and no DOS screen popping up. In other words, a true good-looking relative-path transportable shortcut file :)

The solution should be easy to follow and do even for non-programmers and goes as follows:

  1. open notepad
  2. write: %windir%\system32\cmd.exe /c start "" "%CD%\optional
    subfolder\mainpy2exeGUI.exe"
  3. save as "whatever.bat"
  4. convert the bat-file to an exe file using a program called "BAT to EXE converter"
    while checking the "invisible application" option, and selection the
    icon file you want under the "versioninformations" tab. You can name
    the output exe file to whatever you want. Link to the converter
    program can be found at
    http://www.freewaregenius.com/how-to-create-shortcuts-with-a-relative-path-for-use-on-usb-drives/
    The converter program download contains a 32 and 64-bit version, use the 32-bit version to make the shortcut usable by both older and newer PCs.

(note, this solutions is almost the same as suggested at http://www.freewaregenius.com/how-to-create-shortcuts-with-a-relative-path-for-use-on-usb-drives/. However the current solution is different in terms of the code it uses in step2 which allows the launcher progam to be placed anywhere on a computer and not just on the top directory of a USB-stick, and is new to emphasize that the invisible option should be checked. Those differences are crucial.)

More Details (optional):

The original question was: "What's the least ugly way to start a python Tkinter GUI on a Windows machine from a USB stick?"

What was needed can be broken down to four things:
1. An exe program launcher.
2. That works on any computer and in any directory (i.e. it supports relative paths).
3. That has an icon.
4. That does not open an "ugly" DOS window.

There were several possible solutions suggested but none so far that satisfies all criteria. The original poster went for the ".wsf" option which allowed for relative paths and no ugly DOS window, but did not allow a custom icon or the recognizable exe file.

Part of the problem with the previously suggested solutions include:

  1. you do not have C/VB programming skills or software.
  2. you want an icon to your launcher program. Using a shortcut file that executes "cmd" and uses it to open your GUI file will allow you to set an icon file, BUT the icon file reference is absolute and will fail on any other computer than the one you created the shortcut file on.
  3. you do not want the "ugly" DOS window flash. The cmd shortcut solution mentioned in the previous point creates a DOS window that flashes before opening your GUI.
  4. Making the py2exe main executable file as the program launcher would almost be a perfect solution because it satisfies all criteria, but a backdraw with it is that the py2exe ececutable would require an ugly "tlc" folder to be placed in the same top-directory. It is therefore better to hide the main py2exe launcher in a nicely named subfolder. Also, there are many cases where one would like to keep the program launcher and the program itself as separate exe files, for instance if you are only using your main py2exe program to function as a python-runner that can launch open-ended editable python scripts that you can edit on the go without having to create a new py2exe file for each time you make a change to one of your scripts.
尽揽少女心 2024-10-18 11:28:18

您还可以分叉 GitHub 上的可移植 Python 源,并以创建其他可移植 Python 快捷方式的方式创建快捷方式。

这为您提供了启动应用程序、图标的好方法,如果需要,您可以设置自定义注册表/环境变量等。

作为示例,您可以使用 "="" rel="nofollow">可移植的 Python 源代码。

You can also fork Portable Python sources on GitHub and create shortcut in the same way other Portable Python shortcuts are created.

This gives you nice way to start app, icon, you can set custom registry/env variables if you need to, etc etc.

As an example you can take e.g. IDLE shortcut from Portable Python sources.

蝶舞 2024-10-18 11:28:18

我制作了一个批处理脚本(PyRunEXE),它编译一个简单的汇编语言代码来为您制作一个 EXE 启动器:

https: //github.com/SzieberthAdam/pyrunexe

I've made a batch script (PyRunEXE) which compiles a simple Assembly Language code to make an EXE launcher for you:

https://github.com/SzieberthAdam/pyrunexe

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文