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.getMous开发者_如何学Ce()
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)
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
).
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)
精彩评论