一个基于pyqt5的对数计算器
专栏:ExASIC Oct. 27, 2024, 10:17 a.m. 99 阅读
一个基于pyqt5的对数计算器

Windows的自带计算器的log是以10为底的,如果要计算以2为底的对数就需要log(n)/log(2),稍有点麻烦。

我们利用python的math.log(n, base)函数来自己做一个专用的计算器。

下面是python代码:

#!d:/sw/python/Python37/python

import sys
import math
from PyQt5.QtWidgets import (QApplication,
                             QWidget,
                             QPushButton,
                             QLabel,
                             QLineEdit,
                             QComboBox)
from PyQt5.QtGui import QFont

class logCalc(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.qle = QLineEdit(self)
        self.qle.editingFinished.connect(self.calc)
        self.qle.move(50, 50)
        
        self.combo = QComboBox(self)
        self.combo.addItem("log2")
        self.combo.addItem("loge")
        self.combo.addItem("log10")
        self.combo.move(50, 80)
        
        self.btn = QPushButton('Calc', self)
        self.btn.clicked[bool].connect(self.calc)
        self.btn.move(120, 80)

        self.disp = QLineEdit(self)
        self.disp.setFixedWidth(200)
        self.disp.move(50, 120)

        self.setFont(QFont('Arial', 12))
        self.setWindowTitle('Log Calc')
        self.show()

    def calc(self):
        i = int(self.qle.text())
        if self.combo.currentText() == "log2":
            b = 2
        elif self.combo.currentText() == "loge":
            b = math.e
        elif self.combo.currentText() == "log10":
            b = 10
        r = math.log(i, b)
        self.disp.setText(str(r))
        self.disp.adjustSize()


if __name__ == '__main__':
    app = QApplication(sys.argv);

    w = logCalc()

    sys.exit(app.exec_())

效果如下图:

py-25-01.png

py-25-02.png

感谢阅读,更多文章点击这里:【专栏:ExASIC】
最新20篇 开设专栏