import tkinter
class niubijisuanq:
def __init__(self):
self.root = tkinter.Tk()
self.root.title('某某的计算器')
self.root.minsize(300, 420)
self.pinmu = tkinter.StringVar()
self.pinmu.set('0')
#创建一个列表
self.dbg = []
#创建个变量
self.ama = False
#面板调用
self.minaban()
self.root.mainloop()
#按键
def anjian(self,num):
if self.ama == True:
self.pinmu.set('0')
self.ama = False
if self.pinmu.get() == '0':
self.pinmu.set(num)
if self.pinmu.get() =='.':
self.pinmu.set('0'+num)
else:
if self.pinmu.get().count('.')==0:
self.pinmu.set(self.pinmu.get()+num)
elif num != '.':
self.pinmu.set(self.pinmu.get()+num)
#符号
def fuhao(self,xin):
xinzhi = self.pinmu.get()
self.dbg.append(xinzhi)
self.dbg.append(xin)
print(self.dbg)
self.ama = True
#等于
def denghao(self):
try:
xinzhi = self.pinmu.get()
self.dbg.append(xinzhi)
res = eval(''.join(self.dbg))
print(res)
self.pinmu.set(round(res,8))
self.dbg.clear()
except ZeroDivisionError :
self.pinmu.set('数学是体育是教的')
#删除
def delete(self):
if self.pinmu.get() == '' or self.pinmu.get() == '0':
self.pinmu.set('0')
return
chang = len(self.pinmu.get())
if chang >1:
changdu = self.pinmu.get()
changdu = changdu[0:chang-1]
self.pinmu.set(changdu)
else:
self.pinmu.set('0')
#清空
def qingk(self):
self.dbg = []
self.pinmu.set('0')
self.ama =True
#正负
def zhengfu(self):
huoquzhi = self.pinmu.get()
if huoquzhi[0] == '-':
self.pinmu.set(huoquzhi[1:])
elif huoquzhi[0] != '-' and huoquzhi[0] != '0' :
self.pinmu.set('-'+huoquzhi)
else:
huoquzhi.startswith('0.')
self.pinmu.set('-'+huoquzhi)
def minaban(self):
# 屏幕显示
gj = tkinter.Label(self.root,textvariable =self.pinmu , bg='red', bd=5, anchor='se', font=('宋体', 30))
gj.place(x=10, y=5, width=280, height=60)
gj1 = tkinter.Button(text='1',command = lambda :self.anjian('1'))
gj1.place(x=10, y=140, width=60, height=60)
gj2 = tkinter.Button(text='2', command= lambda :self.anjian('2'))
gj2.place(x=80, y=140, width=60, height=60)
gj3 = tkinter.Button(text='3', command=lambda :self.anjian('3'))
gj3.place(x=150, y=140, width=60, height=60)
gj4 = tkinter.Button(text='4', command= lambda :self.anjian('4'))
gj4.place(x=10, y=210, width=60, height=60)
gj5 = tkinter.Button(text='5', command= lambda :self.anjian('5'))
gj5.place(x=80, y=210, width=60, height=60)
gj6 = tkinter.Button(text='6', command= lambda :self.anjian('6'))
gj6.place(x=150, y=210, width=60, height=60)
gj7 = tkinter.Button(text='7', command= lambda :self.anjian('7'))
gj7.place(x=10, y=280, width=60, height=60)
gj8 = tkinter.Button(text='8', command= lambda :self.anjian('8'))
gj8.place(x=80, y=280, width=60, height=60)
gj9 = tkinter.Button(text='9', command= lambda :self.anjian('9'))
gj9.place(x=150, y=280, width=60, height=60)
gj0 = tkinter.Button(text='0', command= lambda :self.anjian('0'))
gj0.place(x=10, y=350, width=130, height=60)
gjd = tkinter.Button(text='.', command= lambda :self.anjian('.'))
gjd.place(x=150, y=350, width=60, height=60)
gjjia = tkinter.Button(text='-', command= lambda :self.fuhao('-') )
gjjia.place(x=220, y=140, width=60, height=60)
gjjia1 = tkinter.Button(text='*', command= lambda :self.fuhao('*'))
gjjia1.place(x=220, y=210, width=60, height=60)
gjcheng = tkinter.Button(text='/', command= lambda :self.fuhao('/'))
gjcheng.place(x=220, y=280, width=60, height=60)
gjchu1 = tkinter.Button(text='=', command= self.denghao)
gjchu1.place(x=220, y=350, width=60, height=60)
gjchu2 = tkinter.Button(text='←', command=self.delete)
gjchu2.place(x=10, y=70, width=60, height=60)
gjchu3 = tkinter.Button(text='C', command=self.qingk)
gjchu3.place(x=80, y=70, width=60, height=60)
gjchu4 = tkinter.Button(text='±', command=self.zhengfu)
gjchu4.place(x=150, y=70, width=60, height=60)
gjchu5 = tkinter.Button(text='+', command= lambda :self.fuhao('+'))
gjchu5.place(x=220, y=70, width=60, height=60)
gj = niubijisuanq()
对象方法编写计算器
最新推荐文章于 2022-09-17 17:55:44 发布