Python 2.7(使用 Tkinter)项目转换成独立的 exe?

发布于 2024-12-05 03:36:44 字数 1219 浏览 2 评论 0原文

我已经搜索这个多年并尝试了一切。只是无法让它发挥作用。

我想做的是为我的小程序制作一个独立的 exe,该程序使用 Tkinter,适用于 Windows。到目前为止,我已经将其制作为 exe,但是 dist/ 文件夹包含一个文件夹 tcl/ 和一个额外的 exe 文件。如何摆脱这些?

另外,我还无法制作一个有效的exe。这是我的 setup.py

from distutils.core import setup
import py2exe
import sys
if len(sys.argv) == 1:
    sys.argv.append("py2exe")

setup(
    options = {
        "py2exe": {"compressed": 1, "optimize": 2, "ascii": 1, "bundle_files": 1, "includes": ["Tkinter"]}
        },
        zipfile = None,
        windows = [
        {
            "script": "hello.py",
            "icon_resources": [(0, "icon_16.ico")]
        }
    ]
)

使用此代码,我在运行后立即收到“此程序已停止响应”的通知。

通过删除选项“ascii”,该程序将无法工作并留下一个日志文件:

Traceback (most recent call last):
  File "hello.py", line 1, in <module>
  File "zipextimporter.pyo", line 82, in load_module
  File "Tkinter.pyo", line 38, in <module>
  File "zipextimporter.pyo", line 82, in load_module
  File "FixTk.pyo", line 56, in <module>
  File "FixTk.pyo", line 23, in convert_path
LookupError: unknown encoding: mbcs

我对Python非常乐观,因为它看起来非常适合编码和寻找帮助,但如果没有这个,我就无法编码任何东西。任何帮助表示赞赏。我是一个完全的新人。

I've searched this for ages and tried everything. Just can't make it work.

What I want to do is to make a stand-alone exe of my small program that uses Tkinter, for Windows. This far I've accomplished to make it an exe, however the dist/ folder including a folder tcl/ and an extra exe-file. How to get rid of these?

Also, I haven't been able to make a working exe. Here's my setup.py:

from distutils.core import setup
import py2exe
import sys
if len(sys.argv) == 1:
    sys.argv.append("py2exe")

setup(
    options = {
        "py2exe": {"compressed": 1, "optimize": 2, "ascii": 1, "bundle_files": 1, "includes": ["Tkinter"]}
        },
        zipfile = None,
        windows = [
        {
            "script": "hello.py",
            "icon_resources": [(0, "icon_16.ico")]
        }
    ]
)

With this code I get a notice "This program has stopped responding" instantly after running it.

By removing the option "ascii", the program doesn't work and leaves a logfile:

Traceback (most recent call last):
  File "hello.py", line 1, in <module>
  File "zipextimporter.pyo", line 82, in load_module
  File "Tkinter.pyo", line 38, in <module>
  File "zipextimporter.pyo", line 82, in load_module
  File "FixTk.pyo", line 56, in <module>
  File "FixTk.pyo", line 23, in convert_path
LookupError: unknown encoding: mbcs

I was very optimistic about Python as it seemed really nice to code and find help for, but this is something I can't code anything without. Any help is appreciated. I'm a total newcomer.

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

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

发布评论

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

评论(3

猫九 2024-12-12 03:36:44

我认为它捆绑 tkinter 的方式有一个错误,你无法在 python 中优化或捆绑 tkinter,取出这些选项并尝试运行它。

I think there is a bug with the way that it bundles tkinter, you cannot optimize or bundle tkinter in python, take those options out and try running it.

两个我 2024-12-12 03:36:44

Tkinter 需要“tcl”文件夹中的任何内容。 Tkinter 实际上链接到 Tcl 解释器(这意味着您可以在 Python 中运行 Tcl 命令!)。因此,您需要 Tcl 解释器所需的一切(包括对 Tk 的支持)。

您收到的错误是编码错误,在您的选项中我看到 "ascii": 1。摆脱它,它应该可以解决问题。

另外,如果您计划为此编译的程序制作安装程序,则没有真正的理由使用 "compressed": 1 压缩该程序(仅在分发时它需要很小)。

Anything in the "tcl" folder is required by Tkinter. Tkinter is actually linking to a Tcl interpreter (which means you can run Tcl commands in Python!). Because of this, you need everything that a Tcl interpreter needs (including support for Tk).

The error you get is an encoding error, and in your options I see "ascii": 1. Get rid of that and it should solve the problem.

Also, if you plan on making an installer for this compiled program, there is no real reason to compress the program with "compressed": 1 (it only needs to be small when it's distributed).

终难愈 2024-12-12 03:36:44

您可以尝试使用下面位置提供的替代解决方案,该解决方案基本上使用安装程序方法。我也无法将该 tcl 文件夹捆绑到 exe 中,所以我想这应该是一个更好的解决方案。

http://www.py2exe.org/index.cgi/SingleFileExecutable

You could try using an alternate solution provided at the location below which basically uses an installer method. I wasn't able to bundle that tcl folder in the exe as well, so I guess this should be a better solution.

http://www.py2exe.org/index.cgi/SingleFileExecutable

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