python 3.6中的cx_freeze

问题描述 投票:2回答:1

我在python 3.6中编写了一个代码,并希望为该代码创建一个exe文件。我已经使用了cx_freeze。代码没有错,但我认为问题出在setup.py文件中。这是我的代码:

from tkinter import *

root = Tk()
root.title("test window")
root.geometry('400x220+500+250')
root.configure(background="dark gray")

def submit(*args):
    root.iconify()
    popup_root_window = Toplevel()
    popup_root_window.geometry('300x50+550+300')
    popup_root_window.resizable(width=False, height=False)
    popup_root_window.focus_force()
    popup_root_window.grab_set()
    popup_root_window_label = Label(popup_root_window, text="new window open successfully.")
    popup_root_window_label.pack(anchor=CENTER, padx=10, pady=20)

frame = Frame(root, bd=4, relief="raise", height=100, width=250)
frame.pack(fill="both", padx=70, pady=35)
frame.pack_propagate(0)

submit_button = Button(frame, text="Submit", command=submit, width=10)
submit_button.grid(row=0, column=0)

cancel_button = Button(frame, text="Cancel", width=10)
cancel_button.grid(row=0, column=1) 

root.mainloop()

我的cx_freeze setup.py文件是这样的:

# setup file for cx_freeze

import sys
from cx_Freeze import setup, Executable

base = None
if sys.platform == "win32":
    base = "Win32GUI"

includes = ["atexit", "re"]

build_exe_options = {"packages": ["os", "tkinter"], "includes": includes}

setup(
    name = "testing",
    version = "1.0",
    description = "Testing of tkinter",
    options = {"build_exe": build_exe_options},
    executables = [Executable("testing.py", base = base)]
    )

当我将我的脚本转换为exe文件时,我收到以下错误。谁能告诉我setup.py文件有什么问题?

running build
running build_exe
Traceback (most recent call last):
  File "setup.py", line 20, in <module>
    executables = [Executable("tk1.py", base = base)]
  File "C:\Python36\lib\site-packages\cx_Freeze\dist.py", line 349, in setup
    distutils.core.setup(**attrs)
  File "C:\Python36\lib\distutils\core.py", line 148, in setup
    dist.run_commands()
  File "C:\Python36\lib\distutils\dist.py", line 955, in run_commands
    self.run_command(cmd)
  File "C:\Python36\lib\distutils\dist.py", line 974, in run_command
    cmd_obj.run()
  File "C:\Python36\lib\distutils\command\build.py", line 135, in run
    self.run_command(cmd_name)
  File "C:\Python36\lib\distutils\cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "C:\Python36\lib\distutils\dist.py", line 974, in run_command
    cmd_obj.run()
  File "C:\Python36\lib\site-packages\cx_Freeze\dist.py", line 219, in run
    freezer.Freeze()
  File "C:\Python36\lib\site-packages\cx_Freeze\freezer.py", line 621, in Freeze
    self.finder = self._GetModuleFinder()
  File "C:\Python36\lib\site-packages\cx_Freeze\freezer.py", line 340, in _GetModuleFinder
    finder.IncludePackage(name)
  File "C:\Python36\lib\site-packages\cx_Freeze\finder.py", line 653, in IncludePackage
    module = self._ImportModule(name, deferredImports)
  File "C:\Python36\lib\site-packages\cx_Freeze\finder.py", line 310, in _ImportModule
    deferredImports, namespace = namespace)
  File "C:\Python36\lib\site-packages\cx_Freeze\finder.py", line 403, in _InternalImportModule
    parentModule, namespace)
  File "C:\Python36\lib\site-packages\cx_Freeze\finder.py", line 416, in _LoadModule
    namespace)
  File "C:\Python36\lib\site-packages\cx_Freeze\finder.py", line 485, in _LoadPackage
    self._LoadModule(name, fp, path, info, deferredImports, parent)
  File "C:\Python36\lib\site-packages\cx_Freeze\finder.py", line 463, in _LoadModule
    self._RunHook("load", module.name, module)
  File "C:\Python36\lib\site-packages\cx_Freeze\finder.py", line 536, in _RunHook
    method(self, *args)
  File "C:\Python36\lib\site-packages\cx_Freeze\hooks.py", line 613, in load_tkinter
    tclSourceDir = os.environ["TCL_LIBRARY"]
  File "C:\Python36\lib\os.py", line 669, in __getitem__
    raise KeyError(key) from None
KeyError: 'TCL_LIBRARY'

当我在setup.py文件中添加以下两行时,它将我的脚本转换为exe。

os.environ['TCL_LIBRARY'] = r'C:\Python36\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Python36\tcl\tk8.6'

但是当我运行exe文件时,我得到了图片中显示的以下错误。

enter image description here

python python-2.7 python-3.x cx-freeze
1个回答
0
投票

你有tcl / tk库文件夹,但你只是缺少运行时或DLL。

您还需要使用include_files参数来获取运行时。

如果你给出的路径是正确的,那么它们都应该位于C:/python36/DLLs/。你想要tcl86t.dlltk86t.dll,所以只需将它们包含在你的设置脚本中即可。这很简单:

build_exe_options = {"packages": ["os", "tkinter"], "includes": includes, "include_files": ["C:/python36/DLLs/tcl86t.dll", "C:/python36/DLLs/tk86t.dll"]}

This answer also relates to your problem

希望我有所帮助。对于迟到的回答,我只是发现了这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.