#简易计算器
from tkinter import *
import math
root = Tk()
root.title('计算器')
root.minsize(340, 310)
# 顶部区域
v = StringVar()
v.set('0')
show_label = Label(root, textvariable=v, bg='white', width=12, height=1, font=("黑体", 20, "bold"), justify=LEFT,
anchor='e')
show_label.pack(padx=10, pady=15)
# 是否按下了运算符
isopera = False
# 设置初始结果
calc = []
# 序列操作
def change(num):
global isopera
if isopera == False:
if v.get() == '0':
v.set('')
v.set(num)
else:
v.set(v.get() + num)
else:
v.set(num)
isopera = False
# 运算符号
def operation(sign):
global isopera
global calc
if isopera == True:
calc[1] = sign
else:
num = v.get()
calc.append(num) # -------------------------------------------------------------运算数1
calc.append(sign) # -----------------------------------------------------------运算符
isopera = True #
# 等于操作
def equal():
global calc
# 获取当前界面的数值准备运算
calc.append(v.get())
print(calc)
# 组成运算字符串
calcstr = ''.join(calc)
# 检测最后一位是否是运算符,是就删除
if calcstr[-1] in '+-*/':
calcstr = calcstr[0:-1]
# 运算操作
result = eval(calcstr)
# 显示结果
v.set(result)
calc.clear()
# 删除操作
def delete():
if v.get() == '' or v.get() == '0':
v.set('0')
return
else:
num = len(v.get())
if num > 1:
strnum = v.get()
strnum = strnum[0:num - 1]
v.set(strnum)
else:
v.set('0')
# 清空操作
def clear():
global calc
calc = []
v.set('0')
isopera = False
# 取反操作
def negation():
strnum = v.get()
if strnum[0] == '-':
v.set(strnum[1:])
elif strnum[0] != '-' and strnum != '0':
v.set('-' + strnum)
# 取倒数
def reciprocal(num):
strnum = v.get()
if strnum != '' and strnum != '0':
v.set(1 / float(strnum))
elif strnum == '0':
pass
# 百分号
def percent(num):
strnum = v.get()
if strnum != '' and strnum != '0':
v.set(float(strnum) / 100)
elif strnum == '0':
pass
# 根号
def sqrt(num):
strnum = v.get()
if float(strnum) > 0:
res = float(strnum)
result = math.sqrt(res)
v.set(result)
else:
pass
# 按键区域
frame_bord = Frame(width=400, height=350, bg='#cccccc')
button_del = Button(frame_bord, text='←', width=5, height=2, command=delete, bg='#aaaaaa').grid(row=0, column=0)
button_clear = Button(frame_bord, text='C', width=5, height=2, command=clear, bg='#aaaaaa').grid(row=0, column=1)
button_negation = Button(frame_bord, text='±', width=5, height=2, command=negation, bg='#aaaaaa').grid(row=0, column=2)
button_division_method = Button(frame_bord, text='/', width=5, height=2, command=lambda: operation('/'),
bg='#aaaaaa').grid(row=0, column=3)
button_7 = Button(frame_bord, text='7', width=5, height=2, command=lambda: change('7')).grid(row=1, column=0)
button_8 = Button(frame_bord, text='8', width=5, height=2, command=lambda: change('8')).grid(row=1, column=1)
button_9 = Button(frame_bord, text='9', width=5, height=2, command=lambda: change('9')).grid(row=1, column=2)
button_4 = Button(frame_bord, text='4', width=5, height=2, command=lambda: change('4')).grid(row=2, column=0)
button_5 = Button(frame_bord, text='5', width=5, height=2, command=lambda: change('5')).grid(row=2, column=1)
button_6 = Button(frame_bord, text='6', width=5, height=2, command=lambda: change('6')).grid(row=2, column=2)
button_multiplication = Button(frame_bord, text='*', width=5, height=2, command=lambda: operation('*'),
bg='#bbbbbb').grid(row=1, column=3)
button_1 = Button(frame_bord, text='1', width=5, height=2, command=lambda: change('1')).grid(row=3, column=0)
button_2 = Button(frame_bord, text='2', width=5, height=2, command=lambda: change('2')).grid(row=3, column=1)
button_3 = Button(frame_bord, text='3', width=5, height=2, command=lambda: change('3')).grid(row=3, column=2)
button_subtraction = Button(frame_bord, text='-', width=5, height=2, command=lambda: operation('-'), bg='#bbbbbb').grid(
row=2, column=3)
button_0 = Button(frame_bord, text='0', width=14, height=2, command=lambda: change('0')).grid(row=4, column=0,
columnspan=2)
button_decimal_point = Button(frame_bord, text='.', width=5, height=2, command=lambda: change('.')).grid(row=4,
column=2)
button_addition = Button(frame_bord, text='+', width=5, height=2, command=lambda: operation('+'), bg='#bbbbbb').grid(
row=3, column=3)
button_sqrt = Button(frame_bord, text='√', width=5, height=2, command=lambda: sqrt('√'), bg='#bbbbbb').grid(row=5,
column=0)
button_equal = Button(frame_bord, text='=', width=5, height=5, command=equal, bg='#bbbbbb').grid(row=4, column=3,
rowspan=2)
button_percent = Button(frame_bord, text='%', width=5, height=2, command=lambda: percent('%'), bg='#bbbbbb').grid(row=5,
column=1)
button_reciprocal = Button(frame_bord, text='1/x', width=5, height=2, command=lambda: reciprocal('1/x'),
bg='#bbbbbb').grid(row=5, column=2)
frame_bord.pack(padx=10, pady=10)
# 主循环
root.mainloop()
兄弟连学python
Python学习交流、资源共享群:563626388 QQ