自学python(07) 简易版计算器(还有一点点小bug未修复)

import tkinter
import tkinter.messagebox
import math
root = tkinter.Tk()
root.title('计算器')
root.minsize(300,430)


def demo():
    print('请注意倒车')
#设置总菜单
allmenu = tkinter.Menu()
#添加子菜单
file = tkinter.Menu(tearoff = 0)
#向子菜单中添加选项卡
file.add_command(label = '标准型',command = demo)
file.add_command(label = '历史记录',command = demo)
file.add_command(label = '统计信息',command = demo)
#添加分割线
file.add_separator()
file.add_command(label = '退出',command = demo)


#添加子菜单
edit = tkinter.Menu(tearoff = 0)
#向子菜单中添加选项卡
edit.add_command(label = '复制',command = demo)
edit.add_command(label = '粘贴',command = demo)
edit.add_command(label = '历史记录',command = demo)
#添加分割线
edit.add_separator()
edit.add_command(label = '退出',command = demo)




#添加子菜单
hep = tkinter.Menu(tearoff = 0)
#向子菜单中添加选项卡
hep.add_command(label = '查看帮助',command = demo)
hep.add_command(label = '关于计算器')
#添加分割线
hep.add_separator()
hep.add_cascade(label = '退出',command = demo)


#将子菜单添加入总菜单
allmenu.add_cascade(menu = file ,label = '查看')
allmenu.add_cascade(menu = edit ,label = '编辑')
allmenu.add_cascade(menu = hep , label = '帮助')
#摆放总菜单
root.configure(menu = allmenu)


#文本输入框
v = tkinter.StringVar()
v.set('0')


#显示label
label = tkinter.Label(textvariable = v , bg = 'white' , font = '黑体' , bd = 5 , anchor = 'se')
label.place(x = 20, y = 5 , width = 260 , height = 50)
#声明一个保存运算过程的容器
operationlist = []


#声明一个是否按下运算符号的变量
ispresssign = False
#声明一个是否按下清空键的变量
#ispressflag = False


#按下数字的函数
def pressnum(num):
    #全局化变量
    global ispresssign
    global operationlist


    #判断是否按下运算的符号
    if ispresssign == True :
        #将数字归零
        v.set('0')
        #重置运算符
        ispresssign = False


    #获取面板原有数据
    oldnum = v.get()
    #判断原数字是否为0
    if oldnum == '0' :
        #直接显示按下的数字
        v.set(num)


    #将原数字与当前数字拼和
    else:
        #判断是否为正负号
        if num == '±':
            if oldnum.startswith('-'):
                v.set(oldnum[1:])


            else:
                v.set('-' + oldnum)
        else:
            v.set(oldnum + num)
    #判断是否按下清空键
    if num == 'AC' :
        operationlist.clear()
        v.set('0')


#按下运算符号的函数


def presssign(sign):
    global ispresssign
    global operationlist


    #获取页面中的原有数字
    oldnum = v.get()
    #将页面原有数字保存到列表中
    operationlist.append(oldnum)
    #保存运算符号
    if  ispresssign == False:
        operationlist.append(sign)
        ispresssign = True
    else:
        operationlist[-1] = sign
        ispresssign = True




#按下等号的函数
def pressequal():
    global operationlist


    #获取界面中的数字
    oldnum = v.get()


    #将列表中的数字组合到一起
    operationlist.append(oldnum)


    #将列表中的步骤组合成字符串,使用eval运算
    result = eval(''.join(operationlist))
    #显示结果
    v.set(result)


    #清空运算列表
    operationlist.clear()






#输入数字
btn1 = tkinter.Button(text = 'AC',command = lambda : pressnum('AC'))
btn1.place(x = 20, y = 80, width = 50, height = 50)


btn2 = tkinter.Button(text = '±' , command = lambda : pressnum('±'))
btn2.place(x = 90, y = 80, width = 50, height = 50)


btn3 = tkinter.Button(text = '%' , command = lambda : presssign('%'))
btn3.place(x = 160, y = 80, width = 50, height = 50)


btn4 = tkinter.Button(text = '/' , command = lambda : presssign('/'))
btn4.place(x = 230, y = 80, width = 50, height = 50)


btn5 = tkinter.Button(text = 7 , command = lambda : pressnum('7'))
btn5.place(x = 20, y = 150, width = 50, height = 50)


btn6 = tkinter.Button(text = 8 , command = lambda : pressnum('8'))
btn6.place(x = 90, y = 150, width = 50, height = 50)


btn7 = tkinter.Button(text = 9 , command = lambda : pressnum('9'))
btn7.place(x = 160, y = 150, width = 50, height = 50)


btn8 = tkinter.Button(text = '*' , command = lambda : presssign('*'))
btn8.place(x = 230, y = 150, width = 50, height = 50)


btn9 = tkinter.Button(text = 4 , command = lambda : pressnum('4'))
btn9.place(x = 20, y = 220, width = 50, height = 50)


btn10 = tkinter.Button(text = 5 , command = lambda : pressnum('5'))
btn10.place(x = 90, y = 220, width = 50, height = 50)


btn11 = tkinter.Button(text = 6 , command = lambda : pressnum('6'))
btn11.place(x = 160, y = 220, width = 50, height = 50)


btn12 = tkinter.Button(text = '-' , command = lambda : presssign('-'))
btn12.place(x = 230, y = 220, width = 50, height = 50)


btn13 = tkinter.Button(text = 1 , command = lambda : pressnum('1'))
btn13.place(x = 20, y = 290, width = 50, height = 50)


btn14 = tkinter.Button(text = 2 , command = lambda : pressnum('2'))
btn14.place(x = 90, y = 290, width = 50, height = 50)


btn15 = tkinter.Button(text = 3 , command = lambda : pressnum('3'))
btn15.place(x = 160, y = 290, width = 50, height = 50)


btn16 = tkinter.Button(text = '+' , command = lambda : presssign('+'))
btn16.place(x = 230, y = 290, width = 50, height = 50)


btn17 = tkinter.Button(text = 0 , command = lambda : pressnum('0'))
btn17.place(x = 20, y = 360, width = 120, height = 50)


btn18 = tkinter.Button(text = '.' , command = lambda : pressnum('.'))
btn18.place(x = 160, y = 360, width = 50, height = 50)


btn19 = tkinter.Button(text = '=' , command = pressequal)
btn19.place(x = 230, y = 360, width = 50, height = 50)




root.mainloop()

你可能感兴趣的:(python简单函数)