简单的计算器(Python)

from tkinter import *

win = Tk()
win.title("计算器")
win.geometry('255x455')
en = Entry(win, width=10, font=("Arial", 31))
en.place(x=10, y=30)


def numinput(i):
    first_num = en.get()
    en.delete(0, END)
    en.insert(0, str(first_num) + str(i))


def C1():
    en.delete(0, END)


def plus():
    global operator
    global first_num
    first_num = en.get()
    en.delete(0, END)
    operator = "+"


def minus():
    global operator
    global first_num
    first_num = en.get()
    en.delete(0, END)
    operator = "-"


def cheng():
    global operator
    global first_num
    first_num = en.get()
    en.delete(0, END)
    operator = "*"


def divide():
    global operator
    global first_num
    first_num = en.get()
    en.delete(0, END)
    operator = "/"


def equal():
    next_num = en.get()
    en.delete(0, END)
    if operator == "+":
        en.insert(0, int(first_num) + int(next_num))
    if operator == "-":
        en.insert(0, int(first_num) - int(next_num))
    if operator == "*":
        en.insert(0, int(first_num) * int(next_num))
    if operator == "/":
        en.insert(0, int(first_num) // int(next_num))


bu7 = Button(win, text=7, width=2, font=("Arial", 31), command=lambda: numinput(7))
bu8 = Button(win, text=8, width=2, font=("Arial", 31), command=lambda: numinput(8))
bu9 = Button(win, text=9, width=2, font=("Arial", 31), command=lambda: numinput(9))
bu4 = Button(win, text=4, width=2, font=("Arial", 31), command=lambda: numinput(4))
bu5 = Button(win, text=5, width=2, font=("Arial", 31), command=lambda: numinput(5))
bu6 = Button(win, text=6, width=2, font=("Arial", 31), command=lambda: numinput(6))
bu1 = Button(win, text=1, width=2, font=("Arial", 31), command=lambda: numinput(1))
bu2 = Button(win, text=2, width=2, font=("Arial", 31), command=lambda: numinput(2))
bu3 = Button(win, text=3, width=2, font=("Arial", 31), command=lambda: numinput(3))
buj = Button(win, text="+", width=2, font=("Arial", 31), command=lambda: plus())
bujj = Button(win, text="-", width=2, font=("Arial", 31), command=lambda: minus())
buc = Button(win, text="*", width=2, font=("Arial", 31), command=lambda: cheng())
bucc = Button(win, text="/", width=2, font=("Arial", 31), command=lambda: divide())
bu0 = Button(win, text=0, width=2, font=("Arial", 31), command=lambda: numinput(0))
bud = Button(win, text="=", width=2, font=("Arial", 31), command=lambda: equal())
buccc = Button(win, text="C", width=2, font=("Arial", 31), command=lambda: C1())

bu7.place(x=10, y=100)
bu8.place(x=70, y=100)
bu9.place(x=130, y=100)
bu4.place(x=10, y=185)
bu5.place(x=70, y=185)
bu6.place(x=130, y=185)
bu1.place(x=10, y=270)
bu2.place(x=70, y=270)
bu3.place(x=130, y=270)
buj.place(x=190, y=100)
bujj.place(x=190, y=185)
buc.place(x=190, y=270)
bucc.place(x=190, y=355)
bu0.place(x=10, y=355)
bud.place(x=70, y=355)
buccc.place(x=130, y=355)

mainloop()

你可能感兴趣的:(python)