Python 使用tkinter和tkinter构建计算器

Python 使用tkinter和tkinter构建计算器,python,tkinter,Python,Tkinter,我想用tkinter做一个计算器,我只是个初学者。 我想问题出在“=”按钮上,它就是不起作用,我也不知道为什么。现在我只想要从1到9的按钮,加、减和等于按钮。当我按下一个数字时,它会显示在条目中,但等于按钮不起作用。例如,如果我想做1+2,它会显示1,我会按加号按钮,然后按2,在条目中我仍然会看到2而不是3 from Tkinter import * import math class Calc(): def __init__(self): self.total=0 sel

我想用tkinter做一个计算器,我只是个初学者。 我想问题出在“=”按钮上,它就是不起作用,我也不知道为什么。现在我只想要从1到9的按钮,加、减和等于按钮。当我按下一个数字时,它会显示在条目中,但等于按钮不起作用。例如,如果我想做1+2,它会显示1,我会按加号按钮,然后按2,在条目中我仍然会看到2而不是3

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
    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

sum=Calc()
root=Tk()
root.geometry("345x270+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)

# it 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)

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

root.mainloop()

你可以这样做。我只显示了一个操作员和一个号码。你可以添加更多

#This is for python 3
import tkinter as tk         #Import Tkinter for python2   

from tkinter import *

root=tk.Tk()

X=StringVar()


def calculations():
    v=e1.get()
    val=eval(v)
    l=Label(root, text=str(val)).grid()

e1=Entry(root,textvariable=X)
e1.grid()

b1=Button(root, text="1", command=lambda:e1.insert(END,str(1))).grid()
bp=Button(root, text="+", command=lambda:e1.insert(END,str("+"))).grid()
b=Button(root, text="=", command=lambda: calculations()).grid()

root.mainloop() 

希望这对您有所帮助

也许您可以提供一些关于代码中实际发生的情况以及您期望发生的情况的信息。“它不工作”还不够你说的对,现在我只想要1到9的按钮,加,减和等于按钮。gui工作正常,当我按下一个数字时,它会显示在条目中,但equals按钮不工作。例如,如果我想执行1+2,它将显示1,然后按下加号按钮,然后按下2,在条目中,我仍然会看到2而不是3。
command=lambda:sum.calc_total
在任何时候都不会实际执行
calc_total
方法。您需要
command=lambda:sum.calc_total()
,或者只需要
command=sum.calc_total
,因为该方法不需要参数(这是在这种情况下需要lambda的常见原因)。