使用cx_freeze将脚本转换为.exe时如何包括tkinter? [英] How to include tkinter when using cx_freeze to convert script to .exe?

查看:68
本文介绍了使用cx_freeze将脚本转换为.exe时如何包括tkinter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用cx_freeze将python文件传输到exe。问题是当我在setup.py中排除tkinter时,我可以成功生成exe文件,但是当执行exe文件时,它说没有名为tkinter的模块

I am using cx_freeze to transfer a python file to a exe. the problem is when i exclude tkinter in the setup.py, i can generate the exe file successfully, but when execute the exe file, it says No Module named tkinter.

build_exe_options = {"packages": ["os","numpy","time","optparse","linecache","pandas",
                     "matplotlib","PIL"], "excludes": ["tkinter"]}

,但是当我尝试包含 tkinter 时,它只是无法生成exe文件。

but when I try to include tkinter, it just can't generate the exe file.

build_exe_options = {"packages": ["os","numpy","time","optparse","linecache","pandas",
                     "matplotlib","PIL","tkinter"]}



File "C:\Users\changchun_xu\AppData\Local\Programs\Python\Python36-32\lib\os.py", line 669, in __getitem__
    raise KeyError(key) from None
KeyError: 'TCL_LIBRARY'


推荐答案

您必须进行两个修改到您的 setup.py 进行操作:

You have to make two modifications to your setup.py to get things working:


  1. Set TCL-LIBRARY TK_LIBRARY 环境变量。 (您已经做到了)

  1. Set TCL-LIBRARY and TK_LIBRARY environment variables. (You already did this)

添加 tcl86t.dll tk86t .dll 到您的 include_files 参数

.py应该看起来像这样:

So the setup.py should look something like this:

import os
from cx_Freeze import setup, Executable

os.environ['TCL_LIBRARY'] = 'c:/python36/tcl/tcl8.6'
os.environ['TK_LIBRARY'] = 'c:/python36/tcl/tk8.6'

# Dependencies are automatically detected, but it might need
# fine tuning.
buildOptions = dict(
    packages = [],
    excludes = [],
    include_files=['c:/python36/DLLs/tcl86t.dll', 'c:/python36/DLLs/tk86t.dll']
)

import sys
base = 'Win32GUI' if sys.platform=='win32' else None

executables = [
    Executable('editor.py', base=base)
]

setup(name='editor',
      version = '1.0',
      description = '',
      options = dict(build_exe = buildOptions),
      executables = executables)

这篇关于使用cx_freeze将脚本转换为.exe时如何包括tkinter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆