from
PyQt5.QtWidgets
import
*
from
PyQt5
import
QtCore, QtGui
from
PyQt5.QtGui
import
*
from
PyQt5.QtCore
import
*
import
math
import
sys
class
Window(QMainWindow):
def
__init__(
self
):
super
().__init__()
self
.setWindowTitle(
"Python "
)
self
.w_width
=
400
self
.w_height
=
460
self
.setGeometry(
100
,
100
,
self
.w_width,
self
.w_height)
self
.UiComponents()
self
.show()
def
UiComponents(
self
):
head
=
QLabel(
"Aspect Ratio 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)
w_label
=
QLabel(
"Width"
,
self
)
w_label.setAlignment(Qt.AlignCenter)
w_label.setGeometry(
20
,
100
,
170
,
40
)
w_label.setStyleSheet(
"QLabel"
"{"
"border : 2px solid black;"
"background : rgba(70, 70, 70, 35);"
"}"
)
w_label.setFont(QFont(
'Times'
,
9
))
self
.w_spin
=
QSpinBox(
self
)
self
.w_spin.setGeometry(
200
,
100
,
180
,
40
)
self
.w_spin.setRange(
1
,
999999
)
self
.w_spin.setFont(QFont(
'Times'
,
9
))
self
.w_spin.setAlignment(Qt.AlignCenter)
h_label
=
QLabel(
"Height"
,
self
)
h_label.setAlignment(Qt.AlignCenter)
h_label.setGeometry(
20
,
150
,
170
,
40
)
h_label.setStyleSheet(
"QLabel"
"{"
"border : 2px solid black;"
"background : rgba(70, 70, 70, 35);"
"}"
)
h_label.setFont(QFont(
'Times'
,
9
))
self
.h_spin
=
QSpinBox(
self
)
self
.h_spin.setGeometry(
200
,
150
,
180
,
40
)
self
.h_spin.setRange(
1
,
999999
)
self
.h_spin.setFont(QFont(
'Times'
,
9
))
self
.h_spin.setAlignment(Qt.AlignCenter)
calculate
=
QPushButton(
"Calculate"
,
self
)
calculate.setGeometry(
125
,
220
,
150
,
40
)
calculate.clicked.connect(
self
.calculate_action)
self
.result1
=
QLabel(
self
)
self
.result1.setAlignment(Qt.AlignCenter)
self
.result1.setGeometry(
25
,
300
,
350
,
40
)
self
.result1.setStyleSheet(
"QLabel"
"{"
"border : 3px solid black;"
"background : white;"
"}"
)
self
.result1.setFont(QFont(
'Arial'
,
11
))
self
.result2
=
QLabel(
self
)
self
.result2.setAlignment(Qt.AlignCenter)
self
.result2.setGeometry(
25
,
350
,
350
,
40
)
self
.result2.setStyleSheet(
"QLabel"
"{"
"border : 3px solid black;"
"background : white;"
"}"
)
self
.result2.setFont(QFont(
'Arial'
,
11
))
self
.result3
=
QLabel(
self
)
self
.result3.setAlignment(Qt.AlignCenter)
self
.result3.setGeometry(
25
,
400
,
350
,
40
)
self
.result3.setStyleSheet(
"QLabel"
"{"
"border : 3px solid black;"
"background : white;"
"}"
)
self
.result3.setFont(QFont(
'Arial'
,
11
))
def
calculate_action(
self
):
width
=
self
.w_spin.value()
height
=
self
.h_spin.value()
diagonal
=
width
*
*
2
+
height
*
*
2
diagonal
=
math.sqrt(diagonal)
diagonal
=
'%.2f'
%
diagonal
self
.result1.setText(
"Diagonal = "
+
diagonal)
x
=
width
/
height
x
=
'%.2f'
%
x
self
.result2.setText(
"Aspect Ratio (x:1 format) = "
+
x
+
" : 1"
)
def
computeGCD(x, y):
while
(y):
x, y
=
y, x
%
y
return
x
gcd
=
computeGCD(width, height)
width
=
width
/
/
gcd
height
=
height
/
/
gcd
self
.result3.setText(
"Aspect ratio (w:h format) = "
+
str
(width)
+
" : "
+
str
(height))
App
=
QApplication(sys.argv)
window
=
Window()
sys.exit(App.
exec
())