代码如下:
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()函数将去掉字符串的两个引号,将其解释为一个变量,并执行。