|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 gdmao002 于 2020-11-25 09:20 编辑
以下是练手的简单计算器,未经过优化。写的不好海涵
- import tkinter
- import re
- def del_zero(*args):
- global num
- c()
- r = re.match(r"(\d+\.?\d*)([/*+-]{0,2})(\d*?\.?\d*)(=?)$", num)
- if r:
- if r.group(1) != '': # 第1个单元有值2个字符00-09判断
- if len(num) == 2:
- if len(r.group(1)) == 2:
- if r.group(1)[0] == '0' and r.group(1)[1].isdigit():
- num = r.group(1)[1]
- if r.group(2) != '': # 第2个单元满足不为空的时候
- if '.' == r.group(1)[-1]: # 判断第1个单元如果有1'.'在后的处理
- num = num[:-1]
- elif len(r.group(1)) >= 3: # 判断第1个单元如果有1'.'在中间 对于多'0'处理
- if r.group(1)[0] == '0' and r.group(1)[1] == '.':
- # 处理.后一堆0 # 剩余的0个数
- for i in range(2, len(r.group(1))):
- if r.group(1)[i] != '0':
- break
- else:
- num = '0' + r.group(2)
- if len(r.group(2)) == 2: # 判断运算符号个数
- num = r.group(1) + r.group(2)[-1]
- if r.group(3) != '': # 判断第3个单元不为空的输入
- if len(r.group(3)) == 1: # 第3个单元有值2个字符00-09判断
- if not r.group(3)[0].isdigit():
- num = num[:-1]
- elif len(r.group(3)) == 2:
- if r.group(3)[0] == '0' and r.group(3)[1].isdigit():
- num = r.group(1) + r.group(2) + r.group(3)[1]
- elif len(r.group(3)) >= 3:
- if r.group(3)[-1] == '.' and r.group(3)[-2] == '.': # 处理第三单元.的个数
- num = num[:-1]
- if r.group(4) == '=': # 第4个单元为'='时
- if r.group(3) != '':
- if r.group(3)[-1] == '.': # 判断第3个单元.在最后的处理
- num = num[:-1]
- elif len(r.group(3)) >= 3: # 判断第3个单元如果出现0.00000这类的处理
- if r.group(3)[0] == '0' and r.group(3)[1] == '.':
- for i in range(2, len(r.group(3))): # 处理.后一堆0 # 剩余的0个数
- if r.group(3)[i] != '0':
- break
- else:
- num = r.group(1) + r.group(2) + '0'
- if r.group(2) != '' and r.group(3)[-1].isdigit(): # 如果=号左边为数字 开始结算
- cal_1()
- else:
- num = num[:-1] # 第3单元空 第2单元空
- v.set(num)
- else:
- num = num[:-1]
- v.set(num)
- def fun0():
- global num
- c()
- num += '0'
- del_zero()
- def fun1():
- global num
- c()
- num += '1'
- del_zero()
- def fun2():
- global num
- c()
- num += '2'
- del_zero()
- def fun3():
- global num
- c()
- num += '3'
- del_zero()
- def fun4():
- global num
- c()
- num += '4'
- del_zero()
- def fun5():
- global num
- c()
- num += '5'
- del_zero()
- def fun6():
- global num
- c()
- num += '6'
- del_zero()
- def fun7():
- global num
- c()
- num += '7'
- del_zero()
- def fun8():
- global num
- c()
- num += '8'
- del_zero()
- def fun9():
- global num
- c()
- num += '9'
- del_zero()
- def fun():
- global num
- c()
- num += '.'
- del_zero()
- def fun_add():
- global num
- c()
- num += '+'
- del_zero()
- def fun_sub():
- global num
- c()
- num += '-'
- del_zero()
- def fun_chen():
- global num
- c()
- num += '*'
- del_zero()
- def fun_chu():
- global num
- c()
- num += r'/'
- del_zero()
- def fun_del():
- global num
- c()
- if num != '':
- num = num[:-1]
- v.set(num)
- def fun_c():
- global num
- c()
- if num != '':
- num = ''
- v.set(num)
- def fun_ce():
- global num
- c()
- if num == '':
- v.set(num)
- else:
- r = re.match(r"(\d+\.?\d*)([/*+-]{0,2})(\d*?\.?\d*)(=?)$", num)
- if r:
- if r.group(3) != '':
- num = r.group(1) + r.group(2)
- v.set(num)
- # 状态函数 负责清空结算
- def c():
- global num, count
- if count == 1:
- num = ''
- count = 0
- def cal_1():
- global num, count
- r = re.match(r"(\d+\.?\d*)([/*+-]{0,2})(\d*?\.?\d*)(=?)$", num)
- sum1 = 0
- if '.' in r.group(1):
- x = float(r.group(1))
- else:
- x = int(r.group(1))
- if '.' in r.group(3):
- y = float(r.group(3))
- else:
- y = int(r.group(3))
- if '+' == r.group(2):
- sum1 = x + y
- elif '-' == r.group(2):
- sum1 = x - y
- elif '*' == r.group(2):
- sum1 = x * y
- elif '/' == r.group(2):
- if r.group(3) == '0':
- num = r.group(1) + r.group(2)
- return -1
- else:
- sum1 = x / y
- num += str(sum1)
- count = 1
- def cal():
- global num
- num += '='
- del_zero()
- def callback(event):
- if event.char == '0':
- fun0()
- elif event.char == '1':
- fun1()
- elif event.char == '2':
- fun2()
- elif event.char == '3':
- fun3()
- elif event.char == '4':
- fun4()
- elif event.char == '5':
- fun5()
- elif event.char == '6':
- fun6()
- elif event.char == '7':
- fun7()
- elif event.char == '8':
- fun8()
- elif event.char == '9':
- fun9()
- elif event.char == '.':
- fun()
- elif event.char == '+':
- fun_add()
- elif event.char == '-':
- fun_sub()
- elif event.char == '*':
- fun_chen()
- elif event.char == r'/':
- fun_chu()
- elif event.char == '\r':
- cal()
- else:
- pass
- root = tkinter.Tk()
- root.title('简易计算器')
- root.resizable(0, 0)
- frame2 = tkinter.Frame(root)
- frame2.grid(row=0, column=1, padx=5, pady=5)
- num = ''
- count = 0
- v = tkinter.StringVar()
- label2 = tkinter.Label(frame2, textvariable=v).grid(row=0, column=1, padx=5, pady=5)
- frame1 = tkinter.Frame(root)
- frame1.grid(row=1, column=1, padx=5, pady=5)
- root.bind('<Key>', callback)
- root.focus_set()
- tkinter.Button(frame1, text='←', width=4, command=fun_del).grid(row=2, column=0, padx=2, pady=5)
- tkinter.Button(frame1, text='C', width=4, command=fun_c).grid(row=2, column=1, padx=2, pady=5)
- tkinter.Button(frame1, text='CE', width=4, command=fun_ce).grid(row=2, column=2, padx=2, pady=5)
- tkinter.Button(frame1, text='/', width=4, command=fun_chu).grid(row=2, column=3, padx=5, pady=5)
- tkinter.Button(frame1, text='7', width=4, command=fun7).grid(row=3, column=0, padx=5, pady=2)
- tkinter.Button(frame1, text='8', width=4, command=fun8).grid(row=3, column=1, padx=2, pady=2)
- tkinter.Button(frame1, text='9', width=4, command=fun9).grid(row=3, column=2, padx=2, pady=2)
- tkinter.Button(frame1, text='*', width=4, command=fun_chen).grid(row=3, column=3, padx=5, pady=2)
- tkinter.Button(frame1, text='4', width=4, command=fun4).grid(row=4, column=0, padx=5, pady=2)
- tkinter.Button(frame1, text='5', width=4, command=fun5).grid(row=4, column=1, padx=2, pady=2)
- tkinter.Button(frame1, text='6', width=4, command=fun6).grid(row=4, column=2, padx=2, pady=2)
- tkinter.Button(frame1, text='-', width=4, command=fun_sub).grid(row=4, column=3, padx=5, pady=2)
- tkinter.Button(frame1, text='1', width=4, command=fun1).grid(row=5, column=0, padx=5, pady=2)
- tkinter.Button(frame1, text='2', width=4, command=fun2).grid(row=5, column=1, padx=2, pady=2)
- tkinter.Button(frame1, text='3', width=4, command=fun3).grid(row=5, column=2, padx=2, pady=2)
- tkinter.Button(frame1, text='+', width=4, command=fun_add).grid(row=5, column=3, padx=5, pady=2)
- tkinter.Button(frame1, text='0', width=4, command=fun0).grid(row=6, column=0, padx=5, pady=5)
- tkinter.Button(frame1, text='.', width=4, command=fun).grid(row=6, column=1, padx=2, pady=5)
- tkinter.Button(frame1, text='=', width=10, command=cal).grid(row=6, column=2, padx=5, pady=5, columnspan=2)
- root.mainloop()
复制代码 |
|