importtkinterimportdecimalimportmathclassKeyValue:def__init__(self,key_):self.key_=key_ self.flag=0defdel_(self):print("del_")ifsave:save.pop()else:passvar_text.set(''.join(save))defclear(self):print("clear")save.clear()var_text.set('')defmemory(self):print("memory conf")ifself.key_=="m+":ifsave==[]:passelifself.flag==1:passelse:self.flag=1elifself.key_=="mr":passelifself.key_=='ms':self.clear()elifself.key_=='mc':self.flag=0self.clear()elifself.key_=='m-':self.flag=0self.clear()else:passvar_text.set(''.join(save))defplus_sub(self):print('plus_sub')ifsave[0]:ifsave[0]=='-':save[0]='+'elifsave[0]=='+':save[0]='-'else:save.insert(0,'-')var_text.set(''.join(save))defcalculate(self):globalsave,var_text,result,symbolifvar_text.get()=='':passelse:get1=decimal.Decimal(var_text.get())ifself.key_in('1/x','sqrt'):ifself.key_=='1/x':result=1/get1elifself.key_=='sqrt':result=math.sqrt(get1)elifself.key_in('+','-','*','/','=','%'):ifsymbolisnotNone:get1=decimal.Decimal(result)get2=decimal.Decimal(var_text.get())ifsymbol=='+':result=get1+get2elifsymbol=='-':result=get1-get2elifsymbol=='*':result=get1*get2elifsymbol=='/':result=get1/get2elifsymbol=='%':result=get1%get2else:result=get1ifself.key_=='=':print('calculate result = ',result)symbol=Noneelse:symbol=self.key_print(symbol)var_text.set(str(result))save.clear()defpress(self):print('press',self.key_)ifself.key_=='.':ifsave.count('.')>=1:passelse:ifsave==[]:save.append('0')else:save.append(self.key_)var_text.set(''.join(save))else:save.append(self.key_)var_text.set(''.join(save))defpoint(self):print('point')ifsave.count('.')>=1:passelse:ifsave==[]:save.append('0')save.append('.')var_text.set(''.join(save))defwindow(root):globalvar_text entry1=tkinter.Label(root,width=40,height=2,bg='white',anchor='se',textvariable=var_text)entry1.grid(row=0,columnspan=5)button_MC=tkinter.Button(root,text='MC',width=5,command=KeyValue('mc').clear)button_MC.grid(row=1,column=0)button_MR=tkinter.Button(root,text='MR',width=5,command=KeyValue('mr').memory)button_MR.grid(row=1,column=1)button_MS=tkinter.Button(root,text='MS',width=5,command=KeyValue('ms').memory)button_MS.grid(row=1,column=2)button_M1=tkinter.Button(root,text='M+',width=5,command=KeyValue('m+').memory)button_M1.grid(row=1,column=3)button_M2=tkinter.Button(root,text='M-',width=5,command=KeyValue('m-').clear)button_M2.grid(row=1,column=4)button_del=tkinter.Button(root,text='←',width=5,command=KeyValue('del_').del_)button_del.grid(row=2,column=0)button_CE=tkinter.Button(root,text='CE',width=5,command=KeyValue('ce').clear)button_CE.grid(row=2,column=1)button_C=tkinter.Button(root,text=' C ',width=5,command=KeyValue('c').clear)button_C.grid(row=2,column=2)button_P_S=tkinter.Button(root,text='±',width=5,command=KeyValue('c').plus_sub)button_P_S.grid(row=2,column=3)button_sqrt=tkinter.Button(root,text='√',width=5,command=KeyValue('sqrt').calculate)button_sqrt.grid(row=2,column=4)button_7=tkinter.Button(root,text=' 7 ',width=5,command=KeyValue('7').press)button_7.grid(row=3,column=0)button_8=tkinter.Button(root,text=' 8 ',width=5,command=KeyValue('8').press)button_8.grid(row=3,column=1)button_9=tkinter.Button(root,text=' 9 ',width=5,command=KeyValue('9').press)button_9.grid(row=3,column=2)button_c=tkinter.Button(root,text=' / ',width=5,command=KeyValue('/').calculate)button_c.grid(row=3,column=3)button_f=tkinter.Button(root,text=' % ',width=5,command=KeyValue('%').calculate)button_f.grid(row=3,column=4)button_4=tkinter.Button(root,text=' 4 ',width=5,command=KeyValue('4').press)button_4.grid(row=4,column=0)button_5=tkinter.Button(root,text=' 5 ',width=5,command=KeyValue('5').press)button_5.grid(row=4,column=1)button_6=tkinter.Button(root,text=' 6 ',width=5,command=KeyValue('6').press)button_6.grid(row=4,column=2)button_multi=tkinter.Button(root,text=' * ',width=5,command=KeyValue('*').calculate)button_fraction=tkinter.Button(root,text='1/x',width=5,command=KeyValue('1/x').calculate)button_multi.grid(row=4,column=3)button_fraction.grid(row=4,column=4)button_1=tkinter.Button(root,text=' 1 ',width=5,command=KeyValue('1').press)button_2=tkinter.Button(root,text=' 2 ',width=5,command=KeyValue('2').press)button_3=tkinter.Button(root,text=' 3 ',width=5,command=KeyValue('3').press)button__=tkinter.Button(root,text=' - ',width=5,command=KeyValue('-').calculate)button_equal=tkinter.Button(root,text=' \n = \n ',width=5,command=KeyValue('=').calculate)button_1.grid(row=5,column=0)button_2.grid(row=5,column=1)button_3.grid(row=5,column=2)button__.grid(row=5,column=3)button_equal.grid(row=5,column=4,rowspan=2)button_0=tkinter.Button(root,text=' 0 ',width=13,command=KeyValue('0').press)button_sub=tkinter.Button(root,text=' . ',width=5,command=KeyValue('.').press)button_plus=tkinter.Button(root,text=' + ',width=5,command=KeyValue('+').calculate)button_0.grid(row=6,column=0,columnspan=2)button_sub.grid(row=6,column=2)button_plus.grid(row=6,column=3)defmenu(root):menu=tkinter.Menu(root)sub_menu1=tkinter.Menu(menu,tearoff=0)sub_menu1.add_command(label='标准')sub_menu1.add_command(label='科学')sub_menu1.add_command(label='程序')menu.add_cascade(label='查看(V)',menu=sub_menu1)sub_menu2=tkinter.Menu(menu,tearoff=0)sub_menu2.add_command(label='复制')sub_menu2.add_command(label='粘贴')menu.add_cascade(label='编辑(E)',menu=sub_menu2)sub_menu=tkinter.Menu(menu,tearoff=0)sub_menu.add_command(label='查看帮助')sub_menu.add_separator()sub_menu.add_command(label='关于计算器')menu.add_cascade(label='帮助(H)',menu=sub_menu)root.config(menu=menu)if__name__=='__main__':root=tkinter.Tk()root.title('计算器')root.resizable(0,0)result=symbol=Nonevar_text=tkinter.StringVar()save=[]window(root)menu(root)root.mainloop()
python计算器程序_python GUI- 计算器程序
最新推荐文章于 2024-01-17 12:05:48 发布