我希望它能够将具有不同价格的不同类型添加到一个简单的GUI中,但我不断收到此回调错误 我是从前不久的一个简单的计算器复制代码,但不知道有什么不同,stringvar是否有干扰之处?
from tkinter import *
def price():
num1 = eval(txtFirst())
first = num1 * 22.75
num2 = eval(txtSecond())
second = num2 * 100
num3 = eval(txtThird())
third = num3 * 20.50
num4 = eval(txtFourth())
fourth = num4 * 90
num5 = eval(txtFifth())
fifth = num5 * 25.50
num6 = eval(txtSixth())
sixth = num6 * 125.50
sum = first + second + third + fourth + fifth + sixth
cost.set("Sum: " + str(sum))
window = Tk()
window.title("Chicken Feed")
window.geometry("500x500")
Label(window, text="Feed\nType:").grid(row=0, column=0)
Label(window, text="Amount:").grid(row=0,column=1)
Label(window, text="Feed\nCost:").grid(row=0, column=2)
Label(window, text="10kg Pellets:").grid(row=1,column=0)
Label(window, text="50kg Pellets:").grid(row=2,column=0)
Label(window, text="10kg Mash:").grid(row=3,column=0)
Label(window, text="50kg Mash:").grid(row=4,column=0)
Label(window, text="10kg Enhanced:").grid(row=5,column=0)
Label(window, text="50kg Pellets:").grid(row=6,column=0)
cost = StringVar()
cost = Entry(window, state='readonly', width=20, textvariable=cost)
cost.grid(row=1, column=2, columnspan=3, padx=20, pady=6)
txtFirst = StringVar()
entFirst = Entry(window, width=5, textvariable=txtFirst)
entFirst.grid(row=1, column=1)
txtSecond = StringVar()
entSecond = Entry(window, width=5, textvariable=txtSecond)
entSecond.grid(row=2, column=1)
txtThird = StringVar()
entThird = Entry(window, width=5, textvariable=txtThird)
entThird.grid(row=3, column=1)
txtFourth = StringVar()
entFourth = Entry(window, width=5, textvariable=txtFourth)
entFourth.grid(row=4, column=1)
txtFifth = StringVar()
entFifth = Entry(window, width=5, textvariable=txtFifth)
entFifth.grid(row=5, column=1)
txtSixth = StringVar()
entSixth = Entry(window, width=5, textvariable=txtSixth)
entSixth.grid(row=6, column=1)
btnAdd = Button(window, text="Calculate", width=3, command=price)
btnAdd.grid(row=2, column=2, padx=20)
window.mainloop()
在输入小部件中输入数字后,我仍然收到错误消息:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Luke\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:\Users\Luke\Desktop\stage2.py", line 4, in price
num1 = eval(txtFirst())
TypeError: 'StringVar' object is not callable
答案 0 :(得分:1)
我认为您想做的是返回txtFirst
的值。请勿为此使用eval()
。要获取值:
num1 = txtFirst.get()