简易的计算器程序

代码如下:

from tkinter import *
import tkinter as tk
def add_clicked():
    entry1.insert(len(entry1.get()), '+')
def minus_clicked():
    entry1.insert(len(entry1.get()),'-')
def multiplication_clicked():
    entry1.insert(len(entry1.get()), '*')
def division_clicked():
    entry1.insert(len(entry1.get()),'/')
def answer_clicked():
    entry1.insert(len(entry1.get()),'='+str(eval(entry1.get())))
def clear():
    entry1.delete(0, "end")
    entry1.insert(0, "0")
def delete_fuction():
    entry1.delete(0,"end")
def del_the_last_singel_num():
    entry1.delete(len(entry1.get())-1, "end")
def getter0_click():
    entry1.insert(len(entry1.get()),0)
def getter1_click():
    if entry1.get()=='0':
        entry1.delete(0, "end")
    entry1.insert(len(entry1.get()),1)
def getter2_click():
    if entry1.get()=='0':
        entry1.delete(0, "end")
    entry1.insert(len(entry1.get()),2)
def getter3_click():
    if entry1.get()=='0':
        entry1.delete(0, "end")
    entry1.insert(len(entry1.get()),3)
def getter4_click():
    if entry1.get()=='0':
        entry1.delete(0, "end")
    entry1.insert(len(entry1.get()),4)
def getter5_click():
    if entry1.get()=='0':
        entry1.delete(0, "end")
    entry1.insert(len(entry1.get()),5)
def getter6_click():
    if entry1.get()=='0':
        entry1.delete(0, "end")
    entry1.insert(len(entry1.get()),6)
def getter7_click():
    if entry1.get()=='0':
        entry1.delete(0, "end")
    entry1.insert(len(entry1.get()),7)
def getter8_click():
    if entry1.get()=='0':
        entry1.delete(0, "end")
    entry1.insert(len(entry1.get()),8)
def getter9_click():
    if entry1.get()=='0':
        entry1.delete(0, "end")
    entry1.insert(len(entry1.get()),9)
def dot():
    entry1.insert(len(entry1.get()), ".")
win = tk.Tk()
win.title("计算器")
win.geometry("300x450")
entry1 = tk.Entry(win,width=10)
entry1.insert(0,"0")
entry1.place(x=0,y=0,width=300,height=75)
b01 = Button(win, text="C",fg="#ff0000",command=clear).place(x=0,y=75,width=75,height=75)
b02 = Button(win, text="DEL",bg="#ff9999",command = delete_fuction).place(x=75,y=75,width=75,height=75)

b03 = Button(win, text="BCK",bg="#add8e6",command=del_the_last_singel_num).place(x=150,y=75,width=75,height=75)

b04 = Button(win, text="/",bg="#aaff00",command =division_clicked).place(x=225,y=75,width=75,height=75)

b1 = Button(win, text="7",command = getter7_click).place(x=0,y=150,width=75,height=75)

b2 = Button(win, text="4",command = getter4_click).place(x=0,y=225,width=75,height=75)

b3 = Button(win, text="1",command = getter1_click).place(x=0,y=300,width=75,height=75)

b4 = Button(win, text="8",command = getter8_click).place(x=75,y=150,width=75,height=75)

b5 = Button(win, text="5",command = getter5_click).place(x=75,y=225,width=75,height=75)

b6 = Button(win, text="2",command = getter2_click).place(x=75,y=300,width=75,height=75)

b7 = Button(win, text="9",command = getter9_click).place(x=150,y=150,width=75,height=75)

b8 = Button(win, text="6",command = getter6_click).place(x=150,y=225,width=75,height=75)

b9 = Button(win, text="3",command = getter3_click).place(x=150,y=300,width=75,height=75)

b11 = Button(win, text="*",bg="#aaff00",command = multiplication_clicked).place(x=225,y=150,width=75,height=75)

b12 = Button(win, text="-",bg="#aaff00",command = minus_clicked).place(x=225,y=225,width=75,height=75)

b13 = Button(win, text="+",bg="#aaff00",command = add_clicked).place(x=225,y=300,width=75,height=75)

b14 = Button(win, text="0",command = getter0_click).place(x=0,y=375,width=75,height=75)

b15 = Button(win, text=".",command = dot).place(x=75,y=375,width=75,height=75)

b16 = Button(win, text="=",bg="#ffaa00",command=answer_clicked).place(x=150,y=375,width=150,height=75)

mainloop()

刚开始在编写程序时,并没有想到用eval()函数,导致程序及其复杂。

eval() 的作用是去掉参数中最外层引号,并以Python表达式的方式解析并执行,返回结果输出。eval()函数将去掉字符串的两个引号,将其解释为一个变量,并执行。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值