📜  使用 PyQt5 的比率计算器

📅  最后修改于: 2022-05-13 01:54:44.400000             🧑  作者: Mango

使用 PyQt5 的比率计算器

在本文中,我们将了解如何使用 PyQt5 创建比率计算器。比率是存在于两个特定数字之间的对比,被定义为比率。这个比率计算器是为了计算这种对比度并找出这些数字之间的关系而开发的。下面是计算器的外观

PyQt5是跨平台的 GUI 工具包,一组用于 Qt v5 的Python绑定。由于该库提供的工具和简单性,人们可以非常轻松地开发交互式桌面应用程序。下面是安装 PyQt5 的命令

pip install PyQt5

概念 :
用户必须选择一个比率,然后输入另一个数字,计算器将找到相应的比率值,以下是使用的公式

A / B = C / X

这里 A 和 B 是选定的比率,C 是输入的数字,我们必须找到对应的比率值

下面是实现

# importing libraries
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__()
  
        # setting title
        self.setWindowTitle("Python ")
  
        # width of window
        self.w_width = 550
  
        # height of window
        self.w_height = 300
  
        # 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("Ratio Calculator", self)
  
        head.setWordWrap(True)
  
        # setting geometry to the head
        head.setGeometry(0, 10, 550, 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 spin box
        self.a_spin = QSpinBox(self)
  
        # setting geometry
        self.a_spin.setGeometry(50, 90, 90, 35)
  
        # setting alignment
        self.a_spin.setAlignment(Qt.AlignCenter)
  
        # setting range
        self.a_spin.setRange(1, 999999)
  
        # creating a label
        l1 = QLabel(" : ", self)
  
        # setting geometry
        l1.setGeometry(140, 90, 30, 35)
  
        # setting alignment
        l1.setAlignment(Qt.AlignCenter)
  
        # creating a spin box
        self.b_spin = QSpinBox(self)
  
        # setting geometry
        self.b_spin.setGeometry(170, 90, 90, 35)
  
        # setting alignment
        self.b_spin.setAlignment(Qt.AlignCenter)
  
        # setting range
        self.b_spin.setRange(1, 999999)
  
        # creating a label
        l2 = QLabel(" = ", self)
  
        # setting geometry
        l2.setGeometry(260, 90, 30, 35)
  
        # setting alignment
        l2.setAlignment(Qt.AlignCenter)
  
        # creating a spin box
        self.c_spin = QSpinBox(self)
  
        # setting geometry
        self.c_spin.setGeometry(290, 90, 90, 35)
  
        # setting alignment
        self.c_spin.setAlignment(Qt.AlignCenter)
  
        # setting range
        self.c_spin.setRange(1, 999999)
  
        # creating a label
        l3 = QLabel(" : ", self)
  
        # setting geometry
        l3.setGeometry(380, 90, 20, 35)
  
        # setting alignment
        l3.setAlignment(Qt.AlignCenter)
  
        # creating a label
        lx = QLabel("X", self)
  
        # setting geometry
        lx.setGeometry(410, 90, 90, 35)
  
        # setting alignment
        lx.setAlignment(Qt.AlignCenter)
  
        # setting style sheet
        lx.setStyleSheet("QLabel"
                         "{"
                         "border : 1px solid black;"
                         "background-color : white;"
                         "font-size : 15px;"
                         "}")
  
        # creating a push button
        calculate = QPushButton("Calculate", self)
  
        # setting geometry to the push button
        calculate.setGeometry(175, 150, 200, 40)
  
        # adding action to the button
        calculate.clicked.connect(self.calculate)
  
        # adding color effect to the push button
        color = QGraphicsColorizeEffect()
        color.setColor(Qt.darkGreen)
        calculate.setGraphicsEffect(color)
  
        # creating a label to show result
        self.result = QLabel(self)
  
        # setting properties to result label
        self.result.setAlignment(Qt.AlignCenter)
  
        # setting geometry
        self.result.setGeometry(125, 210, 300, 60)
  
        # making it multi line
        self.result.setWordWrap(True)
  
        # setting stylesheet
        # adding border and background
        self.result.setStyleSheet("QLabel"
                                  "{"
                                  "border : 3px solid black;"
                                  "background : white;"
                                  "}")
  
        # setting font
        self.result.setFont(QFont('Arial', 11))
  
  
    def calculate(self):
  
        # getting spin box 'a' value
        a = self.a_spin.value()
  
        # getting spin box 'b' value
        b = self.b_spin.value()
  
        # getting spin box 'c value
        c = self.c_spin.value()
  
        # calculating 'x' value
        x = (c * b) / a
  
        # removing decimal if decimal is zero
        if x % 1 == 0:
            x = int(x)
  
  
        # setting text to the result label
        self.result.setText(str(a) + " : " + str(b) + " = "
                            + str(c) + " : " + str(x))
  
  
  
  
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
# start the app
sys.exit(App.exec())

输出 :