Tkinter:计算

时间:2019-04-09 02:08:58

标签: python python-3.x tkinter

我正在Tkinter制作计算器,遇到了很多麻烦。我收到我不明白的错误。以下是相关的代码段:

def op(op):
    global operation
    operation = op

def calculate(operation):
    number =(eval(str(str(num1.get) + " " + operation + " " + str(num2.get))))
    messagebox.showinfo("Answer", str(number))

master = Tk()
master.title("")

#Create the first entry and title
num1text = Label(master, text="Number 1")
num1text.grid(row=0, column=1, padx=5)

num1 = Entry(master)
num1.grid(row = 1, column = 1, padx=5, pady=5)

#Create the first entry and title
num2text = Label(master, text="Number 2")
num2text.grid(row=0, column=2, padx=5)

num2 = Entry(master)
num2.grid(row = 1, column = 2, padx=5, pady=5)

#create the four operand buttons
butt1 = Button(text="+", command=lambda: op("+"))
butt1.grid(row=3, column=1, sticky="nesw",)

butt2 = Button(text="-", command=lambda: op("-"))
butt2.grid(row=4, column=1, sticky="nesw")

butt3 = Button(text="*", command=lambda: op("/"))
butt3.grid(row=3, column=2, sticky="nesw")

butt4 = Button(text="/", command=lambda: op("*"))
butt4.grid(row=4, column=2, sticky="nesw")

#create the submit button
submitbutt = Button(text="Submit", command=lambda: calculate(operation))
submitbutt.grid(row=5, column=1, columnspan=2, sticky="nesw")

我尝试了很多方法,有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

尝试读取条目时,必须使用带括号的num1.get()调用get方法:

number =(eval(str(str(num1.get()) + " " + operation + " " + str(num2.get()))))

否则它将返回对get方法的引用,但不返回值。