python 实现一个简单的计算器

发布时间 2023-12-26 20:16:34作者: 夏沫琅琊

python 实现一个简单的计算器

本文主要整合下tkinter ,实现下简单的计算器.

代码如下:

#!/usr/bin/python3
# -*- coding: UTF-8 -*-
"""
 @Author: zh
 @Time 2023/12/2 下午13:01  .
 @Email:
 @Describe:
"""
import tkinter as tk

# 创建计算器窗口
root = tk.Tk()
root.title("计算器")
# root.geometry("800x800+100+100")

input = tk.Entry(root, width=50, borderwidth=4)
input.grid(row=0, column=0, columnspan=3, padx=20, pady=20)


def btnClick(number):
    current = input.get()
    input.delete(0, tk.END)
    input.insert(0, str(current) + str(number))


def clear():
    input.delete(0, tk.END)


def add():
    current = input.get()
    global first_num
    global math
    math = "add"
    first_num = float(current)
    clear()


def subtract():
    current = input.get()
    global first_num
    global math
    math = "subtract"
    first_num = float(current)
    clear()


def multiply():
    current = input.get()
    global first_num
    global math
    math = "multiply"
    first_num = float(current)
    clear()


def divide():
    current = input.get()
    global first_num
    global math
    math = "divide"
    first_num = float(current)
    clear()

def equals():
    second_number = input.get()
    clear()
    if math == "add":
        input.insert(0, first_num + float(second_number))
    elif math == "subtract":
        input.insert(0, first_num - float(second_number))
    elif math == "multiply":
        input.insert(0, first_num * float(second_number))
    elif math == "divide":
        input.insert(0, first_num / float(second_number))

btn1 = tk.Button(root, padx=40, pady=20, text="1", command=lambda: btnClick(1))
btn2 = tk.Button(root, padx=40, pady=20, text="2", command=lambda: btnClick(2))
btn3 = tk.Button(root, padx=40, pady=20, text="3", command=lambda: btnClick(3))

btn4 = tk.Button(root, padx=40, pady=20, text="4", command=lambda: btnClick(4))
btn5 = tk.Button(root, padx=40, pady=20, text="5", command=lambda: btnClick(5))
btn6 = tk.Button(root, padx=40, pady=20, text="6", command=lambda: btnClick(6))

btn7 = tk.Button(root, padx=40, pady=20, text="7", command=lambda: btnClick(7))
btn8 = tk.Button(root, padx=40, pady=20, text="8", command=lambda: btnClick(8))
btn9 = tk.Button(root, padx=40, pady=20, text="9", command=lambda: btnClick(9))

btn0 = tk.Button(root, padx=40, pady=20, text="0", command=lambda: btnClick(0))
btnAdd = tk.Button(root, padx=40, pady=20, text="+", command=lambda: add())
btnSubtract = tk.Button(root, padx=42, pady=20, text="-", command=lambda: subtract())
btnMultiply = tk.Button(root, padx=40, pady=20, text="x", command=lambda: multiply())
btnDivide = tk.Button(root, padx=40, pady=20, text="➗", command=lambda: divide())
btnEquals = tk.Button(root, padx=40, pady=20, text="=", command=lambda: equals())
btnClear = tk.Button(root, padx=120, pady=20, text="clear", command=lambda: clear())

# 布局
btn1.grid(row=1, column=0)
btn2.grid(row=1, column=1)
btn3.grid(row=1, column=2)

btn4.grid(row=2, column=0)
btn5.grid(row=2, column=1)
btn6.grid(row=2, column=2)

btn7.grid(row=3, column=0)
btn8.grid(row=3, column=1)
btn9.grid(row=3, column=2)

btn0.grid(row=4, column=0)
btnAdd.grid(row=4, column=1)
btnSubtract.grid(row=4, column=2)

btnMultiply.grid(row=5, column=0)
btnDivide.grid(row=5, column=1)
btnEquals.grid(row=5, column=2)

btnClear.grid(row=6, column=0, columnspan=3)

root.mainloop()