计算器(Python)

在这里插入图片描述
在这里插入图片描述

from tkinter import *

window = Tk(className="陕西理工大学")
window.geometry('320x395')

show = StringVar()
show0 = StringVar()
entry = Entry(window, justify=RIGHT, relief=RAISED, textvariable=show, font=("楷体", 20)).place(x=0, y=35, width=320, height=35)
temp = Entry(window, justify=RIGHT, textvariable=show0, font=("楷体", 16)).place(x=0, y=0, width=320, height=35)
show.set('0')
sign, A= '', ''
def number(num):
    if show.get() == '0':
        show.set(num)
    else :
        show.set(show.get()+num)
def clearAll():
    show.set('0')
    show0.set('')
def delone():
    if show.get() != '0':
        show.set(show.get()[0:-1])
def xs():
    show.set(show.get()+'.')
def operator(op):
    global sign, A
    A = show.get()
    if op=='+':
        sign = '+'
        show0.set(show.get()+'+')
    elif op=='-':
        sign = '-'
        show0.set(show.get()+'-')
    elif op=='*':
        sign = '*'
        show0.set(show.get()+'*')
    elif op=='/':
        sign = '/'
        show0.set(show.get()+'/')
    show.set('')
def rev():
    if show.get() != '0':
        show.set('-'+show.get())
def value(a):
    global sign, A
    if a:
        show0.set(show.get() + '² = ')
        show.set(str(eval(show.get()) ** 2))
    else:
        try:
            show0.set(show0.get()+show.get()+' =')
            show.set(round(eval(A + ' ' + sign + ' ' + show.get()), 22))
        except ZeroDivisionError:
            show.set("不可以除0")


x = Button(window, command=lambda: value(1), text="x²", bg='SkyBlue', font=("黑体", 20)).place(x=2, y=72, width=75, height=60)
delete = Button(window, command=lambda: delone(), text="del", fg='Red', bg='SkyBlue', font=("黑体", 20)).place(x=82, y=72, width=75, height=60)
cancer = Button(window, command=lambda: clearAll(), text="C", fg='Red', bg='SkyBlue', font=("黑体", 20)).place(x=162, y=72, width=75, height=60)
div = Button(window, command=lambda: operator('/'), text="÷", bg='SkyBlue', font=("黑体", 20)).place(x=242, y=72, width=75, height=60)

button7 = Button(window, command=lambda: number('7'), text="7", bg='Pink', font=("黑体", 20)).place(x=2, y=137, width=75, height=60)
button8 = Button(window, command=lambda: number('8'), text="8", bg='Pink', font=("黑体", 20)).place(x=82, y=137, width=75, height=60)
button9 = Button(window, command=lambda: number('9'), text="9", bg='Pink', font=("黑体", 20)).place(x=162, y=137, width=75, height=60)
mul = Button(window, command=lambda: operator('*'), text="*", bg='SkyBlue', font=("黑体", 20)).place(x=242, y=137, width=75, height=60)

button4 = Button(window, command=lambda: number('4'), text="4", bg='Pink', font=("黑体", 20)).place(x=2, y=202, width=75, height=60)
button5 = Button(window, command=lambda: number('5'), text="5", bg='Pink', font=("黑体", 20)).place(x=82, y=202, width=75, height=60)
button6 = Button(window, command=lambda: number('6'), text="6", bg='Pink', font=("黑体", 20)).place(x=162, y=202, width=75, height=60)
sub = Button(window, command=lambda: operator('-'), text="-", bg='SkyBlue', font=("黑体", 20)).place(x=242, y=202, width=75, height=60)

button1 = Button(window, command=lambda: number('1'), text="1", bg='Pink', font=("黑体", 20)).place(x=2, y=267, width=75, height=60)
button2 = Button(window, command=lambda: number('2'), text="2", bg='Pink', font=("黑体", 20)).place(x=82, y=267, width=75, height=60)
button3 = Button(window, command=lambda: number('3'), text="3", bg='Pink', font=("黑体", 20)).place(x=162, y=267, width=75, height=60)
add = Button(window, command=lambda: operator('+'), text="+", bg='SkyBlue', font=("黑体", 20)).place(x=242, y=267, width=75, height=60)

zf = Button(window, command=lambda: rev(), text="+/-", bg='Pink', font=("黑体", 16)).place(x=2, y=332, width=75, height=60)
button0 = Button(window, command=lambda: number('0'), text="0", bg='Pink', font=("黑体", 20)).place(x=82, y=332, width=75, height=60)
dian = Button(window, command=lambda: xs(), text=".", bg='Pink', fg='Red', font=("黑体", 20)).place(x=162, y=332, width=75, height=60)
dy = Button(window, command=lambda: value(0), text="=", bg='Yellow', font=("黑体", 20)).place(x=242, y=332, width=75, height=60)

window.mainloop()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值