succi -制作计算器

import tkinter as tk

window = tk.Tk()
window.title('计算器')
window.geometry('280x500')

# label
la_jieguo = tk.Label(
    window,
    text="显示结果",
    bg="#FFBBFF",
    fg="black",
    anchor="se",  # 锚 east west north south
    font=("SimHei", 30)
)
la_jieguo.place(x=0, y=0, width=280, height=100)

la_gongshi = tk.Label(
    window,
    bg='#FFBBFF',
    fg='black',
    font=('SimHei', 24),
    text='显示公式',
    anchor='se'

)
la_gongshi.place(x=0, y=100, width=280, height=100)

gongshi = ''


def xianshi(name):
    global gongshi
    if name=='÷':
        name='/'
    if name=='×':
        name='*'

    gongshi = gongshi + name
    la_gongshi["text"] = gongshi


def jisuan():
    global gongshi
    la_jieguo['text'] = eval(gongshi)

def qingchu():
    global gongshi
    gongshi=''
    la_gongshi['text']=gongshi


def btn(name, x, y, w=70, h=60):
    anniu_gongneng = lambda: xianshi(name)  # 函数改成不运行的函数
    if name=='=':
        anniu_gongneng=lambda:jisuan()
    if name=='AC':
        anniu_gongneng=lambda:qingchu()
    btn_left = tk.Button(
        window,
        text=name,
        bg='#BFEFFF',
        fg='orange',
        font=("SimHei", 20, "bold"),
        bd=7,
        command=anniu_gongneng  # 按钮的指向参数;表示点击按钮对应运行哪个函数
    )
    btn_left.place(x=x, y=y, width=w, height=h)


btn('AC', 0, 200)
btn('←', 70, 200)
btn('÷', 140, 200)
btn('×', 210, 200)

btn('7', 0, 260)
btn('8', 70, 260)
btn('9', 140, 260)
btn('-', 210, 260)

btn('4', 0, 320)
btn('5', 70, 320)
btn('6', 140, 320)
btn('+', 210, 320)

btn('1', 0, 380)
btn('2', 70, 380)
btn('3', 140, 380)
btn('=', 210, 380, h=120)

btn('%', 0, 440, w=35)
btn('.', 35, 440, w=35)
btn('0', 70, 440)
btn('(', 140, 440, w=35)
btn(')', 175, 440, w=35)
window.mainloop()

猜你喜欢

转载自blog.csdn.net/houlaos/article/details/106587684