Python tkinter制作简易计算器

python版本3.9

import tkinter as tk

# 自定义异常类
class OperatorError(Exception):
    def __init__(self, count, maxcount):
        self.count = count
        self.maxcount = maxcount


HEIGHT = 400
WIDTH = 400
root = tk.Tk()
root.title('简易计算器')
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()


def callback(screen, number):
    screen.insert('end', number)


def tips(tip, s):
    tip.insert('end', s)


def clear(screen, tip):
    tip.delete(0, 'end')
    screen.delete(0, 'end')


def equal(screen):
    try:
        input = screen.get()
        expression = input.strip()
        sum = expression.count('+') + expression.count('-') + expression.count('*') + expression.count('/')
        if sum >= 2:
            raise OperatorError(sum, 1)
        output = str(eval(expression))
        clear(screen, tip)
        screen.insert('end', output)
    except OperatorError as e:
        tips(tip, "Error: 表达式中不能包含多于一个的运算符")
    except SyntaxError as e:
        tips(tip, "Error: 一个数字中,不能包含多于一个的小数点")


frame = tk.Frame(root, bg='#ADD8E6', bd=8)
frame.place(relx=0, rely=0, relwidth=1, relheight=0.15)

frame1 = tk.Frame(root, bd=8)
frame1.place(relx=0, rely=0.2, relwidth=1, relheight=0.8)

frame2 = tk.Frame(root)
frame2.place(relx=0, rely=0.15, relwidth=1, relheight=0.05)

# 创建显示计算区
screen = tk.Entry(frame, justify='right')
screen.place(relwidth=1, relheight=1)

# 创建显示错误区
tip = tk.Entry(frame2, justify='right')
tip.place(relwidth=1, relheight=1)

ButtonC = tk.Button(frame1, text="C", command=lambda: clear(screen, tip))
ButtonC.place(relx=0.025, rely=0.025, relwidth=0.2, relheight=0.2)

Button7 = tk.Button(frame1, text="7", command=lambda: callback(screen, '7'))
Button7.place(relx=0.025, rely=0.275, relwidth=0.2, relheight=0.2)

Button4 = tk.Button(frame1, text="4", command=lambda: callback(screen, '4'))
Button4.place(relx=0.025, rely=0.525, relwidth=0.2, relheight=0.2)

Button1 = tk.Button(frame1, text="1", command=lambda: callback(screen, '1'))
Button1.place(relx=0.025, rely=0.775, relwidth=0.2, relheight=0.2)

Buttonadd = tk.Button(frame1, text="+", command=lambda: callback(screen, '+'))
Buttonadd.place(relx=0.275, rely=0.025, relwidth=0.2, relheight=0.2)

Button8 = tk.Button(frame1, text="8", command=lambda: callback(screen, '8'))
Button8.place(relx=0.275, rely=0.275, relwidth=0.2, relheight=0.2)

Button5 = tk.Button(frame1, text="5", command=lambda: callback(screen, '5'))
Button5.place(relx=0.275, rely=0.525, relwidth=0.2, relheight=0.2)

Button2 = tk.Button(frame1, text="2", command=lambda: callback(screen, '2'))
Button2.place(relx=0.275, rely=0.775, relwidth=0.2, relheight=0.2)

Buttonsub = tk.Button(frame1, text="-", command=lambda: callback(screen, '-'))
Buttonsub.place(relx=0.525, rely=0.025, relwidth=0.2, relheight=0.2)

Button9 = tk.Button(frame1, text="9", command=lambda: callback(screen, '9'))
Button9.place(relx=0.525, rely=0.275, relwidth=0.2, relheight=0.2)

Button6 = tk.Button(frame1, text="6", command=lambda: callback(screen, '6'))
Button6.place(relx=0.525, rely=0.525, relwidth=0.2, relheight=0.2)

Button3 = tk.Button(frame1, text="3", command=lambda: callback(screen, '3'))
Button3.place(relx=0.525, rely=0.775, relwidth=0.2, relheight=0.2)

Buttonmul = tk.Button(frame1, text="*", command=lambda: callback(screen, '*'))
Buttonmul.place(relx=0.775, rely=0.025, relwidth=0.2, relheight=0.2)

Buttondiv = tk.Button(frame1, text="/", command=lambda: callback(screen, '/'))
Buttondiv.place(relx=0.775, rely=0.275, relwidth=0.2, relheight=0.2)

Buttonequ = tk.Button(frame1, text="=", command=lambda: equal(screen))
Buttonequ.place(relx=0.775, rely=0.525, relwidth=0.2, relheight=0.2)

ButtonC1 = tk.Button(frame1, text="C", command=lambda: clear(screen, tip))
ButtonC1.place(relx=0.775, rely=0.775, relwidth=0.2, relheight=0.2)

root.mainloop()


结果:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值