微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

pygame没有定义?

如何解决pygame没有定义?

每次运行代码时,我都会收到此错误

NameError: name 'pygame' is not defined

这是我的代码

    from tkinter import *
from tkinter import filedialog
from pygame import mixer
from pygame.locals import *

class MusicPlayer:
    def __init__(self,window ):
        window.geometry('320x100'); window.title('MNote Player'); window.resizable(0,0)
        Load = Button(window,text = 'Load',width = 10,font = ('Lucida Grande',14),command = self.load)
        Play = Button(window,text = 'Play',command = self.play)
        Pause = Button(window,text = 'Pause',command = self.pause)
        Stop = Button(window,text = 'Stop',command = self.stop)
        Load.place(x=0,y=20);Play.place(x=110,y=20);Pause.place(x=220,y=20);Stop.place(x=110,y=60) 
        self.music_file = False
        self.playing_state = False
    def load(self):
        self.music_file = filedialog.askopenfilename()
    def play(self):
        if self.music_file:
            pygame.mixer.init()
            pygame.mixer.music.load(self.music_file)
            pygame.mixer.music.play()
    def pause(self):
        if not self.playing_state:
            pygame.mixer.music.pause()
            self.playing_state=True
        else:
            pygame.mixer.music.unpause()
            self.playing_state = False
    def stop(self):
        pygame.mixer.music.stop()
root = Tk()
app= MusicPlayer(root)
root.mainloop()

我没有其他名为.py的{​​{1}}文件,并且我检查了许多类似的问题,但找不到答案。.我只是一个初学者,所以请不要苛刻在我身上!

解决方法

您没有导入$period = Carbon\CarbonPeriod::create(Carbon\Carbon::now()->startOfMonth(),Carbon\Carbon::now()->endOfMonth()); foreach($period as $date) { $dates[] = $date->format('d-m-Y'); } 。您,但是正在导入pygame。因此,请直接在您的代码中使用它(pygame.mixer等,而不是mixer.init())。

或者,将导入指令从更改为

pygame.mixer.init()

from pygame import mixer
,

添加

import pygame

然后运行代码。

,

您实际上并未导入pygame,请添加import pygame以使用整个模块。当您使用from pygame import mixerfrom pygame.locals import *时,如果子模块内部包含__init__,则将导入子模块;如果它们不包含__init__,则仅导入普通类或文件。

仅导入pygame会导入pygame模块,以便您可以调用它并激活该__init__或调用子模块。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。