利用python中的tkinter模块,主要模仿win7系统自带的标准型计算器,实现基本运算功能,运行程序后如下图所示:
1.小数点可以按很多次问题没有解决
2.若有浮点类型数计算精度问题未解决
3.一些未知小BUG,需调试完善的地方还很多
以下是具体代码,面向过程开发,程序很小面向对象的写法也很简单,这里不再给出(代码在linux系统下的pycharm中书写)
import tkinter
import tkinter.messagebox
import math
root = tkinter.Tk()
root.title('银河超级计算器')
root.minsize(350,400)
# 设置菜单
# 设置总菜单
allmenu = tkinter.Menu()
# 添加子菜单
look = tkinter.Menu(tearoff = 0) # 查看
edit = tkinter.Menu(tearoff = 0) # 编辑
help = tkinter.Menu(tearoff = 0) # 帮助
auther = tkinter.Menu(tearoff = 0)# 作者
'''
向子菜单中添加选项卡未作!!!!
'''
# 将子菜单加入总菜单
allmenu.add_cascade(menu = look, label = '查看')
allmenu.add_cascade(menu = edit, label = '编辑')
allmenu.add_cascade(menu = help, label = '帮助')
allmenu.add_cascade(menu = auther, label = '作者')
# 摆放总菜单
root.config(menu = allmenu)
# 文本输入框
v = tkinter.StringVar()
v.set('0')
label = tkinter.Label(textvariable = v, bg = 'white',font =('黑体',20),
bd = 5, anchor = 'se')
label.place(x = 30, y = 15, width = 290, height = 60)
# 声明一个用于保存运算过程的容器
operationlist = []
# 声明一个是否按下运算符号的变量
ispresssign = False
# 声明一个是否按下等于键的变量
ispressflag = False
# 声明一个是否允许按下'.'键的变量
isdot = True
# 按下数字的函数
def pressnum(num):
# 全局化变量
global ispresssign
global operationlist
global ispressflag
global isdot
# 判断是否按下等于键
if ispressflag == True:
# 将数字归0
v.set('0')
# 重置判断符
ispressflag = False
# 判断是否按下运算的符号
if ispresssign == True:
# 将数字归0
v.set('0')
# 重置运算符
ispresssign = False
# 获取面板原有数字
oldnum = v.get()
# 判断原数字是否为0
if oldnum == '0': # 直接显示按下的数字
if num == '.' and isdot == True:
v.set('0' + '.')
isdot = False
else:
v.set(num)
# 判断是否按下1/x键
if num == '1/x':
if oldnum == '0':
v.set('除数不能为0')
else:
result = 1 / float(oldnum)
v.set(result)
# 判断是否按下撤销键( ← 键)
if num == '←':
operationlist.clear()
v.set('0')
else: # 将原数字与当前数字拼和
# 判断是否按下正负号
if num == '±':
if oldnum.startswith('-'):
v.set(oldnum[1:])
else:
v.set('-' + oldnum)
else:
v.set(oldnum + num)
# 判断是否按下撤销键( ← 键)
if num == '←':
if len(oldnum) == 1:
v.set('0')
else:
v.set(oldnum[:-1])
# 判断是否按下根号键
if num == '√':
result = math.sqrt(float(oldnum))
v.set(result)
# 判断是否按下了清空键 (C 或者CE键)
if num == 'C' or num == 'CE':
operationlist.clear()
v.set('0')
# 按下运算符号的函数
def presssign(sign):
# 全局化变量
global ispresssign
global operationlist
global isdot
if ispresssign == True:
operationlist[-1] = sign
else:
# 获取页面中的原有数字
oldnum = v.get()
# 将页面原有数字保存到列表中
operationlist.append(oldnum)
# 保存运算符号
operationlist.append(sign)
# 设置按下运算符的标志
ispresssign = True
isdot = True
# 按下等号的函数
def pressequal():
# 全局化
global operationlist
global ispressflag
# 获取界面中的数字
oldnum = v.get()
if oldnum == '0' and '/' in operationlist:
operationlist.clear()
v.set('除数不能为0')
ispressflag = True
else:
# 将列表中的运算组合到一起
operationlist.append(oldnum)
#print(operationlist)
# 将列表中的步骤组合成字符串使用eval运算
result = eval(''.join(operationlist))
# 显示结果
v.set(result)
# 清空运算列表
operationlist.clear()
ispressflag = True
# 按钮
btnchexiao = tkinter.Button(text = '←', command = lambda : pressnum('←'))
btnchexiao.place(x = 30, y = 90, width = 50, height = 40)
btnCE = tkinter.Button(text = 'CE', command = lambda : pressnum('C'))
btnCE.place(x = 90, y = 90, width = 50, height = 40 )
btnC = tkinter.Button(text = 'C', command = lambda :pressnum('CE'))
btnC.place(x = 150, y = 90, width = 50, height = 40 )
btnflag = tkinter.Button(text = '±', command = lambda : pressnum('±'))
btnflag.place(x = 210, y = 90, width = 50, height = 40 )
btngen = tkinter.Button(text = '√', command = lambda : pressnum('√'))
btngen.place(x = 270, y = 90, width = 50, height = 40 )
btn7 = tkinter.Button(text = '7', command = lambda : pressnum('7'))
btn7.place(x = 30, y = 140, width = 50, height = 40 )
btn8 = tkinter.Button(text = '8', command = lambda : pressnum('8'))
btn8.place(x = 90, y = 140, width = 50, height = 40 )
btn9 = tkinter.Button(text = '9', command = lambda : pressnum('9'))
btn9.place(x = 150, y = 140, width = 50, height = 40 )
btnchu = tkinter.Button(text = '/',command = lambda : presssign('/'))
btnchu.place(x = 210, y = 140, width = 50, height = 40 )
btnyu = tkinter.Button(text = '%', command = lambda : presssign('%'))
btnyu.place(x = 270, y = 140, width = 50, height = 40 )
btn4 = tkinter.Button(text = '4', command = lambda : pressnum('4'))
btn4.place(x = 30, y = 190, width = 50, height = 40 )
btn5 = tkinter.Button(text = '5', command = lambda : pressnum('5'))
btn5.place(x = 90, y = 190, width = 50, height = 40 )
btn6 = tkinter.Button(text = '6', command = lambda : pressnum('6'))
btn6.place(x = 150, y = 190, width = 50, height = 40 )
btncheng = tkinter.Button(text = '*', command = lambda : presssign('*'))
btncheng.place(x = 210, y = 190, width = 50, height = 40 )
btndao = tkinter.Button(text = '1/x', command = lambda : pressnum('1/x'))
btndao.place(x = 270, y = 190, width = 50, height = 40 )
btn1 = tkinter.Button(text = '1', command = lambda : pressnum('1'))
btn1.place(x = 30, y = 240, width = 50, height = 40 )
btn2 = tkinter.Button(text = '2', command = lambda : pressnum('2'))
btn2.place(x = 90, y = 240, width = 50, height = 40 )
btn3 = tkinter.Button(text = '3', command = lambda : pressnum('3'))
btn3.place(x = 150, y = 240, width = 50, height = 40 )
btnjian = tkinter.Button(text = '-', command = lambda : presssign('-'))
btnjian.place(x = 210, y = 240, width = 50, height = 40 )
btndeng = tkinter.Button(text = '=', command = pressequal)
btndeng.place(x = 270, y = 240, width = 50, height = 90 )
btnling = tkinter.Button(text = '0', command = lambda : pressnum('0'))
btnling.place(x = 30, y = 290, width = 110, height = 40 )
btndot = tkinter.Button(text = '.', command = lambda : pressnum('.'))
btndot.place(x = 150, y = 290, width = 50, height = 40 )
btnjia = tkinter.Button(text = '+', command = lambda : presssign('+'))
btnjia.place(x = 210, y = 290, width = 50, height = 40 )
# 绑定查看源代码事件
def look():
tkinter.messagebox.showinfo(title = '嘿嘿嘿~~',message = '想得美,滚蛋滚蛋!!')
btnlook = tkinter.Button(text = '点击查看源代码', bg = 'black', fg = 'white', bd = 5, command = look)
btnlook.place(x = 90, y = 350, width = 170, height = 30)
root.mainloop()