简体   繁体   English

Py2exe与Tkinter

[英]Py2exe with Tkinter

I'm trying to convert a basic tkinter GUI program to an .exe using py2exe. 我正在尝试使用py2exe将基本的tkinter GUI程序转换为.exe。 However I've run into an error using the following conversion script. 但是,我使用以下转换脚本遇到错误。

# C:\Python26\test_hello_con.py py2exe

from distutils.core import setup
import py2exe

setup(windows=[r'C:\Python26\py2exe_test_tk.py'])

C:\\Python26\\py2exe_test_tk.py is the following code C:\\ Python26 \\ py2exe_test_tk.py是以下代码

import Tkinter as tk

root = tk.Tk()
root.title("Test")


label1 = tk.Label(root,text="Hello!",font=('arial', 10, 'bold'), bg='lightblue')
label1.pack(ipadx=100, ipady=100)


root.mainloop()

This is the error I get when I try to run the newly created .exe 这是我尝试运行新创建的.exe时出现的错误

Traceback (most recent call last):
  File "py2exe_test_tk.py", line 4, in <module>
  File "Tkinter.pyc", line 1643, in __init__
_tkinter.TclError: Can't find a usable init.tcl in the following directories: 
    {C:/Users/My_Name/lib/tcl8.5} {C:/Users/My_Name/lib/tcl8.5} C:/Users/lib/tcl8.5 {C:/Users/My_Name/library} C:/Users/library C:/Users/tcl8.5.8/library C:/tcl8.5.8/library



This probably means that Tcl wasn't installed properly.

I'm pretty sure it's something in my conversion script thats giving me problems. 我很确定我的转换脚本中的内容会给我带来麻烦。 What did I omit? 我省略了什么? Or does someone have an example of what the conversion script would look like for a tkinter GUI program? 或者有人举例说明转换脚本对于tkinter GUI程序会是什么样子? Also is it possible to divert the output .exe files to my desktop? 是否可以将输出.exe文件转移到我的桌面?

EDIT: 编辑:

The error report said that I was missing init.tcl from {C:/Users/My_name/lib/tcl8.5} . 错误报告说我从{C:/Users/My_name/lib/tcl8.5}中缺少init.tcl So i made that directory and put a copy of init.tcl there. 所以我制作了那个目录并在那里放了一份init.tcl Now when I try to run the .exe it states that MSVCR90.dll is missing from my computer and is needed to run my program. 现在,当我尝试运行.exe时,它表示我的计算机缺少MSVCR90.dll ,需要运行我的程序。

Also this is python 2.6.5 on Windows 7. 这也是Windows 7上的python 2.6.5。

For your original problem I can't say what exactly the problem is, but usually it helps with trial-and-error to guess missing files and directories. 对于你原来的问题,我不能说究竟什么问题,但通常它有助于试错,猜测丢失的文件和目录。 If you know what you're missing, add them to your packages (for python modules) or data_files (for other files). 如果您知道缺少什么,请将它们添加到packages (对于python模块)或data_files (对于其他文件)。

The second problem is the result of some c-modules (and python itself) being build with MS Visual Studio, thus having a dependency to the MS Visual C++ 9.0 (2008) runtime. 第二个问题是使用MS Visual Studio构建一些c-modules(和python本身)的结果,因此依赖于MS Visual C ++ 9.0(2008)运行时。 You can solve this by either: 您可以通过以下方式解决此问题

  • owning a copy of Visual Studio (Express Edition doesn't count), so that you are permitted to redistribute the MSVCR dependencies (under the condition that you forbid your users reengeneering etc. of the dependend parts) 拥有Visual Studio的副本(Express Edition不计算在内),因此您可以重新分发MSVCR依赖项(在您禁止用户重新生成dependend部分的情况下)

  • pointing your users to the download of the MS Visual C++ 2008 Redistributable package at Microsoft. 指向您的用户在Microsoft下载MS Visual C ++ 2008 Redistributable软件包。

I found a bug on the virutalenv site which suggested the following https://github.com/pypa/virtualenv/issues/93 我在virutalenv网站上发现了一个错误,建议如下https://github.com/pypa/virtualenv/issues/93

for windows in your directory "C:\\Environments\\VirtualEnv\\Scripts\\activate.bat" just add which are set to the right path to TCL and TK for your python version 对于您的目录中的Windows“C:\\ Environments \\ VirtualEnv \\ Scripts \\ activate.bat”只需添加设置为TCL的正确路径和您的python版本的TK

set "TCL_LIBRARY=C:\Python27\tcl\tcl8.5"
set "TK_LIBRARY=C:\Python27\tcl\tk8.5"

and restart your cmd or shell 并重新启动您的cmd或shell

It worked very well for me when I had this error. 当我遇到这个错误时,它对我很有用。

py2exe doesn't work with modules, i've heard of one called c_freeze which apparently works with modules, try that? py2exe不能用于模块,我听说过一个名为c_freeze,它显然适用于模块,试试吗? http://cx-freeze.sourceforge.net/ http://cx-freeze.sourceforge.net/

With respect to MSVCR90.dll , see this post which packages it and maybe less preferable than having user install it separately. 关于MSVCR90.dll ,请参阅此帖子打包它,可能不如让用户单独安装它。

Also, the specific issue in that post was mine and i still don't understand root cause. 此外,该帖子中的具体问题是我的,我仍然不明白根本原因。 That said, a complete uninstall python and clean rebuild worked great... maybe that is your issue too. 也就是说,一个完整的卸载python和干净的重建工作很棒...也许这也是你的问题。 py2exe gives RuntimeError: Too early to create image py2exe给出RuntimeError:太早创建图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM