用Python写一个简单的计算器

import tkinter as a
root = a.Tk()  #创建一个主窗口,用于容纳整个GUI程序
root.minsize(300,511)
root.title('欢迎您使用本计算器')
#result为结果显示区域,result2为运算过程显示区域,anchor 指示文字会被放在控件的什么位置, 此为放到右下
result = a.StringVar()
result.set('') 
result2 = a.StringVar()
result2.set('')
label = a.Label(root,font = ('//Library//Fonts//Hanzipen.ttc',22),bg='black',bd='9',fg='white',anchor='se',textvariable=result2)
label.place(width = 300,height = 73)
label2 = a.Label(root,font = ('//Library//Fonts//Hanzipen.ttc',22),bg='black',bd='9',fg='white',anchor='se',textvariable=result)
label2.place(y = 73,width = 300,height = 73)
#以下为按钮区域
#页面第一行按钮
buttonAC = a.Button(root,text = 'AC',font = ('//Library//Fonts//Hanzipen.ttc',22),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressCompute('AC'))
buttonAC.place(x = 0,y = 146,width = 75,height = 73)
buttonDel = a.Button(root,text = 'DEL',font = ('//Library//Fonts//Hanzipen.ttc',22),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressCompute('del'))
buttonDel.place(x = 75,y = 146,width = 75,height = 73)
buttonYu= a.Button(root,text = '%',font = ('//Library//Fonts//Hanzipen.ttc',22),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressCompute('%'))
buttonYu.place(x = 150,y = 146,width = 75,height = 73)
buttonChu= a.Button(root,text='÷',bg = 'orange',font = ('//Library//Fonts//Hanzipen.ttc',22),highlightbackground="#FF7F24",bd='0.5',fg='#4F4F4F',command = lambda : pressCompute('/'))
buttonChu.place(x = 225,y = 146,width = 75,height = 73)
#页面第二行按钮
button7 = a.Button(root,text = '7',font = ('//Library//Fonts//Hanzipen.ttc',22),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('7'))
button7.place(x = 0,y = 219,width = 75,height = 73)
button8 = a.Button(root,text = '8',font = ('//Library//Fonts//Hanzipen.ttc',22),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('8'))
button8.place(x = 75,y = 219,width = 75,height = 73)
button9 = a.Button(root,text = '9',font = ('//Library//Fonts//Hanzipen.ttc',22),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('9'))
button9.place(x = 150,y = 219,width = 75,height = 73)
buttonCheng= a.Button(root,text = '×',bg = 'orange',font = ('//Library//Fonts//Hanzipen.ttc',22),highlightbackground="#FF7F24",fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressCompute('*'))
buttonCheng.place(x = 225,y = 219,width = 75,height = 73)
#页面第三行按钮
button4 = a.Button(root,text = '4',font = ('//Library//Fonts//Hanzipen.ttc',22),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('4'))
button4.place(x = 0,y = 292,width = 75,height = 73)
button5 = a.Button(root,text = '5',font = ('//Library//Fonts//Hanzipen.ttc',22),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('5'))
button5.place(x = 75,y = 292,width = 75,height = 73)
button6 = a.Button(root,text = '6',font = ('//Library//Fonts//Hanzipen.ttc',22),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('6'))
button6.place(x = 150,y = 292,width = 75,height = 73)
buttonJian= a.Button(root,text = '-',bg = 'orange',font = ('//Library//Fonts//Hanzipen.ttc',22),highlightbackground="#FF7F24",fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressCompute('-'))
buttonJian.place(x = 225,y = 292,width = 75,height = 73)
#页面第四行按钮
button1 = a.Button(root,text = '1',font = ('//Library//Fonts//Hanzipen.ttc',22),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('1'))
button1.place(x = 0,y = 365,width = 75,height = 73)
button2 = a.Button(root,text = '2',font = ('//Library//Fonts//Hanzipen.ttc',22),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('2'))
button2.place(x = 75,y = 365,width = 75,height = 73)
button3 = a.Button(root,text = '3',font = ('//Library//Fonts//Hanzipen.ttc',22),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('3'))
button3.place(x = 150,y = 365,width = 75,height = 73)
buttonJia= a.Button(root,text = '+',bg = 'orange',font = ('//Library//Fonts//Hanzipen.ttc',22),highlightbackground="#FF7F24",fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressCompute('+'))
buttonJia.place(x = 225,y = 365,width = 75,height = 73)
#页面第五行按钮
button0 = a.Button(root,text = '0',font = ('//Library//Fonts//Hanzipen.ttc',22),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('0'))
button0.place(x = 0,y = 438,width = 150,height = 73)
buttonDian= a.Button(root,text = '.',font = ('//Library//Fonts//Hanzipen.ttc',22),fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressNum('.'))
buttonDian.place(x = 150,y = 438,width = 75,height = 73)
buttonDeng= a.Button(root,text = '=',bg = 'orange',font = ('//Library//Fonts//Hanzipen.ttc',22),highlightbackground="#FF7F24",fg = ('#4F4F4F'),bd = 0.5,command = lambda : pressEqual())
buttonDeng.place(x = 225,y = 438,width = 75,height = 73)
#以下为函数
lists = [] #用来保存运算数值
IsPress1 = False  #按下运算符的变量
IsPress2 = False #按下数字键的变量
flag = 0 #用来检测‘-’是不是第一个按下
def pressNum(num):   
     global lists
     global IsPress1
     global IsPress2
     global flag
     oldnum=result.get()
     if oldnum == '0':
          result.set(num)
     else:
          newnum = oldnum + num  #将字符串连接起来
          result.set(newnum)
     IsPress2 = True
#运算函数
def pressCompute(sign):
     global lists
     global IsPress1
     global flag
     num2 = result.get()  
     if flag==1:
          num2 = '-'+num2 
     elif IsPress2 == False and sign == '-' :   #第一次按下'-'键
          flag+=1
          return
     elif sign=='del':
          a=num2[0:-1]
          lists.clear()
          result.set(a)
          return
     lists.append(num2)  #加到lists列表中
     lists.append(sign)
     IsPress1 == True
     if sign =='AC':
          lists.clear()
          result.set(0)
          result2.set('')
          flag=0
     result.set('')
#运算结果显示函数
def pressEqual():
     global lists
     global IsPress1
     global flag
     nownum = result.get()  #获取当前数字变量
     lists.append(nownum)
     computrStr = ''.join(lists)  #讲列表内容用join命令给字符串链接起来
     try:
          endNum = eval(computrStr)   #endNum为浮点数
     except:
          result.set('Syntax ERROR!') #我的得力科学计算器出错了是这样显示的        
     #当数字大于14位时候显示不完全
     if endNum>9999999999:
          result2.set('')
          result.set('Syntax ERROR!')
          return
     else:
          result.set(round(endNum,6))  #endNum保留六位小数
     result2.set(computrStr) #result2显示运算过程在
     flag=0
root.mainloop()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值