So I’m learning python, and I seem to be having a consistent problem with calling setText() methods on Text objects. The process works fine when I’m in the interactive IDLE GUI, but when I save modules and then try to run them, I get:
nonetypeobject has no attributesetText
Do I need to assign some sort of return type to the text assignment? Why would there be different behavior from IDLE to saved modules? I’ve searched the site and Python documentation and was unable to turn up anything. Any help would be much appreciated.
message1 = Text(Point(50,50), "Click).draw(win)
message1.setText("")
Edited to add…
Thanks Geo, your suggestion fixed things.
Now my question is, what’s the difference between…
message = Text(Point(50,50), "Click").draw(win)
… and …
message = Text(Point(50,50), "Click")
message.draw(win)
… with regards to returning something, or ensuring that the message object has a type that supports certain functions?
I’m not sure how to answer your second question properly..so I’ll just do it as an answer here.
The reason the first does not work is because you are assigning the return value of Text.draw to message. Since it returns nothing, then message is
None.In the working code, you assign message with the type
Textand initialize the object. You then call thedrawmethod of this object, and thesetTextmethod.In the non-working code, you are calling the
drawmethod on a newTextobject, then assigning the return of that – that is, NoneType – to message. And sinceNonehas no setText method, you get an error.(Sorry if I have mixed up NoneType and None there)