Как сделать сохранение внесенных данных в файл.ini, QSettings?
Есть приложение, в котором изредка понадобиться изменять некоторые параметры,
которые перезаписываются в переменной (propCity
, normCity
и т.д.).
Но после закрытия приложения естественно все ново внесенные параметры сбрасываются.
Подскажите как сделать, чтобы новые параметры сохранялись в .ini
файл и в будущем, при внесении других параметров, они перезаписывались в этом же файле?
Пытался поковыряться с QSettings
, но так его до конца и не понял.
app.py
import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QMessageBox
from PySide6.QtGui import QPixmap, QIcon
from PySide6.QtCore import QSettings
from design import Ui_root
sys.argv += ['-platform', 'windows:darkmode=1']
class MainWindow(QMainWindow, Ui_root):
def __init__(self):
super().__init__()
self.setupUi(self)
# setting
self.btnSet.clicked.connect(self.saveProperty)
self.btnSet.setShortcut(QApplication.translate("root", u"Ctrl+S", None))
# property
self.propCity = None
self.propHighway = None
self.normCity = None
self.normHighway = None
self.propCityWinter = None
self.normCityWinter = None
self.propHighwayWinter = None
self.normHighwayWinter = None
# function
self.btnSummer.clicked.connect(self.funcSummer)
self.btnWinter.clicked.connect(self.funcWinter)
# menubar
self.btnAbout.clicked.connect(self.funcAbout)
self.btnHelp.clicked.connect(self.funcHelp)
self.btnGear.setCheckable(True)
self.btnGear.clicked.connect(self.funcGear)
# summer
self.applyPropCitySummer.clicked.connect(self.newPropCitySummer)
self.applyNormCitySummer.clicked.connect(self.newNormCitySummer)
self.applyPropHighwaySummer.clicked.connect(self.newPropHighwaySummer)
self.applyNormHighwaySummer.clicked.connect(self.newNormHighwaySummer)
#winter
self.applyPropCityWinter.clicked.connect(self.newPropCityWinter)
self.applyNormCityWinter.clicked.connect(self.newNormCityWinter)
self.applyPropHighwayWinter.clicked.connect(self.newPropHighwayWinter)
self.applyNormHighwayWinter.clicked.connect(self.newNormHighwayWinter)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def saveProperty(self):
settings = QSettings("setting.ini", QSettings.IniFormat)
settings.setValue("setting.ini", self.propCity)
settings.setValue("setting.ini", self.propHighway)
settings.setValue("setting.ini", self.normCity)
settings.setValue("setting.ini", self.normHighway)
settings.setValue("setting.ini", self.propCityWinter)
settings.setValue("setting.ini", self.normCityWinter)
settings.setValue("setting.ini", self.propHighwayWinter)
settings.setValue("setting.ini", self.normHighwayWinter)
msg = QMessageBox.information(self, 'Настройки', f"Новые данные сохранены.")
settings.sync()
# - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# summer
def funcSummer(self):
try:
# Пропорции город лето
if self.propCity:
propCity = self.propCity
else:
propCity = 0.3
# Нормы город лето
if self.normCity:
normCity = self.normCity
else:
normCity = 11.5
# Пропорции трасса лето
if self.propHighway:
propHighway = self.propHighway
else:
propHighway = 0.7
# Нормы трасса лето
if self.normHighway:
normHighway = self.normHighway
else:
normHighway = 8.5
inSummer = float(self.inputSummer.text())
roadCity = round(propCity * inSummer, 2)
roadHighway = round(propHighway * inSummer, 2)
resultCity = round(propCity * inSummer / 100 * normCity, 2)
resultHighway = round(propHighway * inSummer / 100 * normHighway, 2)
totalSummer = round((propCity * inSummer / 100 * normCity) + (propHighway * inSummer / 100 * normHighway),
2)
self.outSummer.setText(
f' Ваш расход составил {totalSummer} литров\n\n'
f' Детальная информация \n'
f' Пробег по городу {roadCity} км \n'
f' Расход по городу {resultCity} литров\n'
f' Пробег по трассе {roadHighway} км \n'
f' Расход по трассе {resultHighway} литров\n\n'
f' Нормы расхода\n'
f' Город {normCity} на 100 км\n'
f' Трасса {normHighway} на 100 км\n\n'
f' Пропорции\n'
f' Коэф. по городу {propCity}\n'
f' Коэф. по трассе {propHighway}'
)
except Exception as e:
msg = QMessageBox.information(
self, 'Внимание', f"Что-то пошло не так. \n"
f"Проверьте исходные данные.")
def newPropCitySummer(self):
try:
self.propCity = float(self.inputPropCitySummer.text())
self.funcSummer()
except Exception as e:
self.propCity = None
msg = QMessageBox.information(
self,
'Внимание',
f"Что-то пошло не так. <br>"
f"Проверьте исходные данные. <br><b>{self.inputPropCitySummer.text()}</b>")
def newNormCitySummer(self):
try:
self.normCity = float(self.inputNormCitySummer.text())
self.funcSummer()
except Exception as e:
self.normCity = None
msg = QMessageBox.information(
self,
'Внимание',
f"Что-то пошло не так. <br>"
f"Проверьте исходные данные. <br><b>{self.inputNormCitySummer.text()}</b>")
def newPropHighwaySummer(self):
try:
self.propHighway = float(self.inputPropHighwaySummer.text())
self.funcSummer()
except Exception as e:
self.propHighway = None
msg = QMessageBox.information(
self,
'Внимание',
f"Что-то пошло не так. <br>"
f"Проверьте исходные данные. <br><b>{self.inputPropHighwaySummer.text()}</b>")
def newNormHighwaySummer(self):
try:
self.normHighway = float(self.inputNormHighwaySummer.text())
self.funcSummer()
except Exception as e:
self.normHighway = None
msg = QMessageBox.information(
self,
'Внимание',
f"Что-то пошло не так. <br>"
f"Проверьте исходные данные. <br><b>{self.inputNormHighwaySummer.text()}</b>")
# winter
# winter
def funcWinter(self):
try:
if self.propCityWinter: # Пропорции город зима
propCityWinter = self.propCityWinter
else:
propCityWinter = 0.3
if self.normCityWinter: # Нормы город зима
normCityWinter = self.normCityWinter
else:
normCityWinter = 13.8
if self.propHighwayWinter: # Пропорции трасса зима
propHighwayWinter = self.propHighwayWinter
else:
propHighwayWinter = 0.7
if self.normHighwayWinter: # Нормы трасса зима
normHighwayWinter = self.normHighwayWinter
else:
normHighwayWinter = 10.2
inWinter = float(self.inputWinter.text())
roadCity = round(propCityWinter * inWinter, 2)
roadHighway = round(propHighwayWinter * inWinter, 2)
resultCity = round(propCityWinter * inWinter / 100 * normCityWinter, 2)
resultHighway = round(propHighwayWinter * inWinter / 100 * normHighwayWinter, 2)
totalWinter = round((propCityWinter * inWinter / 100 * normCityWinter) + (
propHighwayWinter * inWinter / 100 * normHighwayWinter), 2)
self.outWinter.setText(
f' Ваш расход составил {totalWinter} литров\n\n'
f' Детальная информация \n'
f' Пробег по городу {roadCity} км \n'
f' Расход по городу {resultCity} литров\n'
f' Пробег по трассе {roadHighway} км \n'
f' Расход по трассе {resultHighway} литров\n\n'
f' Нормы расхода\n'
f' Город {normCityWinter} на 100 км\n'
f' Трасса {normHighwayWinter} на 100 км\n\n'
f' Пропорции\n'
f' Коэф. по городу {propCityWinter}\n'
f' Коэф. по трассе {propHighwayWinter}'
)
except Exception as e:
msg = QMessageBox.information(
self, 'Внимание', f"Что-то пошло не так. \n"
f"Проверьте исходные данные.")
def newPropCityWinter(self):
try:
self.propCityWinter = float(self.inputPropCityWinter.text())
self.funcWinter()
except Exception as e:
self.propCityWinter = None
msg = QMessageBox.information(
self,
'Внимание',
f"Что-то пошло не так.\n"
f"Проверьте исходные данные.")
def newNormCityWinter(self):
try:
self.normCityWinter = float(self.inputNormCityWinter.text())
self.funcWinter()
except Exception as e:
self.normCityWinter = None
msg = QMessageBox.information(
self,
'Внимание',
f"Что-то пошло не так.\n"
f"Проверьте исходные данные.")
def newPropHighwayWinter(self):
try:
self.propHighwayWinter = float(self.inputPropHighwayWinter.text())
self.funcWinter()
except Exception as e:
self.propHighwayWinter = None
msg = QMessageBox.information(
self,
'Внимание',
f"Что-то пошло не так.\n"
f"Проверьте исходные данные.")
def newNormHighwayWinter(self):
try:
self.normHighwayWinter = float(self.inputNormHighwayWinter.text())
self.funcWinter()
except Exception as e:
self.normHighwayWinter = None
msg = QMessageBox.information(
self,
'Внимание',
f"Что-то пошло не так.\n"
f"Проверьте исходные данные.")
# bar
def funcGear(self):
if self.btnGear.isChecked():
self.resize(560, 670)
else:
self.resize(560, 450)
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setStyle('Fusion')
w = MainWindow()
w.show()
sys.exit(app.exec())
design.py
from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
QMetaObject, QObject, QPoint, QRect,
QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
QFont, QFontDatabase, QGradient, QIcon,
QImage, QKeySequence, QLinearGradient, QPainter,
QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QLabel, QLineEdit, QMainWindow,
QPushButton, QSizePolicy, QTabWidget, QWidget)
import files_rc
class Ui_root(object):
def setupUi(self, root):
if not root.objectName():
root.setObjectName(u"root")
root.resize(560, 450)
root.setMinimumSize(QSize(560, 450))
root.setMaximumSize(QSize(560, 670))
root.setCursor(QCursor(Qt.ArrowCursor))
root.setMouseTracking(False)
icon = QIcon()
icon.addFile(u":/icon/icon.ico", QSize(), QIcon.Normal, QIcon.Off)
root.setWindowIcon(icon)
root.setWindowOpacity(1.000000000000000)
root.setAutoFillBackground(False)
root.setStyleSheet(u"")
root.setDocumentMode(False)
root.setTabShape(QTabWidget.Rounded)
self.grid_w = QWidget(root)
self.grid_w.setObjectName(u"grid_w")
self.grid_w.setStyleSheet(u"")
self.inputSummer = QLineEdit(self.grid_w)
self.inputSummer.setObjectName(u"inputSummer")
self.inputSummer.setGeometry(QRect(20, 40, 241, 31))
self.inputSummer.setLayoutDirection(Qt.LeftToRight)
self.inputSummer.setStyleSheet(u"")
self.inputSummer.setInputMethodHints(Qt.ImhNone)
self.inputSummer.setMaxLength(32767)
self.inputSummer.setAlignment(Qt.AlignLeading | Qt.AlignLeft | Qt.AlignVCenter)
self.inputSummer.setDragEnabled(False)
self.outSummer = QLabel(self.grid_w)
self.outSummer.setObjectName(u"outSummer")
self.outSummer.setGeometry(QRect(20, 130, 241, 281))
self.outSummer.setStyleSheet(u"background-color: rgba(230, 230, 230, 230);")
self.outSummer.setAlignment(Qt.AlignLeading | Qt.AlignLeft | Qt.AlignTop)
self.btnSummer = QPushButton(self.grid_w)
self.btnSummer.setObjectName(u"btnSummer")
self.btnSummer.setGeometry(QRect(20, 80, 241, 41))
self.btnSummer.setCursor(QCursor(Qt.PointingHandCursor))
self.btnSummer.setStyleSheet(u"")
self.btnSummer.setInputMethodHints(Qt.ImhNone)
self.btnWinter = QPushButton(self.grid_w)
self.btnWinter.setObjectName(u"btnWinter")
self.btnWinter.setGeometry(QRect(300, 80, 241, 41))
self.btnWinter.setCursor(QCursor(Qt.PointingHandCursor))
self.btnWinter.setStyleSheet(u"")
self.outWinter = QLabel(self.grid_w)
self.outWinter.setObjectName(u"outWinter")
self.outWinter.setGeometry(QRect(300, 130, 241, 281))
self.outWinter.setStyleSheet(u"background-color: rgb(230, 230, 230);")
self.outWinter.setInputMethodHints(Qt.ImhNone)
self.outWinter.setAlignment(Qt.AlignLeading | Qt.AlignLeft | Qt.AlignTop)
self.inputWinter = QLineEdit(self.grid_w)
self.inputWinter.setObjectName(u"inputWinter")
self.inputWinter.setGeometry(QRect(300, 40, 241, 31))
self.inputWinter.setStyleSheet(u"")
self.inputWinter.setMaxLength(10)
self.label_s = QLabel(self.grid_w)
self.label_s.setObjectName(u"label_s")
self.label_s.setGeometry(QRect(20, 10, 241, 21))
self.label_s.setStyleSheet(u"")
self.lable_w = QLabel(self.grid_w)
self.lable_w.setObjectName(u"lable_w")
self.lable_w.setGeometry(QRect(300, 10, 241, 21))
self.lable_w.setStyleSheet(u"")
# about
self.btnAbout = QPushButton(self.grid_w)
self.btnAbout.setObjectName(u"btnAbout")
self.btnAbout.setGeometry(QRect(20, 417, 29, 28))
self.btnAbout.setCursor(QCursor(Qt.PointingHandCursor))
self.btnAbout.setStyleSheet(u"")
icon1 = QIcon()
icon1.addFile(u"res/button/btn_about.png", QSize(), QIcon.Normal, QIcon.Off)
self.btnAbout.setIcon(icon1)
# help
self.btnHelp = QPushButton(self.grid_w)
self.btnHelp.setObjectName(u"btnHelp")
self.btnHelp.setGeometry(QRect(53, 417, 29, 28))
self.btnHelp.setCursor(QCursor(Qt.PointingHandCursor))
self.btnHelp.setStyleSheet(u"")
icon2 = QIcon()
icon2.addFile(u"res/button/btn_help.png", QSize(), QIcon.Normal, QIcon.Off)
self.btnHelp.setIcon(icon2)
self.btnHelp.setAutoDefault(False)
self.btnHelp.setFlat(False)
# setting
self.btnSet = QPushButton(self.grid_w)
self.btnSet.setObjectName(u"btnSet")
self.btnSet.setGeometry(QRect(476, 417, 30, 28))
self.btnSet.setCursor(QCursor(Qt.PointingHandCursor))
self.btnSet.setStyleSheet(u"")
icon3 = QIcon()
icon3.addFile(u"res/button/btn_setting.png", QSize(), QIcon.Normal, QIcon.Off)
self.btnSet.setIcon(icon3)
# property
self.btnGear = QPushButton(self.grid_w)
self.btnGear.setObjectName(u"btnGear")
self.btnGear.setGeometry(QRect(510, 417, 30, 28))
self.btnGear.setCursor(QCursor(Qt.PointingHandCursor))
self.btnGear.setStyleSheet(u"")
icon3 = QIcon()
icon3.addFile(u"res/button/btn_config.png", QSize(), QIcon.Normal, QIcon.Off)
self.btnGear.setIcon(icon3)
self.labelPropSummer = QLabel(self.grid_w)
self.labelPropSummer.setObjectName(u"labelPropSummer")
self.labelPropSummer.setGeometry(QRect(20, 470, 71, 16))
self.applyPropCitySummer = QPushButton(self.grid_w)
self.applyPropCitySummer.setObjectName(u"applyPropCitySummer")
self.applyPropCitySummer.setGeometry(QRect(180, 490, 81, 31))
self.applyPropCitySummer.setCursor(QCursor(Qt.PointingHandCursor))
self.labelNormSummer = QLabel(self.grid_w)
self.labelNormSummer.setObjectName(u"labelNormSummer")
self.labelNormSummer.setGeometry(QRect(20, 570, 91, 16))
self.applyNormHighwaySummer = QPushButton(self.grid_w)
self.applyNormHighwaySummer.setObjectName(u"applyNormHighwaySummer")
self.applyNormHighwaySummer.setGeometry(QRect(180, 630, 81, 31))
self.applyNormHighwaySummer.setCursor(QCursor(Qt.PointingHandCursor))
self.applyPropHighwaySummer = QPushButton(self.grid_w)
self.applyPropHighwaySummer.setObjectName(u"applyPropHighwaySummer")
self.applyPropHighwaySummer.setGeometry(QRect(180, 530, 81, 31))
self.applyPropHighwaySummer.setCursor(QCursor(Qt.PointingHandCursor))
self.applyNormCitySummer = QPushButton(self.grid_w)
self.applyNormCitySummer.setObjectName(u"applyNormCitySummer")
self.applyNormCitySummer.setGeometry(QRect(180, 590, 81, 31))
self.applyNormCitySummer.setCursor(QCursor(Qt.PointingHandCursor))
self.applyNormCityWinter = QPushButton(self.grid_w)
self.applyNormCityWinter.setObjectName(u"applyNormCityWinter")
self.applyNormCityWinter.setGeometry(QRect(460, 590, 81, 31))
self.applyNormCityWinter.setCursor(QCursor(Qt.PointingHandCursor))
self.applyNormHighwayWinter = QPushButton(self.grid_w)
self.applyNormHighwayWinter.setObjectName(u"applyNormHighwayWinter")
self.applyNormHighwayWinter.setGeometry(QRect(460, 630, 81, 31))
self.applyNormHighwayWinter.setCursor(QCursor(Qt.PointingHandCursor))
self.applyPropCityWinter = QPushButton(self.grid_w)
self.applyPropCityWinter.setObjectName(u"applyPropCityWinter")
self.applyPropCityWinter.setGeometry(QRect(460, 490, 81, 31))
self.applyPropCityWinter.setCursor(QCursor(Qt.PointingHandCursor))
self.applyPropHighwayWinter = QPushButton(self.grid_w)
self.applyPropHighwayWinter.setObjectName(u"applyPropHighwayWinter")
self.applyPropHighwayWinter.setGeometry(QRect(460, 530, 81, 31))
self.applyPropHighwayWinter.setCursor(QCursor(Qt.PointingHandCursor))
self.labelPropWinter = QLabel(self.grid_w)
self.labelPropWinter.setObjectName(u"labelPropWinter")
self.labelPropWinter.setGeometry(QRect(300, 470, 71, 16))
self.labelNormWinter = QLabel(self.grid_w)
self.labelNormWinter.setObjectName(u"labelNormWinter")
self.labelNormWinter.setGeometry(QRect(300, 570, 91, 16))
self.inputPropCitySummer = QLineEdit(self.grid_w)
self.inputPropCitySummer.setObjectName(u"inputPropCitySummer")
self.inputPropCitySummer.setGeometry(QRect(20, 491, 151, 31))
self.inputPropHighwaySummer = QLineEdit(self.grid_w)
self.inputPropHighwaySummer.setObjectName(u"inputPropHighwaySummer")
self.inputPropHighwaySummer.setGeometry(QRect(20, 530, 151, 31))
self.inputPropCityWinter = QLineEdit(self.grid_w)
self.inputPropCityWinter.setObjectName(u"inputPropCityWinter")
self.inputPropCityWinter.setGeometry(QRect(300, 490, 151, 31))
self.inputPropHighwayWinter = QLineEdit(self.grid_w)
self.inputPropHighwayWinter.setObjectName(u"inputPropHighwayWInter")
self.inputPropHighwayWinter.setGeometry(QRect(300, 530, 151, 31))
self.inputNormCitySummer = QLineEdit(self.grid_w)
self.inputNormCitySummer.setObjectName(u"inputNormCitySummer")
self.inputNormCitySummer.setGeometry(QRect(20, 590, 151, 31))
self.inputNormHighwaySummer = QLineEdit(self.grid_w)
self.inputNormHighwaySummer.setObjectName(u"inputNormHighwaySummer")
self.inputNormHighwaySummer.setGeometry(QRect(20, 630, 151, 31))
self.inputNormCityWinter = QLineEdit(self.grid_w)
self.inputNormCityWinter.setObjectName(u"inputNormCityWinter")
self.inputNormCityWinter.setGeometry(QRect(300, 590, 151, 31))
self.inputNormHighwayWinter = QLineEdit(self.grid_w)
self.inputNormHighwayWinter.setObjectName(u"inputNormHighwayWInter")
self.inputNormHighwayWinter.setGeometry(QRect(300, 630, 151, 31))
root.setCentralWidget(self.grid_w)
self.retranslateUi(root)
self.btnHelp.setDefault(False)
self.btnGear.setDefault(False)
QMetaObject.connectSlotsByName(root)
# setupUi
def retranslateUi(self, root):
root.setWindowTitle(QCoreApplication.translate("root", u"BenzConfig", None))
self.inputSummer.setInputMask("")
self.inputSummer.setText("")
self.inputSummer.setPlaceholderText(QCoreApplication.translate("root",
u"\u0412\u0432\u0435\u0434\u0438\u0442\u0435 \u0434\u0430\u043d\u043d\u044b\u0435",
None))
self.outSummer.setText("")
self.btnSummer.setText(
QCoreApplication.translate("root", u"\u0420\u0430\u0441\u0441\u0447\u0438\u0442\u0430\u0442\u044c", None))
#if QT_CONFIG(shortcut)
self.btnSummer.setShortcut(QCoreApplication.translate("root", u"Return", None))
#endif // QT_CONFIG(shortcut)
self.btnWinter.setText(
QCoreApplication.translate("root", u"\u0420\u0430\u0441\u0441\u0447\u0438\u0442\u0430\u0442\u044c", None))
#if QT_CONFIG(shortcut)
self.btnWinter.setShortcut(QCoreApplication.translate("root", u"Tab", None))
#endif // QT_CONFIG(shortcut)
self.outWinter.setText("")
self.inputWinter.setText("")
self.inputWinter.setPlaceholderText(QCoreApplication.translate("root",
u"\u0412\u0432\u0435\u0434\u0438\u0442\u0435 \u0434\u0430\u043d\u043d\u044b\u0435",
None))
self.label_s.setText(QCoreApplication.translate("root", u"\u041b\u0435\u0442\u043d\u0438\u0439", None))
self.lable_w.setText(QCoreApplication.translate("root", u"\u0417\u0438\u043c\u043d\u0438\u0439", None))
self.btnAbout.setText("")
self.btnHelp.setText("")
self.btnGear.setText("")
self.labelPropSummer.setText(
QCoreApplication.translate("root", u"\u041f\u0440\u043e\u043f\u043e\u0440\u0446\u0438\u0438", None))
self.applyPropCitySummer.setText(
QCoreApplication.translate("root", u"\u041f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c", None))
self.labelNormSummer.setText(QCoreApplication.translate("root",
u"\u041d\u043e\u0440\u043c\u044b \u0440\u0430\u0441\u0445\u043e\u0434\u0430",
None))
self.applyNormHighwaySummer.setText(
QCoreApplication.translate("root", u"\u041f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c", None))
self.applyPropHighwaySummer.setText(
QCoreApplication.translate("root", u"\u041f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c", None))
self.applyNormCitySummer.setText(
QCoreApplication.translate("root", u"\u041f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c", None))
self.applyNormCityWinter.setText(
QCoreApplication.translate("root", u"\u041f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c", None))
self.applyNormHighwayWinter.setText(
QCoreApplication.translate("root", u"\u041f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c", None))
self.applyPropCityWinter.setText(
QCoreApplication.translate("root", u"\u041f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c", None))
self.applyPropHighwayWinter.setText(
QCoreApplication.translate("root", u"\u041f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c", None))
self.labelPropWinter.setText(
QCoreApplication.translate("root", u"\u041f\u0440\u043e\u043f\u043e\u0440\u0446\u0438\u0438", None))
self.labelNormWinter.setText(QCoreApplication.translate("root",
u"\u041d\u043e\u0440\u043c\u044b \u0440\u0430\u0441\u0445\u043e\u0434\u0430",
None))
self.inputPropCitySummer.setPlaceholderText(
QCoreApplication.translate("root", u"\u0413\u043e\u0440\u043e\u0434", None))
self.inputPropHighwaySummer.setPlaceholderText(
QCoreApplication.translate("root", u"\u0422\u0440\u0430\u0441\u0441\u0430", None))
self.inputPropCityWinter.setPlaceholderText(
QCoreApplication.translate("root", u"\u0413\u043e\u0440\u043e\u0434", None))
self.inputPropHighwayWinter.setPlaceholderText(
QCoreApplication.translate("root", u"\u0422\u0440\u0430\u0441\u0441\u0430", None))
self.inputNormCitySummer.setPlaceholderText(
QCoreApplication.translate("root", u"\u0413\u043e\u0440\u043e\u0434", None))
self.inputNormHighwaySummer.setPlaceholderText(
QCoreApplication.translate("root", u"\u0422\u0440\u0430\u0441\u0441\u0430", None))
self.inputNormCityWinter.setPlaceholderText(
QCoreApplication.translate("root", u"\u0413\u043e\u0440\u043e\u0434", None))
self.inputNormHighwayWinter.setPlaceholderText(
QCoreApplication.translate("root", u"\u0422\u0440\u0430\u0441\u0441\u0430", None))
# retranslateUi
Ответы (1 шт):
Автор решения: S. Nick
→ Ссылка
Как вариант. Тестировал на summer.
winter - вам для домашней работы.
main.py
import sys
'''
from PySide6.QtWidgets import QApplication, QMainWindow, QMessageBox
from PySide6.QtGui import QPixmap, QIcon
from PySide6.QtCore import QSettings
from design import Ui_root
sys.argv += ['-platform', 'windows:darkmode=1']
'''
from PyQt5.Qt import *
from design import Ui_root
class MainWindow(QMainWindow, Ui_root):
def __init__(self):
super().__init__()
self.setupUi(self)
# setting save
self.btnSet.clicked.connect(self.saveProperty)
self.btnSet.setShortcut(QApplication.translate("root", u"Ctrl+S", None))
# property
self.propCity = None
self.propHighway = None
self.normCity = None
self.normHighway = None
self.propCityWinter = None
self.normCityWinter = None
self.propHighwayWinter = None
self.normHighwayWinter = None
# function
self.btnSummer.clicked.connect(self.funcSummer)
self.btnWinter.clicked.connect(self.funcWinter)
# menubar
self.btnAbout.clicked.connect(self.funcAbout)
self.btnHelp.clicked.connect(self.funcHelp)
self.btnGear.setCheckable(True)
self.btnGear.clicked.connect(self.funcGear)
# summer
self.applyPropCitySummer.clicked.connect(self.newPropCitySummer)
self.applyNormCitySummer.clicked.connect(self.newNormCitySummer)
self.applyPropHighwaySummer.clicked.connect(self.newPropHighwaySummer)
self.applyNormHighwaySummer.clicked.connect(self.newNormHighwaySummer)
#winter
self.applyPropCityWinter.clicked.connect(self.newPropCityWinter)
self.applyNormCityWinter.clicked.connect(self.newNormCityWinter)
self.applyPropHighwayWinter.clicked.connect(self.newPropHighwayWinter)
self.applyNormHighwayWinter.clicked.connect(self.newNormHighwayWinter)
# +++ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
self.load_settings()
def load_settings(self):
settings = QSettings("q1577134_settings.ini", QSettings.IniFormat)
self.inputSummer.setText(settings.value('inputSummer', ""))
self.inputPropCitySummer.setText(settings.value('inputPropCitySummer', ""))
self.inputPropHighwaySummer.setText(settings.value('inputPropHighwaySummer', ""))
self.inputNormCitySummer.setText(settings.value('inputNormCitySummer', ""))
self.inputNormHighwaySummer.setText(settings.value('inputNormHighwaySummer', ""))
if self.inputPropCitySummer.text():
self.applyPropCitySummer.click()
if self.inputNormCitySummer.text():
self.applyNormCitySummer.click()
if self.inputPropHighwaySummer.text():
self.applyPropHighwaySummer.click()
if self.inputNormHighwaySummer.text():
self.applyNormHighwaySummer.click()
# - - - - - - - - - - - - - - - - - - - - -
def saveProperty(self):
settings = QSettings("q1577134_settings.ini", QSettings.IniFormat)
settings.setValue("inputSummer", self.inputSummer.text())
settings.setValue("inputPropCitySummer", self.inputPropCitySummer.text())
settings.setValue("inputPropHighwaySummer", self.inputPropHighwaySummer.text())
settings.setValue("inputNormCitySummer", self.inputNormCitySummer.text())
settings.setValue("inputNormHighwaySummer", self.inputNormHighwaySummer.text())
msg = QMessageBox.information(self,
"Настройки", f"Новые данные сохранены.")
# settings.sync()
# - - - - - - - - - - - - - - - - - - - - -
# +++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# summer
def funcSummer(self):
try:
# Пропорции город лето
if self.propCity:
propCity = self.propCity
else:
propCity = 0.3
# Нормы город лето
if self.normCity:
normCity = self.normCity
else:
normCity = 11.5
# Пропорции трасса лето
if self.propHighway:
propHighway = self.propHighway
else:
propHighway = 0.7
# Нормы трасса лето
if self.normHighway:
normHighway = self.normHighway
else:
normHighway = 8.5
inSummer = float(self.inputSummer.text())
roadCity = round(propCity * inSummer, 2)
roadHighway = round(propHighway * inSummer, 2)
resultCity = round(propCity * inSummer / 100 * normCity, 2)
resultHighway = round(propHighway * inSummer / 100 * normHighway, 2)
totalSummer = round((propCity * inSummer / 100 * normCity) + \
(propHighway * inSummer / 100 * normHighway), 2)
self.outSummer.setText(
f' Ваш расход составил {totalSummer} литров\n\n'
f' Детальная информация \n'
f' Пробег по городу {roadCity} км \n'
f' Расход по городу {resultCity} литров\n'
f' Пробег по трассе {roadHighway} км \n'
f' Расход по трассе {resultHighway} литров\n\n'
f' Нормы расхода\n'
f' Город {normCity} на 100 км\n'
f' Трасса {normHighway} на 100 км\n\n'
f' Пропорции\n'
f' Коэф. по городу {propCity}\n'
f' Коэф. по трассе {propHighway}'
)
except Exception as e:
msg = QMessageBox.information(
self, 'Внимание',
f"Что-то пошло не так. \n"
f"Проверьте исходные данные -->`inputSummer`.")
def newPropCitySummer(self):
try:
self.propCity = float(self.inputPropCitySummer.text())
self.funcSummer()
except Exception as e:
self.propCity = None
msg = QMessageBox.information(
self,
'Внимание',
f"Что-то пошло не так. <br>"
f"Проверьте исходные данные. <br><b>{self.inputPropCitySummer.text()}</b>")
def newNormCitySummer(self):
try:
self.normCity = float(self.inputNormCitySummer.text())
self.funcSummer()
except Exception as e:
self.normCity = None
msg = QMessageBox.information(
self,
'Внимание',
f"Что-то пошло не так. <br>"
f"Проверьте исходные данные. <br><b>{self.inputNormCitySummer.text()}</b>")
def newPropHighwaySummer(self):
try:
self.propHighway = float(self.inputPropHighwaySummer.text())
self.funcSummer()
except Exception as e:
self.propHighway = None
msg = QMessageBox.information(
self,
'Внимание',
f"Что-то пошло не так. <br>"
f"Проверьте исходные данные. <br><b>{self.inputPropHighwaySummer.text()}</b>")
def newNormHighwaySummer(self):
try:
self.normHighway = float(self.inputNormHighwaySummer.text())
self.funcSummer()
except Exception as e:
self.normHighway = None
msg = QMessageBox.information(
self,
'Внимание',
f"Что-то пошло не так. <br>"
f"Проверьте исходные данные. <br><b>{self.inputNormHighwaySummer.text()}</b>")
# winter
def funcWinter(self):
try:
if self.propCityWinter: # Пропорции город зима
propCityWinter = self.propCityWinter
else:
propCityWinter = 0.3
if self.normCityWinter: # Нормы город зима
normCityWinter = self.normCityWinter
else:
normCityWinter = 13.8
if self.propHighwayWinter: # Пропорции трасса зима
propHighwayWinter = self.propHighwayWinter
else:
propHighwayWinter = 0.7
if self.normHighwayWinter: # Нормы трасса зима
normHighwayWinter = self.normHighwayWinter
else:
normHighwayWinter = 10.2
inWinter = float(self.inputWinter.text())
roadCity = round(propCityWinter * inWinter, 2)
roadHighway = round(propHighwayWinter * inWinter, 2)
resultCity = round(propCityWinter * inWinter / 100 * normCityWinter, 2)
resultHighway = round(propHighwayWinter * inWinter / 100 * normHighwayWinter, 2)
totalWinter = round((propCityWinter * inWinter / 100 * normCityWinter) + (
propHighwayWinter * inWinter / 100 * normHighwayWinter), 2)
self.outWinter.setText(
f' Ваш расход составил {totalWinter} литров\n\n'
f' Детальная информация \n'
f' Пробег по городу {roadCity} км \n'
f' Расход по городу {resultCity} литров\n'
f' Пробег по трассе {roadHighway} км \n'
f' Расход по трассе {resultHighway} литров\n\n'
f' Нормы расхода\n'
f' Город {normCityWinter} на 100 км\n'
f' Трасса {normHighwayWinter} на 100 км\n\n'
f' Пропорции\n'
f' Коэф. по городу {propCityWinter}\n'
f' Коэф. по трассе {propHighwayWinter}'
)
except Exception as e:
msg = QMessageBox.information(
self, 'Внимание', f"Что-то пошло не так. \n"
f"Проверьте исходные данные.")
def newPropCityWinter(self):
try:
self.propCityWinter = float(self.inputPropCityWinter.text())
self.funcWinter()
except Exception as e:
self.propCityWinter = None
msg = QMessageBox.information(
self,
'Внимание',
f"Что-то пошло не так.\n"
f"Проверьте исходные данные.")
def newNormCityWinter(self):
try:
self.normCityWinter = float(self.inputNormCityWinter.text())
self.funcWinter()
except Exception as e:
self.normCityWinter = None
msg = QMessageBox.information(
self,
'Внимание',
f"Что-то пошло не так.\n"
f"Проверьте исходные данные.")
def newPropHighwayWinter(self):
try:
self.propHighwayWinter = float(self.inputPropHighwayWinter.text())
self.funcWinter()
except Exception as e:
self.propHighwayWinter = None
msg = QMessageBox.information(
self,
'Внимание',
f"Что-то пошло не так.\n"
f"Проверьте исходные данные.")
def newNormHighwayWinter(self):
try:
self.normHighwayWinter = float(self.inputNormHighwayWinter.text())
self.funcWinter()
except Exception as e:
self.normHighwayWinter = None
msg = QMessageBox.information(
self,
'Внимание',
f"Что-то пошло не так.\n"
f"Проверьте исходные данные.")
# bar
def funcGear(self):
if self.btnGear.isChecked():
self.resize(560, 670)
else:
self.resize(560, 450)
def funcAbout(self):
strAbout = """
BenzConfig ver. 0.7\n
Copyright © Чекаев В.А.
Ресурсы www.flaticon.com\n
Лицензия
GNU General Public License v3.0
"""
msgAbout = QMessageBox()
msgAbout.setWindowTitle("About")
# ---------------------------------> vvvvvvvvv <------------------ установите свое
msgAbout.setWindowIcon(QIcon('image.svg'))
msgAbout.setText(strAbout)
# ----------------------------------> vvvvvvvvvv <---------------- установите свое
msgAbout.setIconPixmap(QPixmap("lena3.png"))
msgAbout.setStandardButtons(QMessageBox.Ok)
msgAbout.exec()
def funcHelp(self):
strAbout = """
Пропорции перерасчета расхода
город/трасса 30% на 70%\n
HOTKEYS
Рассчет Летнего пробега клавиша "Enter"
Рассчет Зимнего пробега клавиша "Tab"
"""
msgAbout = QMessageBox()
msgAbout.setWindowTitle("Help")
# ---------------------------------> vvvvvv <------------------ установите свое
msgAbout.setWindowIcon(QIcon('im.svg'))
msgAbout.setText(strAbout)
# ---------------------------------> vvvvvvvvvv <------------- установите свое
msgAbout.setIconPixmap(QPixmap("lena3.png"))
msgAbout.setStandardButtons(QMessageBox.Ok)
msgAbout.exec()
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setStyle('Fusion')
w = MainWindow()
w.show()
sys.exit(app.exec())
design.py
без изменений