猫史档案馆


【Python作品分享】计算器【作品秀】

用户:NirvanaOvONirvanaOvO查看:2 回复:8 评论:2 创建时间:2020-05-18T22:07:39


【作品展示】

center_image

 

【作品介绍】

大家好,我是不想吃鱼的猫,今天又双给大家带来Python作品

 

【作品源代码】

#导入所需模块
import re
import tkinter, tkinter.messagebox
 
#创建窗口,并设置大小与标题
window = tkinter.Tk() 
window.geometry('300x270+400+100') 
#不允许改变窗口大小
window.resizable(False, False) 
#设置窗口标题
window.title('计算器')

#
def ButtonOperation(m): 
    theme = themeVar.get() 
    if theme.startswith('.'):
        theme = '0' + theme  
    if m in '0123456789':
        theme += m  # 0-9中哪个键按下了,就在theme字符串中增添
    elif m == '.':
        
        Segmentationpart = re.split(r'\+|-|\*|/',theme)[-1]
        if '.' in Segmentationpart:
            tkinter.messagebox.showerror('错误','重复出现的小数点')
            return
        else:
            theme += m #如果没有问题,则在theme字符串中增添
    elif m == 'C':
        theme = '' 
        if theme.endswith(operators):
            tkinter.messagebox.showerror('错误', '不允许存在连续运算符')
            return
        theme += m
    elif m == 'Sqrt':
        n = theme.split('.') #从.处分割存入n,n是一个列表
        if all(map(lambda x:x.isdigit(),n)):
            theme=eval(theme)**0.5
        else:
            tkinter.messagebox.showerror('错误', '算式错误')
            return
    elif m == '=':
        try: #异常处理
            #对输入的算式求值
            theme=str(eval(theme))
        except:
            tkinter.messagebox.showerror('错误', '算式错误')
            return
    themeVar.set(theme) #将结果显示到文本框中,使用set方法可以取值

#设置文本框的大小和位置
themeVar = tkinter.StringVar(window, '')
themeEntry = tkinter.Entry(window, textvariable=themeVar) 
themeEntry['state'] = 'readonly' 
themeEntry.place(x=10, y=10, width=280, height=20) #文本框在窗口中的xy坐标位置

figure = list('0123456789.')+['Sqrt']
index = 0

for row in range(4):#row:行
    for col in range(3):#col:列
        a = figure[index] #按索引从list中取值,和c语言中的数组类似
        index += 1 #索引号递增
        m_figure = tkinter.Button(window, text=a, command=lambda x=a:ButtonOperation(x)) 
        m_figure.place(x=20+col*70, y=80+row*50, width=50, height=20) 

operators = ('+', '-', '*', '/', '**', '//')

for index,operator in enumerate(operators):
    m_operator = tkinter.Button(window, text=operator, bg = 'orange', command=lambda x=operator:ButtonOperation(x))
    m_operator.place(x = 230, y = 80+index*30, width = 50, height = 20)

m_Clear = tkinter.Button(window,text='C',bg = 'red',command=lambda:ButtonOperation('C'))

m_Clear.place(x=40,y=40,width=80,height=20)
m_EqualSign = tkinter.Button(window,text='=',bg = 'yellow',command=lambda:ButtonOperation('='))
m_EqualSign.place(x=170,y=40,width=80,height=20)
 
window.mainloop() 

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
沉著的飞叶龙uZWJ沉著的飞叶龙uZWJ

<pre class="language-python"><code>

import math import tkinter import easygui#准备 calculator=tkinter.Tk()#创建窗口 calculator.geometry('400x400')#设置大小 calculator.resizable(0,0)#不许拉动 calculator.title('计算器')#设置标题 content=tkinter.StringVar() LEDboard=tkinter.Label(calculator,textvariable=content,bg='#efefef',width=50,height=1)#假装是LED板 LEDboard.place(x=20,y=100)#放置LED板 def one(): if"="in content.get(): content.set('1') else: content.set(content.get()+'1') b1 = tkinter.Button(calculator,text='1',bg='white',width=5,height=2,command=one) b1.place(x=100,y=200)

def two(): if"="in content.get(): content.set('2') else: content.set(content.get()+'2')

b2 = tkinter.Button(calculator, text='2', bg='white', width=5, height=2, command=two) b2.place(x=150, y=200)

def three(): if"="in content.get(): content.set('3') else: content.set(content.get()+'3')

b3 = tkinter.Button(calculator, text='3', bg='white', width=5, height=2, command=three) b3.place(x=200, y=200)

def four(): if"="in content.get(): content.set('1') else: content.set(content.get()+'4')

b4 = tkinter.Button(calculator, text='4', bg='white', width=5, height=2, command=four) b4.place(x=100, y=250)

def five(): if"="in content.get(): content.set('5') else: content.set(content.get()+'5')

b5 = tkinter.Button(calculator, text='5', bg='white', width=5, height=2, command=five) b5.place(x=150, y=250)

def six(): if"="in content.get(): content.set('6') else: content.set(content.get()+'6')

b6 = tkinter.Button(calculator, text='6', bg='white', width=5, height=2, command=six) b6.place(x=200, y=250)

def seven(): if"="in content.get(): content.set('1') else: content.set(content.get()+'8')

b7 = tkinter.Button(calculator, text='7', bg='white', width=5, height=2, command=seven) b7.place(x=100, y=300)

def eight(): if"="in content.get(): content.set('8') else: content.set(content.get()+'8')

b8 = tkinter.Button(calculator, text='8', bg='white', width=5, height=2, command=eight) b8.place(x=150, y=300)

def nine(): if"="in content.get(): content.set('9') else: content.set(content.get()+'9')

b9 = tkinter.Button(calculator, text='9', bg='white', width=5, height=2, command=nine) b9.place(x=200, y=300) def zero(): if"="in content.get(): content.set('9') else: content.set(content.get()+'0')

b0 = tkinter.Button(calculator, text='0', bg='white', width=5, height=2, command=zero) b0.place(x=100, y=350)

def dot(): if"="in content.get(): content.set('.') else: content.set(content.get()+'.')

dian = tkinter.Button(calculator, text='.', bg='white', width=5, height=2, command=dot) dian.place(x=150, y=350)
''' 10个数字和点,按钮大小和位置是作者亲测的,请勿修改哦~ ''' def add(): if"="in content.get(): content.set('+') else: content.set(content.get()+'+')

plus = tkinter.Button(calculator, text='+', bg='white', width=5, height=2,command=add) plus.place(x=250,y=200) # https://www.bilibili.com/video/BV1wJ411J7JK/?spm_id_from=333.788.videocard.28
def subtract(): if"="in content.get(): content.set('-') else: content.set(content.get()+'-')

minus= tkinter.Button(calculator, text='-', bg='white', width=5, height=2, command=subtract) minus.place(x=250, y=250) def multiple(): if"="in content.get(): content.set('×') else: content.set(content.get()+'×')

times= tkinter.Button(calculator, text='×', bg='white', width=5, height=2, command=multiple) times.place(x=300, y=250)
def divide(): if"="in content.get(): content.set('÷') else: content.set(content.get()+'÷') chu= tkinter.Button(calculator, text='÷', bg='white', width=5, height=2, command=divide) chu.place(x=250, y=350) #四则运算

点赞0


评论


沉著的飞叶龙uZWJ沉著的飞叶龙uZWJ

<pre class="language-python"><code> def equal():

 

content.set(content.get()+'=') try: value=content.get( )+str(eval(content.get().replace("factorial",'math.factorial').replace('×','*').replace('÷','/') .replace('sqrt','math.sqrt').replace('^', '**').replace('π',str(math.pi)). replace('e',str(math.e)).replace('sinr','math.sin').replace('cosr','math.cos').replace('tanr','math.tan') .replace("sind","math.sin(math.radians").replace("cosd","math.cos(math.radians") .replace("tand","math.tan(math.radians").replace("asin",'math.asin').replace("acos",'math.acos') .replace("atan",'math.atan').replace('=',''))) if'.'==str(value)[-2]and'0'==str(value)[-1]: content.set(str(value).split('.0')[0]) else: content.set(value) except ZeroDivisionError: easygui.msgbox('不能除以零!') content.set('') except: easygui.msgbox('输入有误!') content.set('')



dengyu = tkinter.Button(calculator, text='=', bg='white', width=5, height=2, command=equal) dengyu.place(x=200, y=350) #等于号

 

def chengfang(): if"="in content.get(): content.set('^') else: content.set(content.get()+'^') mi= tkinter.Button(calculator, text='^', bg='white', width=5, height=2, command=chengfang)#乘方运算 mi.place(x=300, y=200) def zuokuohao(): if"="in content.get(): content.set('(') else: content.set(content.get()+'(') leftkuohao= tkinter.Button(calculator, text='(', bg='white', width=5, height=2, command=zuokuohao) leftkuohao.place(x=250,y=300) def youkuohao(): if"="in content.get(): content.set(')') else: content.set(content.get()+')') rightkuohao= tkinter.Button(calculator, text=')', bg='white', width=5, height=2, command=youkuohao) rightkuohao.place(x=300,y=300) #两个括号 def backspace(): if"="in content.get(): content.set('') else: content.set(''.join(list(content.get()[0:len(content.get())-1]))) huishan=tkinter.Button(calculator, text=chr(9003), bg='white', width=5, height=2, command=backspace)#回删 huishan.place(x=300,y=350) def pai(): if"="in content.get(): content.set('π') else: content.set(content.get()+'π') yuanzhoulv=tkinter.Button(calculator, text='π', bg='white', width=5, height=2, command=pai)#圆周率 yuanzhoulv.place(x=50,y=200) def e(): if"="in content.get(): content.set('e') else: content.set(content.get()+'e') # e E=tkinter.Button(calculator, text='e', bg='white', width=5, height=2, command=e) E.place(x=50,y=250) # https://www.bilibili.com/video/BV1qx411R7TA/?spm_id_from=333.788.videocard.0



def all_clear():

 

content.set('') C=tkinter.Button(calculator,text='C',bg='white',width=5,height=2,command=all_clear) C.place(x=0,y=200)#全部清空



def sqr():

 

try: content.set((str(eval(str(content.get()).replace('factorial','math.factorial').replace('×', '*').replace('÷', '/').replace('sqrt', 'math.sqrt').replace('^', '**').replace('π',str(math.pi)). replace('e',str(math.e)).replace('sinr','math.sin').replace('cosr','math.cos').replace('tanr','math.tan') .replace("sind","math.sin(math.radians").replace("cosd","math.cos(math.radians") .replace("tand","math.tan(math.radians").replace("asin",'math.asin').replace("acos",'math.acos') .replace("atan",'math.atan'))**2))) except OverflowError: easygui.msgbox('太大了!!!') content.set('') except: easygui.msgbox('输入有误!')



pingfang = tkinter.Button( calculator, text='x²', bg='white', width=5, height=2, command=sqr) pingfang.place(x=50, y=350) # 快捷平方运算



def sqrt():

 

try: content.set('sqrt('+str(eval(str(content.get()).replace('factorial','math.factorial').replace('×', '*').replace('÷', '/').replace('sqrt','math.sqrt').replace('^', '**').replace('π',str(math.pi)) .replace('e',str(math.e)).replace('sinr','math.sin').replace('cosr','math.cos').replace('tanr','math.tan').replace("sind","math.sin(math.radians").replace("cosd","math.cos(math.radians") .replace("tand","math.tan(math.radians").replace("asin",'math.asin').replace("acos",'math.acos').replace("atan",'math.atan')))+")") except ValueError: easygui.msgbox('负数不能开平方根!') except: easygui.msgbox('输入有误!')



pingfanggen = tkinter.Button( calculator, text='√x', bg='white', width=5, height=2, command=sqrt) pingfanggen.place(x=50, y=300) # 快捷平方根运算 def factorial():

 

try: content.set(('factorial('+str((eval(content.get().replace('factorial','math.factorial').replace('×', '*').replace('sqrt','math.sqrt').replace('÷', '/').replace('^', '**').replace('π',str(math.pi)). replace('e',str(math.e)).replace('sinr','math.sin').replace('cosr','math.cos').replace('tanr','math.tan').replace("sind","math.sin(math.radians").replace("cosd","math.cos(math.radians") .replace("tand","math.tan(math.radians").replace("asin",'math.asin').replace("acos",'math.acos').replace("atan",'math.atan')))))+")") except ValueError: easygui.msgbox('只有整数能够阶乘!') except OverflowError: easygui.msgbox('太大了!!!') content.set('') except: easygui.msgbox('输入有误!')



jiecheng = tkinter.Button( calculator, text='x!', bg='white', width=5, height=2, command=factorial) jiecheng.place(x=0, y=250) # 快捷阶乘运算

 

点赞0


评论


沉著的飞叶龙uZWJ沉著的飞叶龙uZWJ

<pre class="language-python"><code>


def sin_hudu():
try: content.set(('sinr('+str((eval(content.get().replace('factorial','math.factorial').replace('×', '*').replace('sqrt','math.sqrt').replace('÷', '/').replace('^', '**').replace('π',str(math.pi)). replace('e',str(math.e)).replace('sinr','math.sin').replace('cosr','math.cos').replace('tanr','math.tan').replace("sind","math.sin(math.radians").replace("cosd","math.cos(math.radians") .replace("tand","math.tan(math.radians").replace("asin",'math.asin').replace("acos",'math.acos').replace("atan",'math.atan')))))+")") except: easygui.msgbox('输入有误!')

sinrad = tkinter.Button( calculator, text='sinr', bg='white', width=5, height=2, command=sin_hudu) sinrad.place(x=350,y=0)
def cos_hudu(): try: content.set(('cosr('+str((eval(content.get().replace('factorial','math.factorial').replace('×', '*').replace('sqrt','math.sqrt').replace('÷', '/').replace('^', '**').replace('π',str(math.pi)). replace('e',str(math.e)).replace('sinr','math.sin').replace('cosr','math.cos').replace('tanr','math.tan').replace("sind","math.sin(math.radians").replace("cosd","math.cos(math.radians") .replace("tand","math.tan(math.radians").replace("asin",'math.asin').replace("acos",'math.acos').replace("atan",'math.atan')))))+")") except: easygui.msgbox('输入有误!')

cosrad = tkinter.Button( calculator, text='cosr', bg='white', width=5, height=2, command=cos_hudu) cosrad.place(x=350,y=50) def tan_hudu(): try: content.set(('tanr('+str((eval(content.get().replace('factorial','math.factorial').replace('×', '*').replace('sqrt','math.sqrt').replace('÷', '/').replace('^', '**').replace('π',str(math.pi)). replace('e',str(math.e)).replace('sinr','math.sin').replace('cosr','math.cos').replace('tanr','math.tan').replace("sind","math.sin(math.radians").replace("cosd","math.cos(math.radians") .replace("tand","math.tan(math.radians").replace("asin",'math.asin').replace("acos",'math.acos').replace("atan",'math.atan')))))+")") except: easygui.msgbox('输入有误!')

tanrad = tkinter.Button( calculator, text='tanr', bg='white', width=5, height=2, command=tan_hudu) tanrad.place(x=350,y=100) """三角函数弧度运算""" def sin_jiaodu(): try: content.set(('sind('+str((eval(content.get().replace('factorial','math.factorial').replace('×', '*').replace('sqrt','math.sqrt').replace('÷', '/').replace('^', '**').replace('π',str(math.pi)). replace('e',str(math.e)).replace('sinr','math.sin').replace('cosr','math.cos').replace('tanr','math.tan').replace("sind","math.sin(math.radians").replace("cosd","math.cos(math.radians") .replace("tand","math.tan(math.radians").replace("asin",'math.asin').replace("acos",'math.acos'). replace("atan",'math.atan')))))+"))") except:# 就是两个括号↑,其余同 easygui.msgbox('输入有误!')

sindeg = tkinter.Button( calculator, text='sind', bg='white', width=5, height=2, command=sin_jiaodu) sindeg.place(x=350,y=150) def cos_jiaodu(): try: content.set(('cosd('+str((eval(content.get().replace('factorial','math.factorial').replace('×', '*').replace('sqrt','math.sqrt').replace('÷', '/').replace('^', '**').replace('π',str(math.pi)). replace('e',str(math.e)).replace('sinr','math.sin').replace('cosr','math.cos').replace('tanr','math.tan').replace("sind","math.sin(math.radians").replace("cosd","math.cos(math.radians") .replace("tand","math.tan(math.radians").replace("asin",'math.asin').replace("acos",'math.acos').replace("atan",'math.atan'))))+"))")) except: easygui.msgbox('输入有误!')

cosdeg = tkinter.Button( calculator, text='cosd', bg='white', width=5, height=2, command=cos_jiaodu) cosdeg.place(x=350,y=200)

def tan_jiaodu():
try: content.set(('tand('+str((eval(content.get().replace('factorial','math.factorial').replace('×', '*').replace('sqrt','math.sqrt').replace('÷', '/').replace('^', '**').replace('π',str(math.pi)). replace('e',str(math.e)).replace('sinr','math.sin').replace('cosr','math.cos').replace('tanr','math.tan').replace("sind","math.sin(math.radians").replace("cosd","math.cos(math.radians") .replace("tand","math.tan(math.radians").replace("asin",'math.asin').replace("acos",'math.acos').replace("atan",'math.atan')))))+"))") except: easygui.msgbox('输入有误!')

tandeg = tkinter.Button( calculator, text='tand', bg='white', width=5, height=2, command=tan_jiaodu) tandeg.place(x=350,y=250) """三角函数角度运算""" def asin():
#try: content.set(('asin('+str((eval(content.get().replace('factorial','math.factorial').replace('×', '*').replace('sqrt','math.sqrt').replace('÷', '/').replace('^', '**').replace('π',str(math.pi)). replace('e',str(math.e)).replace('sinr','math.sin').replace('cosr','math.cos').replace('tanr','math.tan').replace("sind","math.sin(math.radians").replace("cosd","math.cos(math.radians") .replace("tand","math.tan(math.radians").replace("asin",'math.asin').replace("acos",'math.acos').replace("atan",'math.atan')))))+")") # except ValueError: # easygui.msgbox('asin只能填-1到1的数字!') # content.set('') # except: # easygui.msgbox('输入有误!')

arcsin = tkinter.Button( calculator, text='asin', bg='white', width=5, height=2, command=asin) arcsin.place(x=350,y=300) def acos():
try: content.set(('acos('+str((eval(content.get().replace('factorial','math.factorial').replace('×', '*').replace('sqrt','math.sqrt').replace('÷', '/').replace('^', '**').replace('π',str(math.pi)). replace('e',str(math.e)).replace('sinr','math.sin').replace('cosr','math.cos').replace('tanr','math.tan').replace("sind","math.sin(math.radians").replace("cosd","math.cos(math.radians") .replace("tand","math.tan(math.radians").replace("asin",'math.asin').replace("acos",'math.acos').replace("atan",'math.atan')))))+")") except ValueError: easygui.msgbox('acos只能填-1到1的数字!') content.set('') except: easygui.msgbox('输入有误!')

arccos = tkinter.Button( calculator, text='acos', bg='white', width=5, height=2, command=acos) arccos.place(x=350,y=350)
def atan():
try: content.set(('atan('+str((eval(content.get().replace('factorial','math.factorial').replace('×', '*').replace('sqrt','math.sqrt').replace('÷', '/').replace('^', '**').replace('π',str(math.pi)). replace('e',str(math.e)).replace('sinr','math.sin').replace('cosr','math.cos').replace('tanr','math.tan').replace("sind","math.sin(math.radians").replace("cosd","math.cos(math.radians") .replace("tand","math.tan(math.radians").replace("asin",'math.asin').replace("acos",'math.acos').replace("atan",'math.atan')))))+")")
except: easygui.msgbox('输入有误!')

arctan = tkinter.Button( calculator, text='atan', bg='white', width=5, height=2, command=atan) arctan.place(x=0,y=300) """反三角函数运算""" def exit(): quit() tuichu= tkinter.Button( calculator, text='退出', bg='white', width=5, height=2, command=exit) tuichu.place(x=0,y=350) calculator.mainloop() # 运行计算器

点赞0


评论


沉著的飞叶龙uZWJ沉著的飞叶龙uZWJ

从下至上看

点赞0


评论


NirvanaOvONirvanaOvO

考我自己的古

点赞0


评论


NirvanaOvONirvanaOvO

偶也

点赞0


评论


NirvanaOvONirvanaOvO

考古

点赞0


评论


NirvanaOvONirvanaOvO

一个月前,我应该是编程猫第一个发的吧

点赞0


评论