使用python连续添加计算器

时间:2018-04-17 13:00:15

标签: python tkinter

我使用python和tkinter构建了一个计算器。

我无法弄清楚如何让它注册多于两个数字,例如,如果我输入5 + 5 + 5,它将不会显示结果。但是,如果我做5 + 5,它将显示10。

from Tkinter import *
import math
class Calc():
def __init__(self):
    self.total=0
    self.current=""
    self.new_num=True
    self.op_pending=False
    self.op=""
    self.eq=False

def num_press(self,num):
    self.eq=False
    temp=e.get()
    temp2=str(num)
    if self.new_num:
        self.current=temp2
        self.new_num=False
    else:
        if temp2=='.':
            if temp2 in temp:
                return
        self.current=temp+temp2
    self.display(self.current)

def calc_total(self):
    self.eq=True
    self.current=float(self.current)
    if self.op_pending==True:
        self.do_sum()
    else:
        self.total=float(e.get())

def display(self,value):
    e.delete(0,END)
    e.insert(0,value)

def do_sum(self):
    if self.op=="add":
        self.total+=self.current
    if self.op=="minus":
        self.total-=self.current
    if self.op=="multi":
        self.total*=self.current
    if self.op=="div":
        self.total/=self.current
    self.new_num=True
    self.op_pending=False
    self.display(self.total)

def operation(self,op):
    self.current=float(self.current)
    if self.op_pending:
        self.do_sum()
    elif not self.eq:
        self.total=self.current
        self.new_num=True
        self.op_pending=True
        self.op=op
        self.eq=False

def clear(self):
    self.total=0
    self.display(self.total)

sum=Calc()
root=Tk()
root.geometry("410x270+200+200")
e = Entry(width=20)
e.grid(row=0,column=4)

b1= Button(text="1",height=4,width=8,command=lambda:sum.num_press(1)).grid(row=0,column=0)

b2= Button(text="2",height=4,width=8,command=lambda:sum.num_press(2)).grid(row=0,column=1)

#goes up to b9                                                                                                                                                           

equals= Button(text="=",height=4,width=8,command=lambda:sum.calc_total()).grid(row=3,column=2)

add= Button(text="+",height=4,width=8,command=lambda: sum.operation("add")).grid(row=3,column=1)

minus= Button(text="-",height=4,width=8,command=lambda: sum.operation("minus")).grid(row=3,column=0)

multi= Button(text="*",height=4,width=8,command=lambda: sum.operation("multi")).grid(row=1,column=3)

div= Button(text="/",height=4,width=8,command=lambda: sum.operation("div")).grid(row=2,column=3) 

clear=Button(text="AC",height=4,width=8,command=lambda:sum.clear()).grid(row=3,column=3)

root.mainloop()

1 个答案:

答案 0 :(得分:2)

要创建一个不仅可以计算2个值的计算器,我建议使用Shunting Yard算法。有了这个,你可以做出更复杂的计算,相对容易。

伪代码来源:Wikipedia

while there are tokens to be read:
    read a token.
    if the token is a number, then:
        push it to the output queue.
    if the token is a function then:
        push it onto the operator stack 
    if the token is an operator, then:
        while ((there is a function at the top of the operator stack)
               or (there is an operator at the top of the operator stack with greater precedence)
               or (the operator at the top of the operator stack has equal precedence and is left associative))
              and (the operator at the top of the operator stack is not a left bracket):
            pop operators from the operator stack onto the output queue.
        push it onto the operator stack.
    if the token is a left bracket (i.e. "("), then:
        push it onto the operator stack.
    if the token is a right bracket (i.e. ")"), then:
        while the operator at the top of the operator stack is not a left bracket:
            pop the operator from the operator stack onto the output queue.
        pop the left bracket from the stack.
        /* if the stack runs out without finding a left bracket, then there are mismatched parentheses. */
if there are no more tokens to read:
    while there are still operator tokens on the stack:
        /* if the operator token on the top of the stack is a bracket, then there are mismatched parentheses. */
        pop the operator from the operator stack onto the output queue.
exit.

使用此输出计算答案:

  1. 从ops堆栈中取一个操作员,并根据操作员
  2. 从操作数堆栈中取出一个或两个元素,应用运算符
  3. 推送结果并返回1.直到完成。