需要了解python中的此错误文本的帮助:
好的,所以基本上我写了一个不太漂亮的GUI,它提供随机的简单数学问题。它就像我想要的那样工作。但是,每当我单击enter时,空闲外壳就会向我吐出红色。尽管如此,正如我所说,它仍能继续发挥我所希望的作用。因此,我很难理解为什么我的代码的这一特定部分会导致问题。为代码的冗长部分道歉:需要了解python中的此错误文本的帮助:,python,tkinter,traceback,Python,Tkinter,Traceback,好的,所以基本上我写了一个不太漂亮的GUI,它提供随机的简单数学问题。它就像我想要的那样工作。但是,每当我单击enter时,空闲外壳就会向我吐出红色。尽管如此,正如我所说,它仍能继续发挥我所希望的作用。因此,我很难理解为什么我的代码的这一特定部分会导致问题。为代码的冗长部分道歉: from tkinter import Label, Frame, Entry, Button, LEFT, RIGHT, END from tkinter.messagebox import showinfo imp
from tkinter import Label, Frame, Entry, Button, LEFT, RIGHT, END
from tkinter.messagebox import showinfo
import random
class Ed(Frame):
'Simple arithmetic education app'
def __init__(self,parent=None):
'constructor'
Frame.__init__(self, parent)
self.pack()
self.tries = 0
Ed.make_widgets(self)
Ed.new_problem(self)
def make_widgets(self):
'defines Ed widgets'
self.entry1 = Entry(self, width=20, bg="gray", fg ="black")
self.entry1.grid(row=0, column=0, columnspan=4)
self.entry1.pack(side = LEFT)
self.entry2 = Entry(self, width=20, bg="gray", fg ="black")
self.entry2.grid(row=2, column=2, columnspan=4)
self.entry2.pack(side = LEFT)
Button(self,text="ENTER", command=self.evaluate).pack(side = RIGHT)
def new_problem(self):
'creates new arithmetic problem'
opList = ["+", "-"]
a = random.randrange(10+1)
b = random.randrange(10+1)
op = random.choice(opList)
if b > a:
op = "+"
self.entry1.insert(END, a)
self.entry1.insert(END, op)
self.entry1.insert(END, b)
def evaluate(self):
'handles button "Enter" clicks by comparing answer in entry to correct result'
result = eval(self.entry1.get())
if result == eval(self.entry2.get()):
self.tries += 1
showinfo(title="Huzzah!", message="That's correct! Way to go! You got it in {} tries.".format(self.tries))
self.entry1.delete(0, END)
self.entry2.delete(0, END)
self.entry1.insert(END, self.new_problem())
else:
self.tries += 1
self.entry2.delete(0, END)
这是我得到的信息:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python32\lib\tkinter\__init__.py", line 1399, in __call__
return self.func(*args)
File "C:\Python32\Python shit\csc242hw4\csc242hw4.py", line 55, in evaluate
self.entry1.insert(END, self.new_problem())
File "C:\Python32\lib\tkinter\__init__.py", line 2385, in insert
self.tk.call(self._w, 'insert', index, string)
_tkinter.TclError: wrong # args: should be ".52882960.52883240 insert index text"
错误消息说Tkinter认为它获取了错误的args数 再回溯一下,我们看到这一行导致了错误:
File "C:\Python32\Python shit\csc242hw4\csc242hw4.py", line 55, in evaluate
self.entry1.insert(END, self.new_problem())
但这似乎是正确的论据数量!发生了什么事
问题是self.new\u problem()
返回None
。当参数为None
时,Python的Tkinter包装器似乎不会传递参数
要解决此问题,请取消对insert
的调用,并将第55行更改为
self.new_problem()
这是因为您已经在调用
self.entry.insert
从内部新问题
太棒了!你是对的,我太专注于使用insert语句了,以至于忽略了我在以前的函数中已经在这样做的事实,所以我需要做的就是调用它。非常感谢您花时间查看我的代码!注意:不要使用eval()从字符串中获取值。如果需要整数,请使用int()。如果您希望使用float,请使用float()。