So I'm working on a pretty basic program in which one of three "doors" is randomly selected. This choice is compared with the user's choice and the user wins if his choice is the same as the randomly selected one.
So basically, I want to re-set the text in the GUI based on whether the user is correct or not. There is a "setText" method in the graphics library that I'm using (comes with the textbook) that should do the trick but for some reason I keep getting this error:
label.setText("Congrats! You got it!")
AttributeError: 'NoneType' object has no attribute 'setText'
But I'm certain that setText is a method for the text class!
user_select = win.getMouse()
for i in range(n):
selection = door_list[randrange(0,3)]
user_select = win.getMouse()
label = Text(Point(5,9), "hello").draw(win)
if selection.clicked(user_select):
label.setText("Congrats! You got it!")
wins = wins + 1
elif not selection.clicked(user_select):
label.setText("Sorry, try again")
losses = losses + 1
print(wins, losses)
-
Which toolkit are you using? I believe that attribute error means that label == None, as in it's never getting set.Collin– Collin2011年03月04日 14:35:58 +00:00Commented Mar 4, 2011 at 14:35
-
First line of defense is to take the error message literally. Notice it says "'NoneType' object has no attribute..." rather than "'Label' object has no attribute...". In other words, it's not that the object is missing the attribute, it's that the object is None.Bryan Oakley– Bryan Oakley2011年03月04日 15:12:22 +00:00Commented Mar 4, 2011 at 15:12
2 Answers 2
I don't whether this is a complete fix, but you need to replace this:
label = Text(Point(5,9), "hello").draw(win)
with this:
label = Text(Point(5,9), "hello")
label.draw(win)
In your version, the Text object is created and drawn, but it's the return value of the draw function that gets assigned to label (and presumably, draw returns None).
Comments
Not knowing your textbook or the API, I would guess that the draw method of Text does not return the Text, so you'll need to break this out into two lines:
label = Text(Point(5,9), "hello")
label.draw(win)