使用tkinter做一个简单的计算器

使用tkinter做一个简单的计算器_第1张图片

 主要思路:将数字和运算符号都作为按钮展现,当点击按钮时,数字显示在输入框中,点击=号是对数字进行运算,将原来的数字删除,将运算结果展示在输入框中。

注意!当第一个数是0的时候将这个0从输入框中进行删除,再输入其他数。

下面直接上代码:

import tkinter as tk
from tkinter import messagebox

class Calculator:
    """
    这是一个包含加减乘除的简单的计算器
    """
    def __init__(self,master):
        #定义界面
        self.e1 = tk.Entry(master,font = ('Arial',20),width=18)
        self.e1.grid(row = 0, columnspan=4, ipady=12)
        self.e1.insert('end', '0')

        self.b1 = tk.Button(master,text = '7',font=('Arial',15),bg = 'LightSkyBlue',width=4,command = self.seven)
        self.b1.grid(row=1,column=0)
        self.b2 = tk.Button(master, text='8', font=('Arial', 15),bg = 'LightSkyBlue', width=4, command = self.eight)
        self.b2.grid(row=1, column=1)
        self.b3 = tk.Button(master, text='9', font=('Arial', 15),bg = 'LightSkyBlue', width=4,command = self.nine)
        self.b3.grid(row=1, column=2)
        self.b4 = tk.Button(master, text='/', font=('Arial', 15),bg = 'orange', width=4,command = self.chu)
        self.b4.grid(row=1, column=3)

        self.b5 = tk.Button(master, text='4', font=('Arial', 15),bg = 'LightSkyBlue', width=4,command = self.four)
        self.b5.grid(row=2, column=0,pady=8)
        self.b6 = tk.Button(master, text='5', font=('Arial', 15),bg = 'LightSkyBlue', width=4,command = self.five)
        self.b6.grid(row=2, column=1,pady=8)
        self.b7 = tk.Button(master, text='6', font=('Arial', 15),bg = 'LightSkyBlue', width=4,command = self.six)
        self.b7.grid(row=2, column=2,pady=8)
        self.b8 = tk.Button(master, text='*', font=('Arial', 15),bg = 'orange', width=4,command = self.cheng)
        self.b8.grid(row=2, column=3,pady=8)

        self.b9 = tk.Button(master, text='1', font=('Arial', 15),bg = 'LightSkyBlue', width=4,command = self.one)
        self.b9.grid(row=3, column=0)
        self.b10 = tk.Button(master, text='2', font=('Arial', 15),bg = 'LightSkyBlue', width=4,command = self.two)
        self.b10.grid(row=3, column=1)
        self.b11 = tk.Button(master, text='3', font=('Arial', 15),bg = 'LightSkyBlue', width=4,command = self.three)
        self.b11.grid(row=3, column=2)
        self.b12 = tk.Button(master, text='-', font=('Arial', 15),bg = 'orange', width=4,command = self.jian)
        self.b12.grid(row=3, column=3)

        self.b13 = tk.Button(master, text='0', font=('Arial', 15),bg = 'LightSkyBlue', width=4,command = self.zero)
        self.b13.grid(row=4, column=0,pady=8)
        self.b14 = tk.Button(master, text='.', font=('Arial', 15),bg = 'LightSkyBlue', width=4,command = self.point)
        self.b14.grid(row=4, column=1,pady=8)
        self.b15 = tk.Button(master, text='+', font=('Arial', 15),bg = 'orange', width=4,command = self.add)
        self.b15.grid(row=4, column=2,pady=8)

        self.b16 = tk.Button(master, text='(', font=('Arial', 15),bg = 'orange', width=4, command=self.k1)
        self.b16.grid(row=5, column=0)
        self.b17 = tk.Button(master, text=')', font=('Arial', 15),bg = 'orange', width=4, command=self.k2)
        self.b17.grid(row=5, column=1)
        self.b18 = tk.Button(master, text='AC', font=('Arial', 15),bg = 'orange', width=4, command=self.de)
        self.b18.grid(row=5, column=2)



        self.b19 = tk.Button(master, text='=', font=('Arial', 15), bg = 'orange',width=4,height=3,command = self.cal)
        self.b19.grid(row=4, column=3,rowspan = 2)


    #定义button函数
    def zero(self):
        if self.e1.get() and self.e1.get() != '0':
            self.e1.insert('end','0')


    def one(self):
        if self.e1.get() != '0':
            self.e1.insert('end','1')
        else:
            self.e1.delete(0, 'end')
            self.e1.insert('end', '1')

    def two(self):
        if self.e1.get() != '0':
            self.e1.insert('end', '2')
        else:
            self.e1.delete(0, 'end')
            self.e1.insert('end', '2')

    def three(self):
        if self.e1.get() != '0':
            self.e1.insert('end','3')
        else:
            self.e1.delete(0, 'end')
            self.e1.insert('end', '3')

    def four(self):
        if self.e1.get() != '0':
            self.e1.insert('end', '4')
        else:
            self.e1.delete(0, 'end')
            self.e1.insert('end', '4')

    def five(self):
        if self.e1.get() != '0':
            self.e1.insert('end', '5')
        else:
            self.e1.delete(0, 'end')
            self.e1.insert('end', '5')

    def six(self):
        if self.e1.get() != '0':
            self.e1.insert('end', '6')
        else:
            self.e1.delete(0, 'end')
            self.e1.insert('end', '6')

    def seven(self):
        if self.e1.get() != '0':
            self.e1.insert('end', '7')
        else:
            self.e1.delete(0, 'end')
            self.e1.insert('end', '7')

    def eight(self):
        if self.e1.get() != '0':
            self.e1.insert('end','8')
        else:
            self.e1.delete(0, 'end')
            self.e1.insert('end', '8')

    def nine(self):
        if self.e1.get() != '0':
            self.e1.insert('end', '9')
        else:
            self.e1.delete(0, 'end')
            self.e1.insert('end', '9')

    def point(self):
        self.e1.insert('end','.')

    def add(self):
        self.e1.insert('end','+')

    def jian(self):
        self.e1.insert('end','-')

    def cheng(self):
        self.e1.insert('end','*')

    def chu(self):
        self.e1.insert('end','/')

    def k1(self):
        self.e1.insert('end','(')

    def k2(self):
        self.e1.insert('end',')')

    def de(self):
        self.e1.delete(0,'end')
        self.e1.insert('end', 0)

    def cal(self):
        if self.e1.get():
            try:
                res = eval(self.e1.get())
                self.e1.delete(0, 'end')
                self.e1.insert('end', res)

            except Exception as e:
                messagebox.showerror(title='错误信息', message=str(e))

        else:
            self.e1.insert('end', '0')


if __name__ == '__main__':
    root = tk.Tk()
    root.title('计算器')
    root.geometry('280x310+200+100')
    root.resizable(0,0)
    Calculator(root)
    root.mainloop()

打包APP:可在虚拟环境中进行APP的打包,不会的可以参考我之前发的打包exe文件过大.txt-Python文档类资源-CSDN下载

这样,你就拥有了你自己的一款计算器了,O(∩_∩)O哈哈~

你可能感兴趣的:(python)