目前可以实现简单的计算。计算前请重置,设计的时候默认数字是0,
学了半天就做出来个这么个结果,bug不少。
python3.5 + PyQt5 +Eric6 在windows7 32位系统可以完美运行
计算器,简单学了半天就画个图实现的存在bug,部分按钮还未实现,后续优化。

代码结构如图:

 jisuan.py
import re
#匹配整数或小数的乘除法,包括了开头存在减号的情况
mul_div=re.compile("(-?\d+)(\.\d+)?(\*|/)(-?\d+)(\.\d+)?")
#匹配整数或小数的加减法,包括了开头存在减号的情况
plus_minus = re.compile("(-?\d+)(\.\d+)?(-|\+)(-?\d+)(\.\d+)?")
#匹配括号
bracket=re.compile("\([^()]*\)")
#匹配乘法的时候出现乘以负数的情况,包括了开头存在减号的情况
mul_minus_minus = re.compile("(-?\d+)(\.\d+)?(\*-)(\d+)(\.\d+)?")
#匹配除法的时候出现乘以负数的情况,包括了开头存在减号的情况
div_minus_minus = re.compile("(-?\d+)(\.\d+)?(/-)(\d+)(\.\d+)?")
#定义一个两位数的加减乘除法的运算,匹配左边的右边的数字和左边的数字,然后进行计算
def touble_cale(str_expire):
if str_expire.count("+") == 1:
right_num = float(str_expire[(str_expire.find("+")+1):])
left_num = float(str_expire[:str_expire.find("+")])
return str(right_num+left_num)
elif str_expire[1:].count("-") == 1:
right_num = float(str_expire[:str_expire.find("-",1)])
left_num = float(str_expire[(str_expire.find("-", 1) + 1):])
return str(right_num - left_num)
elif str_expire.count("*") == 1:
right_num = float(str_expire[:str_expire.find("*")])
left_num = float(str_expire[(str_expire.find("*")+1):])
return str(right_num * left_num)
elif str_expire.count("/") == 1:
right_num = float(str_expire[:str_expire.find("/")])
left_num = float(str_expire[(str_expire.find("/") + 1):])
return str(right_num / left_num) #定义一个方法用于判断是否存在乘以负数和除以负数的情况
def judge_mul_minus(str_expire):
#判断公式中乘以负数的部分
if len(re.findall("(\*-)", str_expire)) != 0:
#调用上面的正则取得*-的公式
temp_mul_minus = mul_minus_minus.search(str_expire).group()
#将匹配的部分的*-换成*并将-放到前面
temp_mul_minus_2 = temp_mul_minus.replace(temp_mul_minus,"-" + temp_mul_minus.replace("*-","*"))
#经更改的的部分与原来的部分进行替换
str_expire=str_expire.replace(temp_mul_minus,temp_mul_minus_2)
return judge_mul_minus(str_expire)
#return str_expire
# 判断公式中除以负数的部分
elif len(re.findall(r"(/-)", str_expire)) != 0:
# 调用上面的正则取得/-的公式
temp_dev_minus = div_minus_minus.search(str_expire).group()
# 将匹配的部分的/-换成/并将-放到前面
temp_dev_minus_2 = temp_dev_minus.replace(temp_dev_minus,"-" + temp_dev_minus.replace("/-","/"))
# 经更改的的部分与原来的部分进行替换
str_expire = str_expire.replace(temp_dev_minus,temp_dev_minus_2)
return judge_mul_minus(str_expire)
#调用change_sign将公式中的++换成= +-换成-
return change_sign(str_expire) #定义一个方法取将--更改为+ +-改为-
def change_sign(str_expire):
if len(re.findall(r"(\+-)", str_expire)) != 0:
str_expire = str_expire.replace("+-", "-")
return change_sign(str_expire)
elif len(re.findall(r"(--)", str_expire)) != 0:
str_expire = str_expire.replace("--", "+")
return change_sign(str_expire)
return str_expire #定义一个方法用于计算只有加减乘除的公式,优先处理乘法
def cale_mix(str_expire):
#如果公式中出现符号数字的情况即+5 -6 *8 /8的这种情况直接放回数字否则则先计算乘除在处理加减
while len(re.findall("[-+*/]",str_expire[1:])) != 0:
if len(re.findall("(\*|/)",str_expire)) != 0:
str_expire = str_expire.replace(mul_div.search(str_expire).group(),touble_cale(mul_div.search(str_expire).group()))
elif len(re.findall("(\+|-)",str_expire)) !=0:
str_expire = str_expire.replace(plus_minus.search(str_expire).group(),touble_cale(plus_minus.search(str_expire).group()))
return str_expire #定义一个方法用于去括号,并调用上述的方法进行计算
def remove_bracket(str_expire):
#判断公式中是否有括号
if len(bracket.findall(str_expire)) == 0:
return cale_mix(judge_mul_minus(str_expire))
elif len(bracket.findall(str_expire))!=0:
while len(bracket.findall(str_expire)) !=0:
#print(bracket.search(str_expire).group())
#只有存在括号优先处理括号中的内容并对内容进行替换,直到没有括号位置
str_expire = str_expire.replace(bracket.search(str_expire).group(),cale_mix(judge_mul_minus(bracket.search(str_expire).group()[1:-1])))
str_expire = cale_mix(judge_mul_minus(str_expire))
return str_expire
if __name__ == "__main__":
while True:
user_input_expire = input("请输入你的公式:(不要带空格,q表示退出):")
print("%s=%s" %(user_input_expire,remove_bracket(user_input_expire)))
continue
untitled.py
# -*- coding: utf-8 -*-
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui, QtWidgets
from Ui_untitled import Ui_Dialog
from jisuan import remove_bracket
class Dialog(QDialog, Ui_Dialog):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.setupUi(self)
@pyqtSlot()
def on_Button_6_clicked(self):
self.Edit_xianshi.insertPlainText('')
@pyqtSlot()
def on_Button_2_clicked(self):
self.Edit_xianshi.insertPlainText('')
@pyqtSlot()
def on_Button_3_clicked(self):
self.Edit_xianshi.insertPlainText('')
@pyqtSlot()
def on_Button_pingfang_clicked(self):
me=self.Edit_xianshi.toPlainText()
m=int(me) *int(me)
self.Edit_xianshi.clear()
self.Edit_xianshi.append(str(m))
@pyqtSlot()
def on_Button_add_clicked(self):
h=self.Edit_xianshi.toPlainText()
self.Edit_xianshi.clear()
self.Edit_xianshi.append(h+'+')
@pyqtSlot()
def on_Button_jian_clicked(self):
h = self.Edit_xianshi.toPlainText()
self.Edit_xianshi.clear()
self.Edit_xianshi.append(h + '-')
@pyqtSlot()
def on_Button_9_clicked(self):
self.Edit_xianshi.insertPlainText('')
@pyqtSlot()
def on_Button_chu_clicked(self):
h = self.Edit_xianshi.toPlainText()
self.Edit_xianshi.clear()
self.Edit_xianshi.append(h + '/')
@pyqtSlot()
def on_Button_cheng_clicked(self):
h = self.Edit_xianshi.toPlainText()
self.Edit_xianshi.clear()
self.Edit_xianshi.append(h + '*')
@pyqtSlot()
def on_Button_8_clicked(self):
self.Edit_xianshi.insertPlainText('')
@pyqtSlot()
def on_Button_4_clicked(self):
self.Edit_xianshi.insertPlainText('')
@pyqtSlot()
def on_Button_esc_clicked(self):
self.Edit_xianshi.clear()
@pyqtSlot()
def on_Button_7_clicked(self):
self.Edit_xianshi.insertPlainText('')
@pyqtSlot()
def on_Button_1_clicked(self):
self.Edit_xianshi.insertPlainText('')
@pyqtSlot()
def on_Button_5_clicked(self):
self.Edit_xianshi.insertPlainText('')
@pyqtSlot()
def on_Button_xiaoshu_clicked(self):
self.Edit_xianshi.insertPlainText('.')
@pyqtSlot()
def on_Button_0_clicked(self):
self.Edit_xianshi.insertPlainText('')
@pyqtSlot()
def on_Button_dengyu_clicked(self):
pe=self.Edit_xianshi.toPlainText()
m=remove_bracket(pe)
self.Edit_xianshi.clear()
self.Edit_xianshi.append(str(m)) def on_Button_fenzhi_clicked(self):
pe = self.Edit_xianshi.toPlainText()
if int(pe) ==0:
QMessageBox.information(self,u'提示',u'零不能作为分母')
Dialog()
else:
m=1/(int(pe))
self.Edit_xianshi.clear()
self.Edit_xianshi.append(str(m))
Dialog()
if __name__ =="__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
app.processEvents()
ui = Dialog()
ui.show() sys.exit(app.exec_())
Ui_untitled.py
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'C:\Users\Administrator\Desktop\pyqt5\untitled.ui'
#
# Created by: PyQt5 UI code generator 5.5
#
# WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(357, 320)
Dialog.setStyleSheet("font: 75 16pt \"Aharoni\";\n"
"background-color: rgb(206, 255, 251);")
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(201, 210, 301, 21))
self.label.setText("")
self.label.setObjectName("label")
self.Edit_xianshi = QtWidgets.QTextEdit(Dialog)
self.Edit_xianshi.setGeometry(QtCore.QRect(0, 0, 351, 41))
self.Edit_xianshi.setStyleSheet("font: 75 16pt \"Aharoni\";")
self.Edit_xianshi.setObjectName("Edit_xianshi")
self.gridLayoutWidget = QtWidgets.QWidget(Dialog)
self.gridLayoutWidget.setGeometry(QtCore.QRect(0, 30, 351, 281))
self.gridLayoutWidget.setObjectName("gridLayoutWidget")
self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
self.gridLayout.setObjectName("gridLayout")
self.Button_6 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.Button_6.setObjectName("Button_6")
self.gridLayout.addWidget(self.Button_6, 2, 2, 1, 1)
self.Button_2 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.Button_2.setObjectName("Button_2")
self.gridLayout.addWidget(self.Button_2, 3, 1, 1, 1)
self.Button_3 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.Button_3.setObjectName("Button_3")
self.gridLayout.addWidget(self.Button_3, 3, 2, 1, 1)
self.Button_fenzhi = QtWidgets.QPushButton(self.gridLayoutWidget)
self.Button_fenzhi.setObjectName("Button_fenzhi")
self.gridLayout.addWidget(self.Button_fenzhi, 1, 3, 1, 1)
self.Button_pingfang = QtWidgets.QPushButton(self.gridLayoutWidget)
self.Button_pingfang.setObjectName("Button_pingfang")
self.gridLayout.addWidget(self.Button_pingfang, 0, 3, 1, 1)
self.Button_add = QtWidgets.QPushButton(self.gridLayoutWidget)
self.Button_add.setObjectName("Button_add")
self.gridLayout.addWidget(self.Button_add, 2, 3, 1, 1)
self.Button_jian = QtWidgets.QPushButton(self.gridLayoutWidget)
self.Button_jian.setObjectName("Button_jian")
self.gridLayout.addWidget(self.Button_jian, 3, 3, 1, 1)
self.Button_9 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.Button_9.setObjectName("Button_9")
self.gridLayout.addWidget(self.Button_9, 1, 2, 1, 1)
self.Button_chu = QtWidgets.QPushButton(self.gridLayoutWidget)
self.Button_chu.setObjectName("Button_chu")
self.gridLayout.addWidget(self.Button_chu, 0, 2, 1, 1)
self.Button_cheng = QtWidgets.QPushButton(self.gridLayoutWidget)
self.Button_cheng.setObjectName("Button_cheng")
self.gridLayout.addWidget(self.Button_cheng, 0, 1, 1, 1)
self.Button_8 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.Button_8.setObjectName("Button_8")
self.gridLayout.addWidget(self.Button_8, 1, 1, 1, 1)
self.Button_4 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.Button_4.setObjectName("Button_4")
self.gridLayout.addWidget(self.Button_4, 2, 0, 1, 1)
self.Button_esc = QtWidgets.QPushButton(self.gridLayoutWidget)
self.Button_esc.setObjectName("Button_esc")
self.gridLayout.addWidget(self.Button_esc, 0, 0, 1, 1)
self.Button_7 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.Button_7.setObjectName("Button_7")
self.gridLayout.addWidget(self.Button_7, 1, 0, 1, 1)
self.Button_1 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.Button_1.setObjectName("Button_1")
self.gridLayout.addWidget(self.Button_1, 3, 0, 1, 1)
self.Button_5 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.Button_5.setObjectName("Button_5")
self.gridLayout.addWidget(self.Button_5, 2, 1, 1, 1)
self.pushButton_17 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_17.setText("")
self.pushButton_17.setObjectName("pushButton_17")
self.gridLayout.addWidget(self.pushButton_17, 4, 0, 1, 1)
self.Button_xiaoshu = QtWidgets.QPushButton(self.gridLayoutWidget)
self.Button_xiaoshu.setObjectName("Button_xiaoshu")
self.gridLayout.addWidget(self.Button_xiaoshu, 4, 1, 1, 1)
self.Button_0 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.Button_0.setStyleSheet("")
self.Button_0.setObjectName("Button_0")
self.gridLayout.addWidget(self.Button_0, 4, 2, 1, 1)
self.Button_dengyu = QtWidgets.QPushButton(self.gridLayoutWidget)
self.Button_dengyu.setObjectName("Button_dengyu")
self.gridLayout.addWidget(self.Button_dengyu, 4, 3, 1, 1) self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.Edit_xianshi.setHtml(_translate("Dialog", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'Aharoni\'; font-size:16pt; font-weight:72; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-family:\'SimSun\'; font-weight:400;\">0</span></p></body></html>"))
self.Button_6.setText(_translate("Dialog", ""))
self.Button_2.setText(_translate("Dialog", ""))
self.Button_3.setText(_translate("Dialog", ""))
self.Button_fenzhi.setText(_translate("Dialog", "1/^"))
self.Button_pingfang.setText(_translate("Dialog", "^2"))
self.Button_add.setText(_translate("Dialog", "+"))
self.Button_jian.setText(_translate("Dialog", "-"))
self.Button_9.setText(_translate("Dialog", ""))
self.Button_chu.setText(_translate("Dialog", "/"))
self.Button_cheng.setText(_translate("Dialog", "*"))
self.Button_8.setText(_translate("Dialog", ""))
self.Button_4.setText(_translate("Dialog", ""))
self.Button_esc.setText(_translate("Dialog", "esc"))
self.Button_7.setText(_translate("Dialog", ""))
self.Button_1.setText(_translate("Dialog", ""))
self.Button_5.setText(_translate("Dialog", ""))
self.Button_xiaoshu.setText(_translate("Dialog", "."))
self.Button_0.setText(_translate("Dialog", ""))
self.Button_dengyu.setText(_translate("Dialog", "=")) if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())

效果图:

github 传送门

python3.5 + PyQt5 +Eric6 实现的一个计算器的更多相关文章

  1. PyQt5+Eric6开发的一个使用菜单栏、工具栏和状态栏的示例

    前言 在做一个数据分析的桌面端程序遇到一些问题,这里简单整理下,分享出来供使用者参考. 1.网上查使用PyQt5工具栏的示例,发现很多只是一个简单的退出功能,如果有几个按钮如何处理?如何区分点击的究竟 ...

  2. Ubuntu 14.04下搭建Python3.4 + PyQt5.3.2 + Eric6.0开发平台

    引言 找了很多Python GUI工具集,还是觉得PyQt比较理想,功能强大跨平台,还支持界面设计器.花一天时间折腾了Ubuntu14.04(32位)+ Python3.4 + Qt5.3.2 + P ...

  3. Mac OS X 10.11.1下搭建Python3.4 + PyQt5.5.1 +Eric6.1.1开发平台

    由于Python易学.开源.面向对象.可移植性高.库丰富的特点,近期开始学习Python.百度了解了各款Python IDE后,还是认为Eric比较适合我,所以踏上了安装Eric坎坷之路,从选定工具到 ...

  4. 程序员修仙之路- CXO让我做一个计算器!!

    菜菜呀,个税最近改革了,我得重新计算你的工资呀,我需要个计算器,你开发一个吧 CEO,CTO,CFO于一身的CXO X总,咱不会买一个吗? 菜菜 那不得花钱吗,一块钱也是钱呀··这个计算器支持加减乘除 ...

  5. 【PyQt5-Qt Designer】PyQt5+eric6 安装和配置

    PyQt5+eric6 安装及配置 1.利用pip命令安装PyQt5 第一步:安装PyQt5 在cmd命令行中输入: pip install PyQt5 第二步:安装Qt的工具包 pip instal ...

  6. 用Qt实现一个计算器

    一· 介绍 目的: 做一个标准型的计算器.用于学习Qt基础学习. 平台: Qt 5.12.0 二· 结构框架设计 2.1最终产品样式 界面的设计大体按照win系统自带的计算器做模仿.左边是win7 的 ...

  7. 用VBA写一个计算器

    着急的 玩家 可以 跳过“============”部分 ======================================可以跳过的 部分   开始==================== ...

  8. Java初学者作业——定义一个计算器类, 实现计算器类中加、 减、 乘、 除的运算方法, 每个方法能够接收2个参数。

    返回本章节 返回作业目录 需求说明: 定义一个计算器类, 实现计算器类中加. 减. 乘. 除的运算方法, 每个方法能够接收2个参数. 实现思路: 定义计算器类. 定义计算器类中加.减.乘.除的方法. ...

  9. C#编写一个计算器

    编写一个计算器,练习在窗体上添加控件.调整控件的布局,设置或修改控件属性,编写事件处理程序的方法. 代码: using System; using System.Collections.Generic ...

随机推荐

  1. strlen 与 sizeof

    #include<stdio.h>#include<string.h>#include<string>#include<iostream>using n ...

  2. CCF-201312-4-有趣的数

    问题描述 试题编号: 201312-4 试题名称: 有趣的数 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 我们把一个数称为有趣的,当且仅当: 1. 它的数字只包含0, 1, ...

  3. PHP 使用redis实现秒杀

    PHP 使用redis实现秒杀 使用redis队列,因为pop操作是原子的,即使有很多用户同时到达,也是依次执行,推荐使用(mysql事务在高并发下性能下降很厉害,文件锁的方式也是) 先将商品库存如队 ...

  4. 初识java这个小姑娘(三)

    说烂了的面向对象 我要说的面向对象,其实是一个我自己都觉的有点恶心的东西. 它是java语言入门如此初级的一个概念.作为一个老鸟,你可以吐口水给我,我可以把它们擦干,但作为总结还得说一说. 因为对于一 ...

  5. C#设计模式之二十策略模式(Stragety Pattern)【行为型】

    一.引言   今天我们开始讲"行为型"设计模式的第七个模式,该模式是[策略模式],英文名称是:Stragety Pattern.在现实生活中,策略模式的例子也非常常见,例如,在一个 ...

  6. APP端的网络优化(DNS优化,HTTP优化)

    一.使用httpDNS优化DNS解析和缓存 一般来说在App内用域名发送请求都要经过DNS解析出ip,然后再根据ip去拿对应的资源,这个过程中,如果LocalDNS中存在这个域名对应的ip,就会直接返 ...

  7. 关于 AspNet Core 的配置文件 与VS2017 安装

    下面链接 是VS2017 安装EXE 我现在装过了就不去截图演示了,有哪位不理解的可以@我. 链接:https://pan.baidu.com/s/1hsjGuJq 密码:ug59 1.今天我给大家带 ...

  8. 第五章 MVC之 FileResult 和 JS请求二进制文件

    一.FileResult 1.简介 表示一个用于将二进制文件内容发送到响应的基类.它有三个子类: FileContentResultFilePathResultFileStreamResult 推荐阅 ...

  9. 【Java集合源代码剖析】LinkedHashmap源代码剖析

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/37867985 前言:有网友建议分析下LinkedHashMap的源代码.于是花了一晚上时间 ...

  10. [hdu 4869](14年多校I题)Turn the pokers 找规律+拓欧逆元

    Turn the pokers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...