超简单的python Tkinter计算器

# 简单说下实现思路
# 设置按钮,没按下一个按钮后,结果对应的字符串发生改变,最后通过eval()函数计算结果
import tkinter

Calculator = tkinter.Tk()

Calculator.title("Easy Calculator!")
Calculator.geometry('200x400')
StartCount = tkinter.StringVar()
StartCount.set('')


def NumberOne():
    StartCount.set(str(StartCount.get()) + "1")


def NumberTwo():
    StartCount.set(str(StartCount.get()) + "2")


def NumberThree():
    StartCount.set(str(StartCount.get()) + "3")


def NumberFour():
    StartCount.set(str(StartCount.get()) + "4")


def NumberFive():
    StartCount.set(str(StartCount.get()) + "5")


def NumberSix():
    StartCount.set(str(StartCount.get()) + "6")


def NumberSeven():
    StartCount.set(str(StartCount.get()) + "7")


def NumberEight():
    StartCount.set(str(StartCount.get()) + "8")


def NumberNine():
    StartCount.set(str(StartCount.get()) + "9")


def NumberZero():
    StartCount.set(str(StartCount.get()) + "0")


def Add():
    StartCount.set(str(StartCount.get()) + "+")


def Reduce():
    StartCount.set(str(StartCount.get()) + "-")


def Multiplication():
    StartCount.set(str(StartCount.get()) + "*")


def Division():
    StartCount.set(str(StartCount.get()) + "/")


def Result():
    try:
        StartCount.set(eval(str(StartCount.get())))
    except:
        StartCount.set("error")


def Clear():
    StartCount.set("")


result = tkinter.Label(Calculator, bg='white', fg='black', font=('Arail', '20'), bd='0', anchor='se',
                       textvariable=StartCount)
result.place(x='0', y='0', width='200', height='80')

ButtonNumberOne = tkinter.Button(Calculator, text="1", command=NumberOne, width=5, height=3, bg='white')
ButtonNumberOne.place(x='0', y='350')
ButtonNumberTwo = tkinter.Button(Calculator, text="2", command=NumberTwo, width=5, height=3, bg='white')
ButtonNumberTwo.place(x='50', y='350')
ButtonNumberThree = tkinter.Button(Calculator, text="3", command=NumberThree, width=5, height=3, bg='white')
ButtonNumberThree.place(x='100', y='350')

ButtonNumberFour = tkinter.Button(Calculator, text="4", command=NumberFour, width=5, height=3, bg='white')
ButtonNumberFour.place(x='0', y='280')
ButtonNumberFive = tkinter.Button(Calculator, text="5", command=NumberFive, width=5, height=3, bg='white')
ButtonNumberFive.place(x='50', y='280')
ButtonNumberSix = tkinter.Button(Calculator, text="6", command=NumberSix, width=5, height=3, bg='white')
ButtonNumberSix.place(x='100', y='280')

ButtonNumberSeven = tkinter.Button(Calculator, text="7", command=NumberSeven, width=5, height=3, bg='white')
ButtonNumberSeven.place(x='0', y='210')
ButtonNumberEight = tkinter.Button(Calculator, text="8", command=NumberEight, width=5, height=3, bg='white')
ButtonNumberEight.place(x='50', y='210')
ButtonNumberNine = tkinter.Button(Calculator, text="9", command=NumberNine, width=5, height=3, bg='white')
ButtonNumberNine.place(x='100', y='210')
ButtonZero = tkinter.Button(Calculator, text="0", command=NumberZero, width=5, height=3, bg='white')
ButtonZero.place(x='0', y='140')

ButtonAdd = tkinter.Button(Calculator, text="+", command=Add, width=5, height=8, bg='white')
ButtonAdd.place(x='150', y='210')
ButtonReduce = tkinter.Button(Calculator, text="-", command=Reduce, width=5, height=3, bg='white')
ButtonReduce.place(x='150', y='140')
ButtonMultiplication = tkinter.Button(Calculator, text="*", command=Multiplication, width=5, height=3, bg='white')
ButtonMultiplication.place(x='100', y='140')
ButtonDivision = tkinter.Button(Calculator, text="/", command=Division, width=5, height=3, bg='white')
ButtonDivision.place(x='50', y='140')

ButtonDivision = tkinter.Button(Calculator, text="Clear", command=Clear, width=26, height=3, bg='white')
ButtonDivision.place(x='0', y='80')

ButtonResult = tkinter.Button(Calculator, text="=", command=Result, width=5, height=3, bg='white')
ButtonResult.place(x='150', y='350')

Calculator.mainloop()

 

超简单的python Tkinter计算器_第1张图片

你可能感兴趣的:(超简单的python Tkinter计算器)