计算器.py

import tkinter
import decimal
import math


class KeyValue:

    def __init__(self, key_):
        self.key_ = key_
        self.flag = 0

    def del_(self):#删除,如果save不为空则删除栈顶元素,否则pass
        if save:
            save.pop()
        else:
            pass
        var_text.set(''.join(save))

    def clear(self):#清零,将save清空
        save.clear()
        var_text.set('')

    def plus_sub(self):#取相反数操作
        if save[0]:
            if save[0] == '-':
                save[0] = '+'
            elif save[0] == '+':
                save[0] = '-'
            else:
                save.insert(0, '-')
        var_text.set(''.join(save))

    def calculate(self):#运算操作
        global save, var_text, result, symbol
        if var_text.get() == '':
            pass
        else:
            get1 = decimal.Decimal(var_text.get())
            if self.key_ in ('1/x', 'sqrt'):
                if self.key_ == '1/x':
                    result = 1 / get1
                    if '.' in str(result):
                        result = round(result, 15)
                    result = str(result).rstrip('0')
                elif self.key_ == 'sqrt':
                    result = math.sqrt(get1)
            elif self.key_ in ('+', '-', '*', '/', '=', '%'):
                if symbol is not None:
                    get1 = decimal.Decimal(result)
                    get2 = decimal.Decimal(var_text.get())
                    if symbol == '+':
                        result = get1 + get2
                    elif symbol == '-':
                        result = get1 - get2
                    elif symbol == '*':
                        result = get1 * get2
                    elif symbol == '/':
                        result = get1 / get2
                        if '.' in str(result):
                            result = round(result, 15)
                        result=str(result).rstrip('0')
                    elif symbol == '%':
                        result = get1 % get2
                else:
                    result = get1
                if self.key_ == '=':
                    symbol = None
                else:
                    symbol = self.key_


            var_text.set(str(result))
            save.clear()

    def press(self):#按键操作
        if self.key_ == '.':
            if save.count('.') >= 1:
                pass
            else:
                if save == []:
                    pass
                else:
                    save.append(self.key_)
                    var_text.set(''.join(save))
        else:
            save.append(self.key_)
            var_text.set(''.join(save))

    def point(self):#取小数操作
        if save.count('.') >= 1:
            pass
        else:
            if save == []:
                save.append('0')
            save.append('.')
            var_text.set(''.join(save))


def window(root):
    #显示框标签布局
    global var_text
    entry1 = tkinter.Label(root, width=21, height=2, bg='white',font=("楷体", 25), anchor='se', textvariable=var_text)
    entry1.grid(row=0, columnspan=5)

    #按钮布局
    button_del = tkinter.Button(root, text='←', width=9, height=3,bg='#F7F7F7',command=KeyValue('del_').del_)
    button_del.grid(row=2, column=0)
    button_CE = tkinter.Button(root, text='CE', width=9,  height=3,bg='#F7F7F7',command=KeyValue('ce').clear)
    button_CE.grid(row=2, column=1)
    button_C = tkinter.Button(root, text=' C ', width=9,  height=3,bg='#F7F7F7',command=KeyValue('c').clear)
    button_C.grid(row=2, column=2)
    button_P_S = tkinter.Button(root, text='±', width=9,  height=3,bg='#F7F7F7',command=KeyValue('c').plus_sub)
    button_P_S.grid(row=2, column=3)
    button_sqrt = tkinter.Button(root, text='√', width=9,  height=3,bg='#F7F7F7',command=KeyValue('sqrt').calculate)
    button_sqrt.grid(row=2, column=4)

    button_7 = tkinter.Button(root, text=' 7 ', width=9,  height=3, bg='white',command=KeyValue('7').press)
    button_7.grid(row=3, column=0)
    button_8 = tkinter.Button(root, text=' 8 ', width=9,  height=3, bg='white',command=KeyValue('8').press)
    button_8.grid(row=3, column=1)
    button_9 = tkinter.Button(root, text=' 9 ', width=9,  height=3, bg='white',command=KeyValue('9').press)
    button_9.grid(row=3, column=2)
    button_c = tkinter.Button(root, text=' / ', width=9,  height=3, bg='#F7F7F7',command=KeyValue('/').calculate)
    button_c.grid(row=3, column=3)
    button_f = tkinter.Button(root, text=' % ', width=9,  height=3, bg='#F7F7F7',command=KeyValue('%').calculate)
    button_f.grid(row=3, column=4)

    button_4 = tkinter.Button(root, text=' 4 ', width=9,  height=3, bg='white',command=KeyValue('4').press)
    button_4.grid(row=4, column=0)
    button_5 = tkinter.Button(root, text=' 5 ', width=9,  height=3, bg='white',command=KeyValue('5').press)
    button_5.grid(row=4, column=1)
    button_6 = tkinter.Button(root, text=' 6 ', width=9,  height=3, bg='white',command=KeyValue('6').press)
    button_6.grid(row=4, column=2)
    button_multi = tkinter.Button(root, text=' * ', width=9,  height=3, bg='#F7F7F7',command=KeyValue('*').calculate)
    button_fraction = tkinter.Button(root, text='1/x', width=9,  height=3, bg='#F7F7F7',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=9,  height=3, bg='white',command=KeyValue('1').press)
    button_2 = tkinter.Button(root, text=' 2 ', width=9,  height=3, bg='white',command=KeyValue('2').press)
    button_3 = tkinter.Button(root, text=' 3 ', width=9,  height=3, bg='white',command=KeyValue('3').press)
    button__ = tkinter.Button(root, text=' — ', width=9,  height=3, bg='#F7F7F7',command=KeyValue('-').calculate)
    button_equal = tkinter.Button(root, text=' \n = \n ', width=9, bg='#91C1E7', height=7, 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=19,  height=3,bg='white', command=KeyValue('0').press)
    button_sub = tkinter.Button(root, text=' . ', width=9,  height=3,bg='white',command=KeyValue('.').press)
    button_plus = tkinter.Button(root, text=' + ', width=9,  height=3, bg='#F7F7F7',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)



if __name__ == '__main__':
    root = tkinter.Tk()#图形界面的接口
    root.title('计算器')
    root.resizable(0, 0)#设置禁止调整界面大小
    result = symbol = None
    var_text = tkinter.StringVar()
    save = []

    window(root)
    root.mainloop()

界面效果图如图如下:
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值