开发者

method call problem with Python

开发者 https://www.devze.com 2023-02-14 04:56 出处:网络
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 modu

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:

nonetype object has no attribute setText

D开发者_开发百科o 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?


Perhaps the draw method is not returning anything. Try changing your code to this:

message1 = Text(Point(50,50), "Click")
message1.draw(win)
message1.setText("")


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 Text and initialize the object. You then call the draw method of this object, and the setText method.

In the non-working code, you are calling the draw method on a new Text object, then assigning the return of that - that is, NoneType - to message. And since None has no setText method, you get an error.

(Sorry if I have mixed up NoneType and None there)

0

精彩评论

暂无评论...
验证码 换一张
取 消