from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Python ")
self.w_width = 400
self.w_height = 450
self.setGeometry(100, 100,
self.w_width,
self.w_height)
self.UiComponents()
self.show()
def UiComponents(self):
head = QLabel("1/4 Mile Calculator",
self)
head.setGeometry(0, 10, 400, 60)
font = QFont('Times', 15)
font.setBold(True)
font.setItalic(True)
font.setUnderline(True)
head.setFont(font)
head.setAlignment(Qt.AlignCenter)
color = QGraphicsColorizeEffect(self)
color.setColor(Qt.darkCyan)
head.setGraphicsEffect(color)
e_label = QLabel("Equation", self)
e_label.setAlignment(Qt.AlignCenter)
e_label.setGeometry(20, 100, 170, 40)
e_label.setStyleSheet("QLabel"
"{"
"border : 2px solid black;"
"background : rgba(70, 70, 70, 35);"
"}")
e_label.setFont(QFont('Times', 9))
self.equation = QComboBox(self)
items = ["Huntington", "Fox", "Hale"]
self.equation.addItems(items)
self.equation.setFont(QFont('Times', 9))
self.equation.setGeometry(200, 100, 180, 40)
self.equation.setEditable(False)
w_label = QLabel("Car Weight (lb)", self)
w_label.setAlignment(Qt.AlignCenter)
w_label.setGeometry(20, 150, 170, 40)
w_label.setStyleSheet("QLabel"
"{"
"border : 2px solid black;"
"background : rgba(70, 70, 70, 35);"
"}")
w_label.setFont(QFont('Times', 9))
self.weight = QLineEdit(self)
onlyInt = QIntValidator()
self.weight.setValidator(onlyInt)
self.weight.setGeometry(200, 150, 180, 40)
self.weight.setAlignment(Qt.AlignCenter)
self.weight.setFont(QFont('Times', 9))
p_label = QLabel("Engine Power (hp) ", self)
p_label.setAlignment(Qt.AlignCenter)
p_label.setGeometry(20, 200, 170, 40)
p_label.setStyleSheet("QLabel"
"{"
"border : 2px solid black;"
"background : rgba(70, 70, 70, 35);"
"}")
p_label.setFont(QFont('Times', 9))
self.power = QLineEdit(self)
onlyInt = QIntValidator()
self.power.setValidator(onlyInt)
self.power.setGeometry(200, 200, 180, 40)
self.power.setAlignment(Qt.AlignCenter)
self.power.setFont(QFont('Times', 9))
calculate = QPushButton("Calculate Time", self)
calculate.setGeometry(125, 270, 150, 40)
calculate.clicked.connect(self.calculate_action)
self.result = QLabel(self)
self.result.setAlignment(Qt.AlignCenter)
self.result.setGeometry(30, 340, 340, 80)
self.result.setWordWrap(True)
self.result.setStyleSheet("QLabel"
"{"
"border : 3px solid black;"
"background : white;"
"}")
self.result.setFont(QFont('Arial', 11))
def calculate_action(self):
weight = self.weight.text()
power = self.power.text()
if len(weight) == 0 or len(power) == 0:
return
weight = int(weight)
power = int(power)
if power == 0 or weight == 0:
return
equation = self.equation.currentIndex()
if equation == 0:
constant = 6.290
elif equation == 1:
constant = 6.269
else:
constant = 5.825
result = constant * ((weight/power)**(1/3))
result = "{:.2f}".format(result)
self.result.setText("1/4 Mile Elapsed Time : "
+ str(result) + " seconds")
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())