大家好,小编来为大家解答以下问题,python做出来的作品,python作品创意简单,今天让我们一起来看看吧!
Python语言学习-----简易计算器的制作
我是一名普通的大二在校学生,在大二第一学期开设的课程里接触到Python语言与PyQt。下面向大家展示的是我的python作品–简易计算机火车头采集器伪原创插件。
作品可能有不完善的地方和问题,希望大家能多多给我建议。
应用程序:Python,PyQt,VScode
一·作品展示
本项目所设计的计算器的Qt界面大体如下图
在此计算器的显示部分,为了美观我使用了一个多行文本框(Text Edit),并将两个标签(Label)覆盖其上,用两行标签来显示计算器计算内容。
二.代码部分
(1)模块,在代码部分我加入了math模块
(2)数字输入
以数字1为例,代码如下
#如果当前a的值为0,则将0换为1
#如果a为空,则退出函数
(3)基本运算
思路:上一级运算交由下一级运算符计算,下一级运算由更下一级运算
例如:1+2-3=0。按照正常的思维逻辑,我们应先计算1+2,得到结果3,再用所得结果减掉最后的3,最后结果为0。而此次项目的运算思维略有不同,当+号触发时,计算器并不会进行计算,只是将加数1保存下来,等到-号触发时,计算器才会计算出1+2=3,以此类推。
具体程序(以加法为例)如下:
函数中定义全局变量A,使用A获取计算每一步的出的结果。然后后一位的运算符激活时,获取A的值,进行下一步计算
#[-1]字符串取到倒数第一个元素
#“-1”倒数第一个元素
#float()将整数和字符串换成浮点数(双精度,64位)
以上是进行最终结果的计算
(4)扩展运算
此计算器除了加减乘除基础运算以外,还能够进行扩展运算比如:三角函数、反三角函数、反比例函数、对数函数。
而这一部分的我们可以调用math库中的函数进行计算。
因为不明确知道根号符号后面有多少字符串,所以以“b=a+√”的形式表示;
#log(d,b)为以b为底d的对数
不足:在计算关于根号(root)的代码时,labnumber1必须是要有字符的,如果没有字符则报错。
(5)清空与退格
在此计算器中共有两种删除模式:退格(backspace)和清空(clear)。退格的删除方式可以将labNumber1框里的内容从右往左依次删除,清空的方式可以将labNumber1与labNumber2中所有的内容删除。
两种删除模式代码分别如下:
三.代码展示
def One(self):a=self.labNumber1.text()if a=='0':b='1'else: b = self.labNumber1.text() + '1'self.labNumber1.setText(str(b))def Two(self):a=self.labNumber1.text()if a=='0':b='2'else: b = self.labNumber1.text() + '2'self.labNumber1.setText(str(b))def Three(self):a=self.labNumber1.text()if a=='0':b='3'else: b = self.labNumber1.text() + '3'self.labNumber1.setText(str(b))def Four(self):a=self.labNumber1.text()if a=='0':b='4'else: b = self.labNumber1.text() + '4'self.labNumber1.setText(str(b))def Five(self):a=self.labNumber1.text()if a=='0':b='5'else: b = self.labNumber1.text() + '5'self.labNumber1.setText(str(b))def Six(self):a=self.labNumber1.text()if a=='0':b='6'else: b = self.labNumber1.text() + '6'self.labNumber1.setText(str(b))def Seven(self):a=self.labNumber1.text()if a=='0':b='7'else: b = self.labNumber1.text() + '7'self.labNumber1.setText(str(b))def Eight(self):a=self.labNumber1.text()if a=='0':b='8'else: b = self.labNumber1.text() + '8'self.labNumber1.setText(str(b))def Nine(self):a=self.labNumber1.text()if a=='0':b='9'else: b = self.labNumber1.text() + '9'self.labNumber1.setText(str(b))def Zero(self):a=self.labNumber1.text()if a=='0':b='9'else: b = self.labNumber1.text() + '0'self.labNumber1.setText(str(b))def Point(self):a=self.labNumber1.text()if a=='':returna = self.labNumber1.text() + '.'self.labNumber1.setText(str(a))def Add(self):a =self.labNumber1.text()b =self.labNumber2.text()if a=='':returnd=float(a)c = b + a + '+'self.labNumber2.setText(c)self.labNumber1.setText("")global Aif b=='':A=dreturnif b[-1]=="+":A=float(A+d)def Subtract(self):a =self.labNumber1.text()b =self.labNumber2.text()if a=='':returnd=float(a)c = b + a + '-'self.labNumber2.setText(c)self.labNumber1.setText("")global Aif b=='':A=dreturnif b[-1]=="-":A=float(A-d)def Multiply(self):a =self.labNumber1.text()b =self.labNumber2.text() if a=='':returnd=float(a) c = b + a + '×'self.labNumber2.setText(c)self.labNumber1.setText("")global Aif b=='':A=dreturnif b[-1]=="×":A=float(A*d)def Divide(self):a =self.labNumber1.text()b =self.labNumber2.text()if a=='':returnd=float(a)c = b + a + '÷'self.labNumber2.setText(c)self.labNumber1.setText("")global Aif b=='':A=dreturnif b[-1]=="÷":if d==0:returnA=float(A/d)def Sin(self):self.labNumber2.setText("sin")def Cos(self):self.labNumber2.setText("cos")def Tan(self):self.labNumber2.setText("tan") def Root(self):a =self.labNumber1.text()c =self.labNumber2.text()if '+' in c or '-' in c or '×' in c or '÷' in c:returnif a=='':returnb = a + "√"self.labNumber2.setText(b)self.labNumber1.setText("")def Special(self):a=float(self.labNumber1.text())c =self.labNumber2.text()if '+' in c or '-' in c or '×' in c or '÷' in c:returnb=1/aself.labNumber1.setText(str(b))def Involution(self):a =self.labNumber1.text()c =self.labNumber2.text()if '+' in c or '-' in c or '×' in c or '÷' in c:returnif a=='':returnb = a + "^"self.labNumber2.setText(b)self.labNumber1.setText("")def Log(self):a =self.labNumber1.text()c =self.labNumber2.text()if '+' in c or '-' in c or '×' in c or '÷' in c:returnif a=='':returnb = a + "log"self.labNumber2.setText(b)self.labNumber1.setText("")def Backspace(self):a=self.labNumber1.text()if a=='':returnif a!='':u = a[0:-1]self.labNumber1.setText(str(u))def Delete(self):self.labNumber1.setText('')self.labNumber2.setText('')def Equality(self):a=self.labNumber2.text()o=self.labNumber1.text()if a=='' and o=='':returnif a=='' and o!="":c=float(o)returnb=float(self.labNumber1.text())if o=='':b=0else:b=float(self.labNumber1.text())if a[-1]=='+' or a[-1] == '-' or a[-1] == '×' or a[-1] == '÷' :c=float(A)if a[-1]=="+":c=float(A+b)if a[-1]=="-":c=float(A-b)if a[-1]=="×":c=float(A*b)if a[-1]=="÷":if b==0:returnc=float(A/b)if a[-1] == '√':c = math.sqrt(int(a[0:-1])) if a[-1] == '^':c = float(a[0:-1]) ** b if a[-1] == 'g':d=int(a[0:-3])c = math.log(d,b) if a=="sin":c=math.sin(b)if a=="cos":c=math.cos(b)if a=="tan":c=math.tan(b) if c % 1 == 0 :d=int(c)self.labNumber1.setText(str(d))else:self.labNumber1.setText(str(c))self.labNumber2.setText('')
以上就是我的结课作品,若果有需要完善和改正的地方,欢迎各位留言指正。