0
我有init函数构造一个tkinter窗口。窗口内部有一个5 * 10的乘法和一个输入框。如果用户用正确的答案填充框,按下执行结果功能的按钮后,必须用正确的信息显示标签,否则显示错误信息的标签。问题是:Python Tkinter条目 - 获得值
即使答案是正确的,显示错误的信息。如果我在结果函数中打印self.content,则在终端中显示正确的值。但是如果结果函数语句存在问题。
从Tkinter的进口*
类道具:
def __init__(self):
self.root = Tk()
self.root.geometry("800x600")
self.x = 5
self.y = 10
self.title = Label(self.root, text = "TEST")
self.title.grid(row=0, column=0, columnspan=6)
self.title.config(font=("Courier", 30))
self.labelx = Label(self.root,text=self.x)
self.labelx.grid(row=2, column=2)
self.labelx.config(font=("Courier", 30))
self.epi = Label(self.root,text="X")
self.epi.grid(row=2, column=3)
self.epi.config(font=("Courier", 30))
self.labely = Label(self.root,text=self.y)
self.labely.grid(row=2, column=4)
self.labely.config(font=("Courier", 30))
self.total = Entry(self.root, font = "Courier 25 bold",justify="center",width=3)
self.total.grid(row=2, column=5, padx=20)
button = Button(self.root, text="OK", command = self.result)
button.grid(row=3, column=2)
self.content = Entry.get(self.total)
self.root.mainloop()
def result(self):
if self.content == (self.x * self.y):
self.labres = Label(self.root, text="Right")
self.labres.grid(row=2, column=6)
self.labres.config(font=("Courier", 30))
else:
self.labres = Label(self.root, text="Wrong")
self.labres.grid(row=2, column=6)
self.labres.config(font=("Courier", 30))
开始=道具()
如果我这样做:如果INT(self.content)我得到:ValueError异常:无效的字面INT()基数为10: – Athon
@Athon请确保'自我.content'不为空或不包含任何字母或符号。你需要在'int(self.content)'中使用它之前验证'self.content'' –
你能解释我以前的意思吗? – Athon