我创建了GUI应用程序,用于将数据从GUI传输到.xlsx格式。以.py运行程序时一切正常,数据导出到文件'choosen_data.xlsx'。 但是在使用cx_freeze创建exe文件之后,应用程序正在运行但没有将数据导出到'choosen_file.xlsx'
我读了很多关于冰冻状态和这个功能的信息:
def find_data_file(filename):
if getattr(sys, 'frozen', False):
# The application is frozen
datadir = os.path.dirname(sys.executable)
else:
# The application is not frozen
# Change this bit to match where you store your data files:
datadir = os.path.dirname(__file__)
return os.path.join(datadir, filename)
但我不能让它发挥作用。有什么问题?
setup.py
from cx_Freeze import setup, Executable
import sys, os
# Dependencies are automatically detected, but it might need
# fine tuning.
def find_data_file(filename):
if getattr(sys, 'frozen', False):
# The application is frozen
datadir = os.path.dirname(sys.executable)
print(str(datadir))
else:
# The application is not frozen
# Change this bit to match where you store your data files:
datadir = os.path.dirname(__file__)
return os.path.join(datadir, filename)
data_sheet = find_data_file('data_sheet.csv')
choosen_data = find_data_file('choosen_data.xlsx')
buildOptions = dict(packages = [],
excludes = [],
include_files = [data_sheet, choosen_data])
import sys
base = 'Win32GUI' if sys.platform=='win32' else None
executables = [
Executable('main.py', base=base)
]
setup(name='test',
version = '1.0',
description = '',
options = dict(build_exe = buildOptions),
executables = executables)
我还尝试将文件作为'choosen_data.xlsx'放在include_files中 - 没有用。
我的main.py文件的开头
#-*- coding: utf-8 -*-
from PyQt5 import QtGui
from PyQt5 import QtWidgets
import sys
import design things
import csv, sqlite3
from openpyxl import load_workbook
import os
def find_data_file(filename):
if getattr(sys, 'frozen', False):
# The application is frozen
datadir = os.path.dirname(sys.executable)
else:
# The application is not frozen
# Change this bit to match where you store your data files:
datadir = os.path.dirname(__file__)
return os.path.join(datadir, filename)
choosen_data = find_data_file('choosen_data.xlsx')
class ExampleApp(QtWidgets.QMainWindow, design.Ui_MainWindow):
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self)
self.pushButton.clicked.connect(lambda: self.update_xlsx(choosen_data,
comboboxes_list,labels_list))
内置文件夹
也许这些信息很重要:我将cx_freeze-quickstart复制到我的应用程序的文件夹中,与我的主文件并排运行。像那样:
folder with main.py and cx_freeze
我还试图以两种方式修改openpyxl主目录中的 init .py文件(没有结果,数据仍未导出)
1.改变此行
here = os.path.abspath(os.path.dirname(__file__))
到此:
here = os.path.abspath(os.path.dirname(sys.executable))
2。并添加全新的功能,如:
import json
import os
import sys
def find_data_file(filename):
if getattr(sys, 'frozen', False):
# The application is frozen
datadir = os.path.dirname(sys.executable)
else:
# The application is not frozen
# Change this bit to match where you store your data files:
datadir = os.path.dirname(__file__)
return os.path.join(datadir, filename)
try:
## here = os.path.abspath(os.path.dirname(sys.executable))
here = find_data_file(__file__)
src_file = os.path.join(here, ".constants.json")
with open(src_file) as src:
constants = json.load(src)
__author__ = constants['__author__']
__author_email__ = constants["__author_email__"]
__license__ = constants["__license__"]
__maintainer_email__ = constants["__maintainer_email__"]
__url__ = constants["__url__"]
__version__ = constants["__version__"]
except IOError:
# packaged
pass