码迷,mamicode.com
首页 > 编程语言 > 详细

Python制作简易计算器(PyQt5)

时间:2020-03-26 21:24:38      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:event   对象   browser   错误   pyqt5   str   click   ==   部分   

工具

使用PyQt5,QtDegsiner完成的计算器

还有部分问题未处理。

体验

使用 pip install pyqt5,安装pyqt5后,运行脚本即可即可

应用截图

技术图片技术图片

技术图片

核心部分代码:事件绑定,运算处理

    def event_strigger(self):
        self.pushButton.clicked.connect(lambda: self.clickButton(self.pushButton))
        self.pushButton_2.clicked.connect(lambda: self.clickButton(self.pushButton_2))
        self.pushButton_3.clicked.connect(lambda: self.clickButton(self.pushButton_3))
        self.pushButton_4.clicked.connect(lambda: self.clickButton(self.pushButton_4))
        self.pushButton_5.clicked.connect(lambda: self.clickButton(self.pushButton_5))
        self.pushButton_6.clicked.connect(lambda: self.clickButton(self.pushButton_6))
        self.pushButton_7.clicked.connect(lambda: self.clickButton(self.pushButton_7))
        self.pushButton_8.clicked.connect(lambda: self.clickButton(self.pushButton_8))
        self.pushButton_9.clicked.connect(lambda: self.clickButton(self.pushButton_9))
        self.pushButton_10.clicked.connect(lambda: self.clickButton(self.pushButton_10))
        self.pushButton_11.clicked.connect(lambda: self.clickButton(self.pushButton_11))
        self.pushButton_12.clicked.connect(lambda: self.clickButton(self.pushButton_12))
        self.pushButton_13.clicked.connect(lambda: self.clickButton(self.pushButton_13))
        self.pushButton_14.clicked.connect(lambda: self.clickButton(self.pushButton_14))
        self.pushButton_15.clicked.connect(lambda: self.clickButton(self.pushButton_15))
        self.pushButton_16.clicked.connect(lambda: self.clickButton(self.pushButton_16))
        self.pushButton_17.clicked.connect(lambda: self.clickButton(self.pushButton_17))
        self.pushButton_18.clicked.connect(lambda: self.clickButton(self.pushButton_18))
        self.pushButton_19.clicked.connect(lambda: self.clickButton(self.pushButton_19))
        self.pushButton_20.clicked.connect(lambda: self.clickButton(self.pushButton_20))



    # 按钮点击事件
    def clickButton(self, pbt):
        """

        :param pbt: 按钮的对象
        :return:
        """
        pbt_name = pbt.text()
        if pbt_name == "C":
            self.pushButton.setText("AC")
            self.textBrowser.setText("0")
        elif pbt_name == "AC":
            self.textBrowser.toPlainText()
        elif pbt_name == "+/-":
            # 获取到文本框中内容
            last_result = self.textBrowser.toPlainText()
            if last_result[0] != "-":
                after_result = "-" + last_result
                logging.info(after_result)
                self.textBrowser.setText(after_result)

            else:
                # 将结果显示栏获取的内容转换成int
                try:
                    last_result = int(eval(last_result))
                    last_result = str(abs(last_result))
                    self.textBrowser.setText(last_result)
                except Exception as e:
                    logging.error(e)
                    self.textBrowser.setText("错误")

        elif pbt_name == "%":
            last_result = self.textBrowser.toPlainText()
            # 判断最后一位为运算符时,去百分数
            if last_result[-1] not in ["/", "+", "*", "-"]:
                content = self.textBrowser.toPlainText()
                if eval(content) % 100 == 0:
                    result = eval(content) // 100
                else:
                    result = eval(content) / 100
                self.textBrowser.setText(str(result))
        elif pbt_name == "?":
            self.redundant(self.textBrowser.toPlainText(), "/")
        elif pbt_name == "?":
            self.redundant(self.textBrowser.toPlainText(), "+")
        elif pbt_name == "??":
            self.redundant(self.textBrowser.toPlainText(), "*")
        elif pbt_name == "?":
            self.redundant(self.textBrowser.toPlainText(), "-")
        elif pbt_name == ".":
            # 拿到按下. 直接的字符串
            result = self.textBrowser.toPlainText()
            self.redundant(result, ".")
            # 判断字符串是否含有1个以上.
            if self.textBrowser.toPlainText().count(".") > 1:
                # 如果有设置成最开始的字符串
                self.textBrowser.setText(result)

        elif pbt_name == "=":
            try:
                result = eval(self.textBrowser.toPlainText())
                logging.info("%s,计算结果%s"%(self.textBrowser.toPlainText(), result))
                self.textBrowser.setText(str(result))
            except Exception as e:
                self.textBrowser.setText("发生了错误,请联系管理员!或重试")
                logging.error("Exception", e)
        else:
            if self.textBrowser.toPlainText() == "0" and pbt_name != ".":
                self.textBrowser.setText(pbt_name)
                if pbt_name != "0":
                    self.pushButton.setText("C")
            else:
                last_result = self.textBrowser.toPlainText()
                self.textBrowser.setText(last_result + pbt_name)

    def redundant(self, x, y):
        """
        过滤运算符后直接跟其他类型运算符的情况
        :param x:文本框中当前的字符串
        :param y: 当前输入的按钮
        :return:
        """
        if x[-1] not in ["/", "+", "*", "-"]:
            last_result = x + y
            self.textBrowser.setText(last_result)
            self.pushButton.setText("C")

源代码

Python制作简易计算器(PyQt5)

标签:event   对象   browser   错误   pyqt5   str   click   ==   部分   

原文地址:https://www.cnblogs.com/thloveyl/p/12577275.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!