• python小玩意——计算器


    代码说明:

    python语言利用tk实现一个简易计算器

    具有0~9十位数字、符号’.’,以及±*/四则等运算功能,具有记忆存储功能(下一轮计算用到上一轮结果)、清零、删除功能。

    效果如下:

    在这里插入图片描述

    代码如下:

    import tkinter, time, decimal, math, string
    
    root = tkinter.Tk()
    root.title('计算器')
    root.resizable(0, 0)
    global cuncu, vartext, result, fuhao
    result = fuhao = None
    vartext = tkinter.StringVar()
    cuncu = []
    
    
    class anjianzhi:
        global cuncu, vartext, result, fuhao
    
        def __init__(self, anjian):
            self.anjian = anjian
    
        def jia(self):
            cuncu.append(self.anjian)
            vartext.set(''.join(cuncu))
    
        def tui(self):
            cuncu.pop()
            vartext.set(''.join(cuncu))
    
        def clear(self):
            cuncu.clear()
            vartext.set('')
            result = None
            fuhao = None
    
        def zhengfu(self):
            if cuncu[0]:
                if cuncu[0] == '-':
                    cuncu[0] = '+'
                elif cuncu[0] == '+':
                    cuncu[0] = '-'
                else:
                    cuncu.insert(0, '-')
            vartext.set(''.join(cuncu))
    
        def xiaoshudian(self):
            if cuncu.count('.') >= 1:
                pass
            else:
                if cuncu == []:
                    cuncu.append('0')
                cuncu.append('.')
                vartext.set(''.join(cuncu))
    
        def yunshuan(self):
            global cuncu, vartext, result, fuhao
            if vartext.get() == '':
                pass
            else:
                get1 = decimal.Decimal(vartext.get())
                if self.anjian in ('1/x', 'sqrt'):
                    if self.anjian == '1/x':
                        result = 1 / get1
                    elif self.anjian == 'sqrt':
                        result = math.sqrt(get1)
                elif self.anjian in ('+', '-', '*', '/', '='):
                    if fuhao is not None:
                        get1 = decimal.Decimal(result)
                        get2 = decimal.Decimal(vartext.get())
                        if fuhao == '+':
                            result = get1 + get2
                        elif fuhao == '-':
                            result = get1 - get2
                        elif fuhao == '*':
                            result = get1 * get2
                        elif fuhao == '/':
                            result = get1 / get2
                    else:
                        result = get1
                    if self.anjian == '=':
                        fuhao = None
                    else:
                        fuhao = self.anjian
                print(fuhao)
                print(result)
                vartext.set(str(result))
                cuncu.clear()
    
    
    def copy1():
        # tkinter.Misc().clipboard_clear()
        tkinter.Misc().clipboard_append(string(vartext.get()))
    
    
    def buju(root):
        global cuncu, vartext, result, fuhao
        entry1 = tkinter.Label(root, width=30, height=2, bg='white', anchor='se', textvariable=vartext)
        entry1.grid(row=0, columnspan=5)
        buttonMC = tkinter.Button(root, text='MC', width=5)
        buttonMR = tkinter.Button(root, text='MR', width=5)
        buttonMS = tkinter.Button(root, text='MS', width=5)
        buttonM1 = tkinter.Button(root, text='M+', width=5)
        buttonM2 = tkinter.Button(root, text='M-', width=5)
        buttonMC.grid(row=1, column=0)
        buttonMR.grid(row=1, column=1)
        buttonMS.grid(row=1, column=2)
        buttonM1.grid(row=1, column=3)
        buttonM2.grid(row=1, column=4)
    
        buttonJ = tkinter.Button(root, text='←', width=5, command=anjianzhi('c').tui)
        buttonCE = tkinter.Button(root, text='CE', width=5)
        buttonC = tkinter.Button(root, text=' C ', width=5, command=anjianzhi('c').clear)
        button12 = tkinter.Button(root, text='±', width=5, command=anjianzhi('c').zhengfu)
        buttonD = tkinter.Button(root, text='√', width=5, command=anjianzhi('sqrt').yunshuan)
        buttonJ.grid(row=2, column=0)
        buttonCE.grid(row=2, column=1)
        buttonC.grid(row=2, column=2)
        button12.grid(row=2, column=3)
        buttonD.grid(row=2, column=4)
    
        button7 = tkinter.Button(root, text=' 7 ', width=5, command=anjianzhi('7').jia)
        button8 = tkinter.Button(root, text=' 8 ', width=5, command=anjianzhi('8').jia)
        button9 = tkinter.Button(root, text=' 9 ', width=5, command=anjianzhi('9').jia)
        buttonc = tkinter.Button(root, text=' / ', width=5, command=anjianzhi('/').yunshuan)
        buttonf = tkinter.Button(root, text=' % ', width=5)
        button7.grid(row=3, column=0)
        button8.grid(row=3, column=1)
        button9.grid(row=3, column=2)
        buttonc.grid(row=3, column=3)
        buttonf.grid(row=3, column=4)
    
        button4 = tkinter.Button(root, text=' 4 ', width=5, command=anjianzhi('4').jia)
        button5 = tkinter.Button(root, text=' 5 ', width=5, command=anjianzhi('5').jia)
        button6 = tkinter.Button(root, text=' 6 ', width=5, command=anjianzhi('6').jia)
        buttonx = tkinter.Button(root, text=' * ', width=5, command=anjianzhi('*').yunshuan)
        buttonfs = tkinter.Button(root, text='1/x', width=5, command=anjianzhi('1/x').yunshuan)
        button4.grid(row=4, column=0)
        button5.grid(row=4, column=1)
        button6.grid(row=4, column=2)
        buttonx.grid(row=4, column=3)
        buttonfs.grid(row=4, column=4)
    
        button1 = tkinter.Button(root, text=' 1 ', width=5, command=anjianzhi('1').jia)
        button2 = tkinter.Button(root, text=' 2 ', width=5, command=anjianzhi('2').jia)
        button3 = tkinter.Button(root, text=' 3 ', width=5, command=anjianzhi('3').jia)
        button_ = tkinter.Button(root, text=' - ', width=5, command=anjianzhi('-').yunshuan)
        buttondy = tkinter.Button(root, text=' \n = \n ', width=5, command=anjianzhi('=').yunshuan)
        button1.grid(row=5, column=0)
        button2.grid(row=5, column=1)
        button3.grid(row=5, column=2)
        button_.grid(row=5, column=3)
        buttondy.grid(row=5, column=4, rowspan=2)
    
        button0 = tkinter.Button(root, text='   0   ', width=11, command=anjianzhi('0').jia)
        buttonjh = tkinter.Button(root, text=' . ', width=5, command=anjianzhi('c').xiaoshudian)
        buttonjia = tkinter.Button(root, text=' + ', width=5, command=anjianzhi('+').yunshuan)
        button0.grid(row=6, column=0, columnspan=2)
        buttonjh.grid(row=6, column=2)
        buttonjia.grid(row=6, column=3)
    
    
    def caidan(root):
        menu = tkinter.Menu(root)
        submenu1 = tkinter.Menu(menu, tearoff=0)
        menu.add_cascade(label='查看', menu=submenu1)
        submenu2 = tkinter.Menu(menu, tearoff=0)
        submenu2.add_command(label='复制')
        submenu2.add_command(label='粘贴')
        menu.add_cascade(label='编辑', menu=submenu2)
        submenu = tkinter.Menu(menu, tearoff=0)
        submenu.add_command(label='查看帮助')
        submenu.add_separator()
        submenu.add_command(label='关于计算机')
        menu.add_cascade(label='帮助', menu=submenu)
        root.config(menu=menu)
    
    
    buju(root)
    caidan(root)
    root.mainloop()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
  • 相关阅读:
    C#学习教程二——变量
    分布式机器学习:同步并行SGD算法的实现与复杂度分析(PySpark)
    c++介绍与入门基础(详细总结)
    超简单版Python打包exe文件,并修改图标,这将是你见过最容易上手的教程~
    神经网络控制器设计原理,神经网络控制系统设计
    vue 02
    适合中小企业的项目管理系统有哪些?
    SpringMVC简介
    io+day8
    好用的IP反查接口(2)
  • 原文地址:https://blog.csdn.net/zouxun660/article/details/126546842