zoukankan      html  css  js  c++  java
  • python ——计算器

    from tkinter import *
    import parser
    from math import factorial
    
    root = Tk()
    root.title("计算器")
    
    # i keeps the track of current position on the input text field
    i = 0
    # Receives the digit as parameter and display it on the input field
    def get_variables(num):
        global i
        display.insert(i,num)
        i+=1
    
    def clear_all():
        display.delete(0,END)
    
    #add the input field
    display = Entry(root)
    display.grid(row=1,columnspan=6,sticky=N+E+S+W)
    
    def get_operation(operator):
        global i
        length = len(operator)
        display.insert(i,operator)
        i+=length
    
    #删除一个
    def undo():
        entire_string = display.get()
        if len(entire_string):
            new_string = entire_string[:-1]
            clear_all()
            display.insert(0,new_string)
        else:
            clear_all()
            display.insert(0,"Error")
    
    def calculate():
        entire_string = display.get()
        try:
            a = parser.expr(entire_string).compile()
            result = eval(a)
            clear_all()
            display.insert(0,result)
        except Exception:
            clear_all()
            display.insert(0,"Error")
    
    def undo():
        entire_string = display.get()
        if len(entire_string):
            new_string = entire_string[:-1]
            clear_all()
            display.insert(0,new_string)
        else:
            clear_all()
            display.insert(0,"Error")
    
    #Code to add buttons to the Calculator
    #这里click函数就与button这个插件关联了,但是不好的地方就是不能传递参数。tkinter要求由按钮(或者其它的插件)
    # 触发的控制器函数不能含有参数,
    # 目的就是为了以统一的方式去调用他们。那么我们有什么办法给函数传递参数,因为这经常会用到。答案就是使用lambda。
    Button(root,text="1",command=lambda: get_variables(1)).grid(row=2,column=0,sticky=N+S+E+W)
    Button(root,text="2",command=lambda: get_variables(2)).grid(row=2,column=1,sticky=N+S+E+W)
    Button(root,text="3",command=lambda: get_variables(3)).grid(row=2,column=2,sticky=N+S+E+W)
    
    Button(root,text="4",command=lambda : get_variables(4)).grid(row=3,column=0,sticky=N+S+E+W)
    Button(root,text="5",command=lambda: get_variables(5)).grid(row=3,column=1,sticky=N+S+E+W)
    Button(root,text="6",command=lambda: get_variables(6)).grid(row=3,column=2,sticky=N+S+E+W)
    
    Button(root,text="7",command=lambda : get_variables(7)).grid(row=4,column=0,sticky=N+S+E+W)
    Button(root,text="8",command=lambda: get_variables(8)).grid(row=4,column=1,sticky=N+S+E+W)
    Button(root,text="9",command=lambda: get_variables(9)).grid(row=4,column=2,sticky=N+S+E+W)
    
    #添加清除所有按钮
    Button(root,text="AC",command=lambda: clear_all()).grid(row=5,column=0,sticky=N+S+E+W)
    Button(root,text="0",command=lambda: get_variables(0)).grid(row=5,column=1,sticky=N+S+E+W)
    Button(root,text=".",command=lambda: get_variables(".")).grid(row=5,column=2,sticky=N+S+E+W)
    
    
    Button(root,text="+",command= lambda :get_variables("+")).grid(row=2,column=3, sticky=N+S+E+W)
    Button(root,text="-",command= lambda :get_operation("-")).grid(row=3,column=3, sticky=N+S+E+W)
    Button(root,text="*",command= lambda :get_operation("*")).grid(row=4,column=3, sticky=N+S+E+W)
    Button(root,text="/",command= lambda :get_operation("/")).grid(row=5,column=3, sticky=N+S+E+W)
    
    # adding new operations
    Button(root,text="pi",command= lambda :get_operation("*3.14")).grid(row=2,column=4, sticky=N+S+E+W)
    Button(root,text="%",command= lambda :get_operation("%")).grid(row=3,column=4, sticky=N+S+E+W)
    Button(root,text="(",command= lambda :get_operation("(")).grid(row=4,column=4, sticky=N+S+E+W)
    Button(root,text="exp",command= lambda :get_operation("**")).grid(row=5,column=4, sticky=N+S+E+W)
     
    Button(root,text="<-",command= lambda :undo()).grid(row=2,column=5, sticky=N+S+E+W)
    Button(root,text="x!", command= lambda: fact()).grid(row=3,column=5, sticky=N+S+E+W)
    Button(root,text=")",command= lambda :get_operation(")")).grid(row=4,column=5, sticky=N+S+E+W)
    Button(root,text="^2",command= lambda :get_operation("**2")).grid(row=5,column=5, sticky=N+S+E+W)
    Button(root,text="^2",command= lambda :get_operation("**2")).grid(row=5,column=5, sticky=N+S+E+W)
    Button(root,text="=",command= lambda :calculate()).grid(columnspan=6, sticky=N+S+E+W)
    
    root.mainloop()
  • 相关阅读:
    用代码关闭冰刃(IceSword)
    .h和.cpp文件的区别
    获取其他进程的命令行(ReadProcessMemory其它进程的PPROCESS_PARAMETERS和PEB结构体)
    C#调用记事本并填写内容
    C#中比较两个对象的地址是否相同(也是引用计数的问题,和Java一样)
    JS代码的几个注意点规范
    javascript常用知识点集
    网站静态化处理—满足静态化的前后端分离(9)
    网站静态化处理—前后端分离—下(8)
    JS对文档进行操作
  • 原文地址:https://www.cnblogs.com/shunguo/p/14563166.html
Copyright © 2011-2022 走看看