Как сделать функцию в Python на QT
Есть приложение, подскажите как сделать так, чтобы можно было вносить свои параметры "Пропорции" inputPropCitySummer
, "Нормы расхода" inputNormCitySummer
и при нажатии на кнопку "Применить" applyPropCitySummer
и applyNormCitySummer
программа сохраняла внесенное в переменные propCity
и normCity
?
app.py
import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QMessageBox
from PySide6.QtGui import QPixmap, QIcon
from design import Ui_root
sys.argv += ['-platform', 'windows:darkmode=1']
class MainWindow(QMainWindow, Ui_root):
def __init__(self):
super().__init__()
self.setupUi(self)
self.btnSummer.clicked.connect(self.funcSummer)
self.outSummer.show()
self.btnWinter.clicked.connect(self.funcWinter)
self.outWinter.show()
self.btnAbout.clicked.connect(self.funcAbout)
self.btnHelp.clicked.connect(self.funcHelp)
self.btnGear.setCheckable(True)
self.btnGear.clicked.connect(self.funcGear)
def funcGear(self):
if self.btnGear.isChecked():
self.resize(560, 670)
else:
self.resize(560, 450)
def funcSummer(self):
try:
propCity = 0.3 or float(self.inputPropCitySummer.text())
propHighway = 0.7 or float(self.inputPropHighwaySummer.text())
normCity = 11.5 or float(self.inputNormCitySummer.text())
normHighway = 8.5 or float(self.inputNormHighwaySummer.text())
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)
totalCity = round(inSummer / 100 * normCity, 2)
totalHighway = round(inSummer / 100 * normHighway, 2)
self.outSummer.setText(
f' '
f'Ваш пробег по городу {roadCity} км\n Потрачено по городу {resultCity} литров\n'
f' Ваш пробег по трассе {roadHighway} км\n Потрачено по трассе {resultHighway} литров\n\n'
f' Ваш общий расход {totalSummer} литров\n\n'
f' Нормы расхода\n'
f' Город {normCity} на 100 км\n'
f' Трасса {normHighway} на 100 км\n\n'
f' Город 100% {totalCity} литров\n'
f' Трасса 100% {totalHighway} литров'
)
except Exception as e:
msg = QMessageBox.information(
self, 'Внимание', f"Что-то пошло не так. \n"
f"Проверьте исходные данные.")
def funcWinter(self):
try:
inWinter = float(self.inputWinter.text())
roadCity = round(0.3 * float(inWinter), 2)
roadHighway = round(0.7 * float(inWinter), 2)
resultCity = round(0.3 * float(inWinter) / 100 * 13.8, 2)
resultHighway = round(0.7 * float(inWinter) / 100 * 10.2, 2)
totalWinter = round((0.3 * inWinter / 100 * 13.8) + (0.7 * inWinter / 100 * 10.2), 2)
"""totalCity = round(float(inWinter) / 100 * 13.8, 2)
totalHighway = round(float(inWinter) / 100 * 10.2, 2)"""
self.outWinter.setText(
f''
f' Ваш пробег по городу {roadCity} км\n Потрачено по городу {resultCity} литров\n\n'
f' Ваш пробег по трассе {roadHighway} км\n Потрачено по трассе {resultHighway} литров\n\n'
f' Ваш общий расход {totalWinter} литров\n\n'
f' Нормы расхода\n'
f' Город: 13.8 на 100 км\n'
f' Трасса: 10.2 на 100 км\n'
)
except Exception as e:
msg = QMessageBox.information(
self, 'Внимание', f"Что-то пошло не так. \n"
f"Проверьте исходные данные.")
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")
msgAbout.setWindowIcon(QIcon('infoMenu.svg'))
msgAbout.setText(strAbout)
msgAbout.setIconPixmap(QPixmap("iconAbout.png"))
msgAbout.setStandardButtons(QMessageBox.Ok)
msgAbout.exec()
def funcHelp(self):
strAbout = """
Пропорции перерасчета расхода
город/трасса 30% на 70%\n
HOTKEYS
Рассчет Летнего пробега клавиша "Enter"
Рассчет Зимнего пробега клавиша "Tab"
"""
msgAbout = QMessageBox()
msgAbout.setWindowTitle("Help")
msgAbout.setWindowIcon(QIcon('helpMenu.svg'))
msgAbout.setText(strAbout)
msgAbout.setIconPixmap(QPixmap("IconHelp.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
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"")
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"info.png", QSize(), QIcon.Normal, QIcon.Off)
self.btnAbout.setIcon(icon1)
self.btnHelp = QPushButton(self.grid_w)
self.btnHelp.setObjectName(u"btnHelp")
self.btnHelp.setGeometry(QRect(50, 417, 30, 28))
self.btnHelp.setCursor(QCursor(Qt.PointingHandCursor))
self.btnHelp.setStyleSheet(u"")
icon2 = QIcon()
icon2.addFile(u"help.png", QSize(), QIcon.Normal, QIcon.Off)
self.btnHelp.setIcon(icon2)
self.btnHelp.setAutoDefault(False)
self.btnHelp.setFlat(False)
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"gear.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.defis = QLabel(self.grid_w)
self.defis.setObjectName(u"defis")
self.defis.setGeometry(QRect(20, 440, 521, 20))
self.defis.setStyleSheet(u"color: rgba(200, 200, 200, 200);")
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"Space", 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.defis.setText(QCoreApplication.translate("root", u"__________________________________________________________________________________________________________", 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
→ Ссылка
Не уверен что правильно понял вас, но как вариант попробуйте так:
import sys
'''
from PySide6.QtWidgets import QApplication, QMainWindow, QMessageBox
from PySide6.QtGui import QPixmap, QIcon
'''
from PyQt5.Qt import *
#from design import Ui_root
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)
# -------------------> vvvvvv <----------------------------------- установите свое
icon.addFile(u"Ok.png", 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"")
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"info.png", QSize(), QIcon.Normal, QIcon.Off)
# -------------------> vvvvvvvv <----------------------------------- установите свое
icon1.addFile(u"ball.png", QSize(), QIcon.Normal, QIcon.Off)
self.btnAbout.setIcon(icon1)
self.btnHelp = QPushButton(self.grid_w)
self.btnHelp.setObjectName(u"btnHelp")
self.btnHelp.setGeometry(QRect(50, 417, 30, 28))
self.btnHelp.setCursor(QCursor(Qt.PointingHandCursor))
self.btnHelp.setStyleSheet(u"")
icon2 = QIcon()
# icon2.addFile(u"help.png", QSize(), QIcon.Normal, QIcon.Off)
# -------------------> vvvvvvvv <----------------------------------- установите свое
icon2.addFile(u"ball.png", QSize(), QIcon.Normal, QIcon.Off)
self.btnHelp.setIcon(icon2)
self.btnHelp.setAutoDefault(False)
self.btnHelp.setFlat(False)
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"gear.png", QSize(), QIcon.Normal, QIcon.Off)
# -------------------> vvvvvvvv <----------------------------------- установите свое
icon3.addFile(u"im.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.defis = QLabel(self.grid_w)
self.defis.setObjectName(u"defis")
self.defis.setGeometry(QRect(20, 440, 521, 20))
self.defis.setStyleSheet(u"color: rgba(200, 200, 200, 200);")
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)
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"Space", 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))
# PropCitySummer
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))
# NormCitySummer
self.applyNormCitySummer.setText(QCoreApplication.translate("root",
u"\u041f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c", None))
self.defis.setText(QCoreApplication.translate("root", u"__________________________________________________________________________________________________________", 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.inputPropCitySummer.setPlaceholderText(QCoreApplication.translate("root", u"Город inputPropCitySummer", 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.inputNormCitySummer.setPlaceholderText(QCoreApplication.translate("root",
u"\u0413\u043e\u0440\u043e\u0434 inputNormCitySummer", 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))
#? sys.argv += ['-platform', 'windows:darkmode=1']
class MainWindow(QMainWindow, Ui_root):
def __init__(self):
super().__init__()
self.setupUi(self)
# +++ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
self.propCity = None # !!! +++
self.normCity = None # !!! +++
# +++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
self.btnSummer.clicked.connect(self.funcSummer)
#? self.outSummer.show()
self.btnWinter.clicked.connect(self.funcWinter)
#? self.outWinter.show()
self.btnAbout.clicked.connect(self.funcAbout)
self.btnHelp.clicked.connect(self.funcHelp)
self.btnGear.setCheckable(True)
self.btnGear.clicked.connect(self.funcGear)
# +++ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
self.applyPropCitySummer.clicked.connect(self.new_propCity)
self.applyNormCitySummer.clicked.connect(self.new_normCity)
def new_propCity(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 new_normCity(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 funcGear(self):
if self.btnGear.isChecked():
self.resize(560, 670)
else:
self.resize(560, 450)
def funcSummer(self):
try:
# +++ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
# propCity = 0.3 or float(self.inputPropCitySummer.text())
# normCity = 11.5 or float(self.inputNormCitySummer.text())
if self.propCity:
propCity = self.propCity
else:
propCity = 0.3
if self.normCity:
normCity = self.normCity
else:
normCity = 11.5
# +++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
propHighway = 0.7 or float(self.inputPropHighwaySummer.text())
normHighway = 8.5 or float(self.inputNormHighwaySummer.text())
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)
totalCity = round(inSummer / 100 * normCity, 2)
totalHighway = round(inSummer / 100 * normHighway, 2)
self.outSummer.setText(
f' '
f'Ваш пробег по городу {roadCity} км\n Потрачено по городу {resultCity} литров\n'
f' Ваш пробег по трассе {roadHighway} км\n Потрачено по трассе {resultHighway} литров\n\n'
f' Ваш общий расход {totalSummer} литров\n\n'
f' Нормы расхода\n'
f' Город {normCity} на 100 км\n'
f' Трасса {normHighway} на 100 км\n\n'
f' Город 100% {totalCity} литров\n'
f' Трасса 100% {totalHighway} литров'
)
except Exception as e:
msg = QMessageBox.information(
self, 'Внимание', f"Что-то пошло не так. \n"
f"Проверьте исходные данные.")
def funcWinter(self):
try:
inWinter = float(self.inputWinter.text())
roadCity = round(0.3 * float(inWinter), 2)
roadHighway = round(0.7 * float(inWinter), 2)
resultCity = round(0.3 * float(inWinter) / 100 * 13.8, 2)
resultHighway = round(0.7 * float(inWinter) / 100 * 10.2, 2)
totalWinter = round((0.3 * inWinter / 100 * 13.8) + (0.7 * inWinter / 100 * 10.2), 2)
"""totalCity = round(float(inWinter) / 100 * 13.8, 2)
totalHighway = round(float(inWinter) / 100 * 10.2, 2)"""
self.outWinter.setText(
f''
f' Ваш пробег по городу {roadCity} км\n Потрачено по городу {resultCity} литров\n\n'
f' Ваш пробег по трассе {roadHighway} км\n Потрачено по трассе {resultHighway} литров\n\n'
f' Ваш общий расход {totalWinter} литров\n\n'
f' Нормы расхода\n'
f' Город: 13.8 на 100 км\n'
f' Трасса: 10.2 на 100 км\n'
)
except Exception as e:
msg = QMessageBox.information(
self, 'Внимание', f"Что-то пошло не так. \n"
f"Проверьте исходные данные.")
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")
# msgAbout.setWindowIcon(QIcon('infoMenu.svg'))
# ---------------------------------> vvvvvvvvv <------------------ установите свое
msgAbout.setWindowIcon(QIcon('image.svg'))
msgAbout.setText(strAbout)
# msgAbout.setIconPixmap(QPixmap("iconAbout.png"))
# ----------------------------------> 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")
# msgAbout.setWindowIcon(QIcon('helpMenu.svg'))
# ---------------------------------> vvvvvv <------------------ установите свое
msgAbout.setWindowIcon(QIcon('im.svg'))
msgAbout.setText(strAbout)
# msgAbout.setIconPixmap(QPixmap("IconHelp.png"))
# ---------------------------------> 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())