python之tkinter 实现简易计算器

 
  
from tkinter import *
root=Tk()
root.maxsize(300,400)


class CreateFrame():
    def __init__(self,frame,width,height):
        self.frame=frame
        self.width=width
        self.height=height
    def creat_frame(self):
        creat_frame=Frame(self.frame,width=self.width,height=self.height,bg='#ddd')
        return creat_frame


class CreateButton():
    def __init__(self):
        self.text=[['CE','C','X','^'],['*','/','-','+'],['0','1','2','3'],['4','5','6','7'],['8','9','.','=']]
        # self.button=[]

    def create_button(self,frame):
        for i in range(0,5):
            for j in range(0,4):
                button1 =Button(frame, text=self.text[i][j], width=8, height=2, bg='pink')
                button1.grid(row=i, column=j, padx=3, pady=3)
                # self.button.append(button1)
        frame.pack()
        return button1

class CreatLabel():

    def label(self,frame):
        label = Label(frame, font=('微软雅黑', 15),anchor='e',text='0')
        label.pack( anchor='e')
        frame.pack()
        return label

class BindBtton():
    # def __init__(self):
        # self.lt=[]
        # self.flage = False
    def btton_class_bind(self,button,label,label_frist):
        button.bind_class('Button','',lambda e: self(e,label,label_frist))
    def __call__(self,e,label,label_frist):
        # 显示运算表达式
        def change():
            if e.widget['text']=='.'and '.'in label['text']:
                #判断第一个数是不是能加小数点
                if '+'not in label['text']and '-'not in label['text']and '*'not in label['text']and '/'not in label['text']:
                    return
                #判断第二个数是否能加小数点
                if label['text'].count('.')==1 :

                    label['text'] =label['text'] + e.widget['text']
                    # return label['text']
                #按括号分割 判断第二数以后的表达式是否能加小数点
                for i in label['text'].split(')')[1:]:
                    # print(i)
                    if '.'in i and i.count('.')==1:
                        continue
                    else:
                        label['text'] += e.widget['text']

                return label['text']


            if label['text']=='0'and label['text']!='.':
                label['text'] =e.widget['text']

            else:
                label['text']=label['text']+e.widget['text']
        # 运算
        def operation():
            if e.widget['text']=='=':
                if label['text'][-1] not in '+-*/=%^':
                    labe2['text']=eval(label['text'])
                    label['text'] ='0'
                else:
                    labe2['text'] = eval(label['text'][:-1])

            elif '+'in label['text']or '-'in label['text'] or '*'in label['text'] or '/'in label['text'] or '%'in label['text']or '^'in label['text']:
                if e.widget['text']in '+-*/=%^':
                    if label['text'][-1] not in '+-*/=%^':
                        label['text']='('+label['text']+')'+e.widget['text']
                        # label['text'] ='('+str(eval(label['text']) )+')' + e.widget['text']
                else:
                    label['text'] += e.widget['text']
            else:
                label['text']+=e.widget['text']



        def equal():
            pass

        # 删除操作
        def delete():
            if label['text'][-1]==')':
                label['text'] = label['text'][1:-1]
            if len(label['text'])!=1:
                label['text']=label['text'][:-1]
            else:
                label['text']='0'
            #清空操作
        def clear():
            label['text']='0'
            labe2['text']='0'
            #正负操作
        # def fan():
        #     pass

        if e.widget['text'] in '1234567890.':
            change()
        elif  e.widget['text'] in  ['C', 'CE']:
            clear()
        elif  e.widget['text'] in '+-*/=%':
            operation()
        elif e.widget['text'] in 'X':
            delete()



frame1=CreateFrame(root,290,20).creat_frame()
label=CreatLabel().label(frame1)

frame3=CreateFrame(root,290,20).creat_frame()
labe2=CreatLabel().label(frame3)

frame2=CreateFrame(root,290,360)
frame=frame2.creat_frame()

button=CreateButton().create_button(frame)
BindBtton().btton_class_bind(button,label,labe2)


root.mainloop()


你可能感兴趣的:(python之tkinter 实现简易计算器)