Keep getting a 'filedialog' error after creating executable with cx_Freeze

与世无争的帅哥 提交于 2019-12-11 03:35:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!