Python方法调用问题

发布于 2024-10-20 03:22:19 字数 749 浏览 4 评论 0原文

所以我正在学习 python,并且在 Text 对象上调用 setText() 方法时似乎遇到了一致的问题。当我在交互式 IDLE GUI 中时,该过程工作正常,但是当我保存模块然后尝试运行它们时,我得到:

nonetype 对象没有属性setText

我是否需要为文本赋值分配某种返回类型?为什么从 IDLE 到保存的模块会有不同的行为?我搜索了该网站和 Python 文档,但无法找到任何内容。任何帮助将不胜感激。

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

编辑添加...

谢谢Geo,你的建议解决了问题。

什么区别?

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

现在我的问题是,... ... 和 ...

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

... 在返回某些内容或确保 message 对象具有支持某些功能的 type 方面有

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤芳又自赏 2024-10-27 03:22:19

也许 draw 方法没有返回任何内容。尝试将您的代码更改为:

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

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("")
旧人哭 2024-10-27 03:22:19

我不知道如何正确回答你的第二个问题..所以我就在这里作为答案。

第一个不起作用的原因是您将 Text.draw 的返回值分配给 message。由于它没有返回任何内容,因此消息为 None

在工作代码中,您分配 Text 类型的消息并初始化该对象。然后调用该对象的 draw 方法和 setText 方法。

在非工作代码中,您在新的 Text 对象上调用 draw 方法,然后将其返回值(即 NoneType)分配给消息。由于 None 没有 setText 方法,因此您会收到错误。

(抱歉,如果我混淆了 NoneType 和 None )

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文