问题
I am trying to create an executable using cx_Freeze from a python script and it appears that certain portions of the tkinter module are not getting loaded into the executable file. The script file contains the import statements:
from tkinter import *
from tkinter.filedialog import askopenfile, asksaveasfile
from tkinter.messagebox import *
import subprocess
import time
Within my script I have some code that looks like this:
f_outfile = filedialog.asksaveasfile(initialdir=r'c:\Program Files (x86)\LTC\LTSpiceIV')
When I run this script from inside the IDLE shell, it works flawlessly. I create an executable file with cx_Freeze using a setup.py file that looks like this:
from cx_Freeze import *
setup(name="voltage_substate_GUI",
version="0.1",
description="test",
executables=[Executable("voltage_substate_GUI.pyw")],
)
The executable is created without any problems, however when I run the executable I get an error in the console window that says
NameError: name 'filedialog is not defined'
However the rest of the script, which creates a GUI, appears to work just fine ---- all the buttons work as well as all the Entry() items, etc.
Any thoughts on the problem?
Thank you!
回答1:
For some reason, I had to import the 'filedialog' and 'messagebox' submodules explicitly in the script I was freezing.
import tkinter.filedialog as fdialog
import tkinter.messagebox as msgbox
import tkinter
While 'Label', 'Button', 'Frame', 'Entry', etc. worked fine, who knows how many others must be imported in that fashion. Hope this helps someone.
来源:https://stackoverflow.com/questions/23790545/keep-getting-a-filedialog-error-after-creating-executable-with-cx-freeze