Python 属性错误:“模块”对象没有属性“init”;

发布于 2024-11-29 07:57:45 字数 392 浏览 1 评论 0原文

我正在运行一个简单的 python 文件:

from livewires import games

games.init(screen_width = 640, screen_height = 480, fps = 50)

games.screen.mainloop()

当我在 IDLE 中运行它时,我在标题中打印了错误。 我从一本书上复制并粘贴了这段代码。我处于初学者的早期阶段,所以我知道的不多。我相当确定我正确安装了 livewires,因为我已经使用其模块运行了其他程序。 (天哪,我知道这可能根本不是正确的术语,抱歉。)我并不是在寻找解决这个问题的方法,(尽管这也很好)我只是希望有人可以解释问题到底是什么。我从书上复制了代码,所以我不明白我做错了什么。我也 100% 肯定没有错别字。

I was running a simple python file:

from livewires import games

games.init(screen_width = 640, screen_height = 480, fps = 50)

games.screen.mainloop()

When I run this in IDLE, I get the error printed in the title.
I copy and pasted this code from a book. I'm at the early stages of a beginner, so I don't know much. I'm fairly sure that I installed livewires properly as i've already run other programs with its modules. (gosh, I know that that is probably not the right terminology at all, sorry.) I'm not exactly looking for a way to fix this, (although that'd be nice, too) I'm just hoping someone could explain exactly what the problem is. I copied the code from a book so I don't understand what I did wrong. I'm 100% positive that there are no typos, too.

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

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

发布评论

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

评论(4

还不是爱你 2024-12-06 07:57:45

好吧,搜索了一下,似乎这是 livewires 网站: Livewires 主页 其中一个可以下载他们的livewires包。

另外,一些代码:第 11 章 看起来像他在这里发布的内容(“披萨恐慌游戏”)。

下载并解压 livewires 可以看到 games 模块中确实没有 init 函数(正如评论中已经提到的)。

要么这本书有不起作用的例子(考虑到它解释这个例子的深度,这是值得怀疑的);要么或者livewires 包在本书编写时和现在之间发生了变化(更有可能)。

livewires 网站上还有其他示例,我认为您会取得更大的成功。
我建议你放弃这本书,因为你很可能会继续遇到这样的障碍。谷歌搜索会出现许多其他免费的Python教程,这些教程是最新的,并且更容易理解。

此页面有一些适合初学者的链接:初学者指南

Well, searching around, it seems that this is the livewires website: Livewires Home Page where one can download their livewires package.

Also, some code from this: Chapter 11 looks like what he's posted here ("The Pizza Panic Game").

Downloading and unpacking livewires shows that there is indeed no init function in the games module (as the comments already mentioned).

Either this book has non-working examples (doubtful, given the depth it explains this example with); or the livewires package has changed between when the book was written and now (much more likely).

The livewires website has other examples on it which I think you'll have much more success with.
I'd recommend you ditch the book, since it's likely you'll keep running into roadblocks like this. A Google search will turn up a number of other free python tutorials which are more up-to-date, and will be easier to follow.

This page has a few links for beginners: Beginners Guide

一场春暖 2024-12-06 07:57:45

可以从本书配套网站下载适用于“绝对初学者的 Python 编程”中的代码的旧版 livewires:

http://www.delmarlearning.com/companions/content/1435455002/downloads/index.asp?isbn=1435455002

An older version of livewires that works with the code from "Python Programming for the Absolute Beginner " can be downloaded from the book companion website :

http://www.delmarlearning.com/companions/content/1435455002/downloads/index.asp?isbn=1435455002

唯憾梦倾城 2024-12-06 07:57:45

我刚刚开始阅读同一本书《Python 编程第三版》。作者:Michael Dawson,并遇到了同样的问题。自这本书写成以来,情况已经发生了变化。

如果您仍然想创建窗口,这里是我使用的代码并且它可以工作:

from livewires import games

#This is the code M. Dawson has in the book
#games.init(screen_width = 640, screen_height = 480, fps = 50)
#games.screen.mainloop()

my_screen = games.Screen(width = 640, height = 480)
my_screen.mainloop(fps = 50)

屏幕现在被视为要实例化的对象。 Screen 类不再在其构造函数中接受 fps,现在将该 fps 传递到 mainloop 方法中。如果您没有将 fps 传递给 mainloop 方法,它将默认为 50。

I just started with the same book, Python Programming Third Ed. by Michael Dawson, and ran into the same problem. It has changed since the book was written.

If you still wanted to create the window here is the code that I used and it works:

from livewires import games

#This is the code M. Dawson has in the book
#games.init(screen_width = 640, screen_height = 480, fps = 50)
#games.screen.mainloop()

my_screen = games.Screen(width = 640, height = 480)
my_screen.mainloop(fps = 50)

The screen is now treated as an object to be instantiated. The Screen class no longer accepts fps in its constructor, now pass that fps into the mainloop method. If you do not pass fps into mainloop method, it will default to 50.

猥琐帝 2024-12-06 07:57:45

我遇到了同样的问题,然后我从他链接到的 slejdops 网站下载了代码。听起来它的创建者对这样的想法持开放态度,因为他们总是将这些东西与教科书一起锁定。这使得它既可以又合法。 :)

所以去这里吧。玩得开心并祝你好运:
http://www.delmarlearning.com/companions/content /1435455002/downloads/index.asp?isbn=1435455002

I had the same problem then I downloaded the code from slejdops' site he linked to. Sounds like the creator of it is open to the idea of getting it like this cause they always keep this stuff on lock down with text books. That makes it both ok and legal. :)

So go here. Have fun and good luck:
http://www.delmarlearning.com/companions/content/1435455002/downloads/index.asp?isbn=1435455002

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