本文主要使用python tkinter下的Gride编写一个计算器(标准和科学计算器),自测没有发现问题,如果有哪位网友发现bug可以给我留言或私信,我来修改。代码如下:
import tkinter
from tkinter import *
from tkinter import messagebox,ttk
win = Tk()
win.title('Grid 科学计算器')
c=str()
def add(i):
global c
a = ['*', '/', '-', '+', '.']
if mode.get()=='科学':
if str(c) in a and i in a: #判断本次输入和前一次输入的是非数字,删除前一次输入的非数字
qq = res.get()[:-1]
res.set(qq)
# elif str(c) in a and i==0:
# print('00',res.get())
# res.set(res.get())
if res.get(): #重新把输入的i复制到后面展示
qq = str(res.get())
qq1 = qq + str(i)
# print(len(qq1))
if len(qq1) >=3:
# print(qq1[-3],type(qq1[-2]))
if qq1[-3] in ['*', '/', '-', '+'] and qq1[-2] == '0' and qq1[-1] not in '.': #eval无法识别x+0xxxx或0x+x
qq = str(res.get()[:-1]) + str(i)
res.set(qq)
print('qq', qq)
else:
res.set(qq1)
else:
res.set(qq1)
else:
res.set(i)
else: #mode='标准'
if str(c) in a and i in a: # 判断本次输入和前一次输入的是非数字,删除前一次输入的非数字
qq = res.get()[:-1]
res.set(qq)
elif str(i) in ['*', '/', '-', '+'] and res.get()[:-1]:
print(res.get())
qq=str(round(eval(res.get()),10))
res.set(qq)
if res.get(): # 重新把输入的i复制到后面展示
qq = str(res.get())
qq1 = qq + str(i)
# print(len(qq1))
if len(qq1) >= 3:
# print(qq1[-3],type(qq1[-2]))
if qq1[-3] in ['*', '/', '-', '+'] and qq1[-2] == '0' and qq1[-1] not in '.': # eval无法识别x+0xxxx或0x+x
qq = str(res.get()[:-1]) + str(i)
res.set(qq)
print('qq', qq)
else:
res.set(qq1)
else:
res.set(qq1)
else:
res.set(i)
c=i
def calc():
try:
if res.get()[0]=='0':
if res.get()[1] in ['*', '/', '-', '+', '.']: #eval无法识别x+0xxxx或0x+x
res.set(round(eval(res.get()),10))
else:
print('fail',res.get()[1:])
res.set(eval(res.get()[1:]))
else:
print('daol')
res.set(round(eval(res.get()),10))
except:
# print(res.get())
if res.get():
tkinter.messagebox.showerror('message info','在瞎输入,40m长的大刀等着你')
else:
res.set('')
print('empty')
pass
def AC(): #AC为了给按钮用,AC1给bind用,一个要带参数
global c
c='0'
res.set('')
def AC1(self):
res.set('')
def dele():
# global c
i=res.get()
# try:
# c=i[-2]
# except:
# print('可忽略')
i=i[:-1]
res.set(i)
# for i in range(1,10):
# # i-1表示从第一行开始,+2(-1+3)表示从第二行开始,这相法比较666,缺点是点击任意一个按钮都只能调用一个函数,且i永远是最后一个数字
# #目前没有找到解决办法,故废弃,用土鳖办法,这段代码可以忽略,如果有解决方法也可以留言
# Li = tkinter.Button(win, text=str(i), width=10,command=add).grid(row=(i +2) // 3, column=(i +2) % 3)
Button(win, text="AC",command=AC,width =10,fg='red').grid(row=1, column=0, sticky=W)
Button(win, text="Del",command=dele,width =10).grid(row=1, column=1, sticky=W)
mode=ttk.Combobox(win,values=["科学","标准"],width =8,state='readonly',background='red')
mode.grid(row=1, column=2, columnspan=1, sticky=W)
mode.current(0)
mode.bind('<<ComboboxSelected>>',AC1)
Button(win, text="/",command=lambda :add('/'),width =10).grid(row=1, column=3, sticky=W)
Button(win, text="*",command=lambda :add('*'),width =10).grid(row=4, column=3, sticky=W)
Button(win, text="0",command=lambda :add(0),width =10).grid(row=5, column=0, columnspan=2,sticky=E+W)
Button(win, text=".",command=lambda :add('.'),width =10).grid(row=5, column=2, sticky=W)
Button(win, text="1",command=lambda:add(1),width =10,).grid(row=2, column=0, sticky=W)
Button(win, text="2",command=lambda :add(2),width =10).grid(row=2, column=1, sticky=W) #,state=DISABLED--禁用按钮状态
Button(win, text="3",command=lambda :add(3),width =10).grid(row=2, column=2, sticky=W)
Button(win, text="4",command=lambda :add(4),width =10).grid(row=3, column=0, sticky=W)
Button(win, text="5",command=lambda :add(5),width =10).grid(row=3, column=1, sticky=W)
Button(win, text="6",command=lambda :add(6),width =10).grid(row=3, column=2, sticky=W) #,state=DISABLED--禁用按钮状态
Button(win, text="7",command=lambda :add(7),width =10).grid(row=4, column=0, sticky=W)
Button(win, text="8",command=lambda :add(8),width =10).grid(row=4, column=1, sticky=W)
Button(win, text="9",command=lambda :add(9),width =10).grid(row=4, column=2, sticky=W)
Button(win, text="+",command=lambda :add('+'),width =10).grid(row=2, column=3, sticky=W)
Button(win, text="-",command=lambda :add("-"),width =10).grid(row=3, column=3, sticky=W)
Button(win, text="=",command=calc,bg='blue',height =1,width =10).grid(row=5, column=3, sticky=W)
win.mainloop()
PS:通过下拉框切换标准或科学输入法,来解决先算输入运行还是统一从左到右进行计算。目前也支持复制粘贴数据进行继续(按从左到右计算)
版权声明:本文为mongo2020原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。