结果呈现
实现了加、减、乘、除运算,并可以进行清除及删除操作。
代码
from tkinter import *
Calculator = Tk()
Calculator.geometry('320x195+400+120')
Calculator.title('计算器示例')
Formula = ''
digit = 0
OutputNumber = 0
def input1():
global Formula
global digit
Inputentry.insert(END,'1')
Formula += '1'
digit += 1
def input2():
global Formula
global digit
digit += 1
Inputentry.insert(END,'2')
Formula += '2'
def input3():
global Formula
global digit
digit += 1
Inputentry.insert(END,'3')
Formula += '3'
def input4():
global Formula
global digit
digit += 1
Inputentry.insert(END,'4')
Formula += '4'
def input5():
global Formula
global digit
digit += 1
Inputentry.insert(END,'5')
Formula += '5'
def input6():
global Formula
global digit
digit += 1
Inputentry.insert(END,'6')
Formula += '6'
def input7():
global Formula
global digit
digit += 1
Inputentry.insert(END,'7')
Formula += '7'
def input8():
global Formula
global digit
digit += 1
Inputentry.insert(END,'8')
Formula += '8'
def input9():
global Formula
global digit
digit += 1
Inputentry.insert(END,'9')
Formula += '9'
def input0():
global Formula
global digit
digit += 1
Inputentry.insert(END,'0')
Formula += '0'
def inputPoint():
global Formula
global digit
digit += 1
Inputentry.insert(END,'.')
Formula += '.'
def inputDivi():
global Formula
global digit
digit += 1
Inputentry.insert(END,'÷')
Formula += '/'
def inputMultip():
global Formula
global digit
digit += 1
Inputentry.insert(END,'×')
Formula += '*'
def inputSubtra():
global Formula
global digit
digit += 1
Inputentry.insert(END,'—')
Formula += '-'
def inputAdd():
global Formula
global digit
digit += 1
Inputentry.insert(END,'+')
Formula += '+'
def inputEqual():
Outputentry.delete(0, END)
global Formula
global digit
digit += 1
Formula += ' '
global OutputNumber
Inputentry.insert(END,'=')
try:
OutputNumber = eval(Formula)
Outputentry.insert(END, OutputNumber)
except:
Outputentry.insert(END, '输入有问题哦!')
def inputCE():
global Formula
global digit
digit = 0
Inputentry.delete(0, END)
Formula = ''
def inputC():
global Formula
global OutputNumber
global digit
digit = 0
Inputentry.delete(0, END)
Formula = ''
Outputentry.delete(0, END)
OutputNumber = 0
def inputDel():
global Formula
global digit
Formula = Formula[:-1]
Inputentry.delete(digit-1,END)
digit -= 1
Input = Label(Calculator, text='输入:',width=10,anchor='n').grid(row=0, column=0, sticky=E + W)
Inputentry = Entry(Calculator, width=35)
Inputentry.grid(row=0, column=1, columnspan=3)
Output = Label(Calculator, text='输出:',width=10,anchor='n').grid(row=1, column=0, sticky=E + W)
Outputentry = Entry(Calculator, width=35)
Outputentry.grid(row=1, column=1, columnspan=3)
L1 = Button(Calculator, text='7', width=10, command = input7)
L2 = Button(Calculator, text='8', width=10, command = input8)
L3 = Button(Calculator, text='9', width=10, command = input9)
L4 = Button(Calculator, text='4', width=10, command = input4)
L5 = Button(Calculator, text='5', width=10, command = input5)
L6 = Button(Calculator, text='6', width=10, command = input6)
L7 = Button(Calculator, text='1', width=10, command = input1)
L8 = Button(Calculator, text='2', width=10, command = input2)
L9 = Button(Calculator, text='3', width=10, command = input3)
L10 = Button(Calculator, text='.', width=10, command = inputPoint)
L11 = Button(Calculator, text='0', width=10, command = input0)
L12 = Button(Calculator, text='=', width=20, command = inputEqual)
L13 = Button(Calculator, text='CE', width=10, command = inputCE)
L14 = Button(Calculator, text='C', width=10, command = inputC)
L15 = Button(Calculator, text='Back', width=10, command = inputDel)
L16 = Button(Calculator, text='÷', width=10, command = inputDivi)
L17 = Button(Calculator, text='×', width=10, command = inputMultip)
L18 = Button(Calculator, text='—', width=10, command = inputSubtra)
L19 = Button(Calculator, text='+', width=10, command = inputAdd)
L13.grid(row=2, column=0)
L14.grid(row=2, column=1)
L15.grid(row=2, column=2)
L16.grid(row=2, column=3)
L1.grid(row=3, column=0)
L2.grid(row=3, column=1)
L3.grid(row=3, column=2)
L17.grid(row=3, column=3)
L4.grid(row=4, column=0)
L5.grid(row=4, column=1)
L6.grid(row=4, column=2)
L18.grid(row=4, column=3)
L7.grid(row=5, column=0)
L8.grid(row=5, column=1)
L9.grid(row=5, column=2)
L19.grid(row=5, column=3)
L10.grid(row=6, column=0)
L11.grid(row=6, column=1)
L12.grid(row=6, column=2, columnspan=2, sticky=E + W)
Calculator.mainloop()