使用 PyQt5 的纵横比计算器
在本文中,我们将了解如何使用 PyQt5 创建纵横比计算器。图像的纵横比是其宽度与高度的比率。它通常表示为用冒号分隔的两个数字,如 16:9。对于 x:y 纵横比,图像是 x 单位宽和 y 单位高。下面是计算器的外观
PyQt5是跨平台的 GUI 工具包,一组用于 Qt v5 的Python绑定。由于该库提供的工具和简单性,人们可以非常轻松地开发交互式桌面应用程序。下面是安装 PyQt5 的命令
pip install PyQt5
概念 :
用户必须选择一个宽度和高度,计算器将找到对角线和纵横比值,下面是计算对角线的公式
diagonal = math.sqrt(width**2 + height**2)
下面是计算纵横比的公式(x:1 格式)
x = width / height
下面是计算纵横比的公式(宽:高格式)
w = width / gcd
h = height / gcd
这里gcd是宽度和高度的最高公因数(HCF)
GUI Implementation Steps :
1. Create a heading label that display the calculator name
2. Create two labels for telling user to enter width and height
3. Create two spin box for entering width and height
4. Create a push button for calculating the ratios
5. Create three result labels for showing three various results
Back-End Implementation :
1. Set range to each of the spin box with minimum value equal to 1 so that user can’t enter 0 as input
2. Set various properties like alignment, geometry to each of the widget in the window
3. Add action tot he push button when it get clicked
4. Inside the push button action get the width and height from the spin boxes
5. Calculate the diagonal value and do formatting of it and show the diagonal value with the help of first result label
6. Calculate the aspect ratio by diving each other this will prove aspect ratio in x : 1 format
7. Show this ratio with the help of second result label
8. Find the gcd of width and height with the help of Euclidean Algorithm
9. Divide the width and height with the gcd
10. Show the new width and height in aspect ratio format with the help of third label
下面是实现
Python3
# importing libraries
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__()
# setting title
self.setWindowTitle("Python ")
# width of window
self.w_width = 400
# height of window
self.w_height = 460
# setting geometry
self.setGeometry(100, 100, self.w_width, self.w_height)
# calling method
self.UiComponents()
# showing all the widgets
self.show()
# method for components
def UiComponents(self):
# creating head label
head = QLabel("Aspect Ratio Calculator", self)
# setting geometry to the head
head.setGeometry(0, 10, 400, 60)
# font
font = QFont('Times', 15)
font.setBold(True)
font.setItalic(True)
font.setUnderline(True)
# setting font to the head
head.setFont(font)
# setting alignment of the head
head.setAlignment(Qt.AlignCenter)
# setting color effect to the head
color = QGraphicsColorizeEffect(self)
color.setColor(Qt.darkCyan)
head.setGraphicsEffect(color)
# creating a label
w_label = QLabel("Width", self)
# setting properties to the label
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))
# creating a spin box
self.w_spin = QSpinBox(self)
# setting geometry to the spin box
self.w_spin.setGeometry(200, 100, 180, 40)
# setting range to the spin box
self.w_spin.setRange(1, 999999)
# setting font and alignment
self.w_spin.setFont(QFont('Times', 9))
self.w_spin.setAlignment(Qt.AlignCenter)
# creating a label
h_label = QLabel("Height", self)
# setting properties to the label
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))
# creating a spin box
self.h_spin = QSpinBox(self)
# setting geometry to the spin box
self.h_spin.setGeometry(200, 150, 180, 40)
# setting range
self.h_spin.setRange(1, 999999)
# setting font and alignment
self.h_spin.setFont(QFont('Times', 9))
self.h_spin.setAlignment(Qt.AlignCenter)
# creating a push button
calculate = QPushButton("Calculate", self)
# setting geometry to the push button
calculate.setGeometry(125, 220, 150, 40)
# adding action to the calculate button
calculate.clicked.connect(self.calculate_action)
# creating a label
self.result1 = QLabel(self)
# setting properties to result label
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))
# creating a label
self.result2 = QLabel(self)
# setting properties to result label
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))
# creating a label
self.result3 = QLabel(self)
# setting properties to result label
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):
# getting width
width = self.w_spin.value()
# getting height
height = self.h_spin.value()
# calculating diagonal
diagonal = width**2 + height**2
diagonal = math.sqrt(diagonal)
# doing formatting of diagonal
diagonal = '%.2f' % diagonal
# setting text to the result 1
self.result1.setText("Diagonal = " + diagonal)
# calculating aspect ratio (x:1 format)
x = width / height
# formatting X value
x = '%.2f' % x
# setting text to the result 2
self.result2.setText("Aspect Ratio (x:1 format) = " + x + " : 1")
# calculating aspect ratio (w:h format)
# method to calculate GCD using Euclidean Algorithm
def computeGCD(x, y):
# looping
while (y):
x, y = y, x % y
# returning gcd value
return x
gcd = computeGCD(width, height)
# setting width and height value
width = width//gcd
height = height//gcd
# setting text to the result 3
self.result3.setText("Aspect ratio (w:h format) = " + str(width) + " : " + str(height))
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())
输出 :