简单的计算器(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()
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值