简体   繁体   中英

cx_freeze in python 3.6

I have written a code in python 3.6 and want to make a exe file for that code. i have used cx_freeze for that. Code is not wrong but i think problem is in setup.py file. Here is my code:

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()

My cx_freeze setup.py file is like this:

# 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)]
    )

When i convert my script into exe file, i got the following error. Can anyone tell me what is wrong with the setup.py file?

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'

when i add the following two lines in my setup.py file, it converts my script into exe.

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

but when i run the exe file, i got the following error shown in the picture.

在此处输入图片说明

You've got the tcl/tk library folders but you are just missing the runtimes or DLLs.

You also need to use the include_files argument to get the runtimes.

If the path you have given is correct then they should both be located at C:/python36/DLLs/ . You want both tcl86t.dll and tk86t.dll so just include them in your setup script. This is as easy as:

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 .

Hope I was helpful. Sorry about the late answer I only just found this question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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