在Mac OS(实际上我使用macOS Mojave 10.14.4)上编译独立应用程序时,在外部ini文件中包含一些数据时遇到问题
试图使用 py2app 进行编译。
我的 main.py
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMessageBox
from decryptwindow import Ui_Dialog # importing our generated file
import sys
import os
class mywindow(QtWidgets.QMainWindow):
def __init__(self):
super(mywindow, self).__init__()
self.ui = Ui_Dialog()
self.ui.setupUi(self)
app = QtWidgets.QApplication([])
application = mywindow()
application.show()
sys.exit(app.exec())
我的界面文件( decryptwindow.py )
# Created by: PyQt5 UI code generator 5.11.3
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QInputDialog, QFileDialog, QListWidget, QMessageBox
from PyQt5.QtCore import pyqtSlot
import os
import configparser
from itertools import filterfalse
from pathlib import Path
import click
import PyPDF2
config = configparser.RawConfigParser()
config.read('config.ini')
path_val = config.get('DEFAULT', 'path')
browsetooltip = config.get('LANGUAGE', 'browsetooltip')
selectbutton = config.get('LANGUAGE', 'selectbutton')
if not os.path.exists(path_val):
path_val = /Users
class Ui_Dialog(QWidget):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(325, 269)
self.directory = path_val
self.BrowsepushButton = QtWidgets.QPushButton(Dialog)
self.BrowsepushButton.setEnabled(True)
self.BrowsepushButton.setGeometry(QtCore.QRect(240, 10, 75, 23))
self.BrowsepushButton.setObjectName("BrowsepushButton")
self.BrowsepushButton.setToolTip(browsetooltip)
self.BrowsepushButton.clicked.connect(self.BrowsepushButton_on_click)
self.fileslistWidget = QtWidgets.QListWidget(Dialog)
self.fileslistWidget.setGeometry(QtCore.QRect(20, 10, 201, 171))
self.fileslistWidget.setObjectName("fileslistWidget")
# fill the fileslistWidget with the files from the folder
# The folder is taken from the config.ini
for file_name in os.listdir(path_val):
self.fileslistWidget.addItem(file_name)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "PDF decrypt/Encrypt utility"))
self.BrowsepushButton.setText(_translate("Dialog", selectbutton))
def BrowsepushButton_on_click(self):
# print('BrowsepushButton button clicked')
self.directory = QtWidgets.QFileDialog.getExistingDirectory(self, "Choose the folder")
if self.directory:
for file_name in os.listdir(self.directory):
self.fileslistWidget.addItem(file_name)
# update existing value in ini file with the self.directory
config.set('DEFAULT', 'Path', self.directory)
# save last selected folder back to config.ini
with open('config.ini', 'w') as configfile:
config.write(configfile)
我的ini文件( config.ini )
[DEFAULT]
path = /FM32YO/files
[LANGUAGE]
browsetooltip = Click to Select the folder
selectbutton = Select folder
我希望将路径写入 config.ini 文件。 实际上,当我通过 python3
运行我的应用程序时但是我需要独立的应用程序。
所以我的步骤是
1) py2applet --make-setup main.py
""
This is a setup.py script generated by py2applet
Usage:
python setup.py py2app
"""
from setuptools import setup
APP = ['main.py']
DATA_FILES = ['']
OPTIONS = {}
setup(
app=APP,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)
2) python3 setup.py py2app
就在这里。我有一个准备启动的应用程序文件。 但是,一旦我运行它,就会出现未知的终端错误。
仅在将 config.ini 包含在 setup.py 中之后,我才能够避免该错误,但这实际上不是很好
所以我的 setup.py 现在看起来像:
""
This is a setup.py script generated by py2applet
Usage:
python setup.py py2app
"""
from setuptools import setup
APP = ['main.py']
DATA_FILES = ['config.ini']
OPTIONS = {}
setup(
app=APP,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)
之后,该应用程序运行正常。 但是...。由于config.ini已包含在应用程序中,因此不再可写。
有人有解决问题的想法吗?
谢谢。
p.s。顺便说一句,在Windows(将应用程序编译为exe文件)中,一切正常。