鱼C论坛

 找回密码
 立即注册
查看: 1309|回复: 1

[已解决]pygame的mixer报错

[复制链接]
发表于 2020-7-28 08:24:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
代码如下:
# 曲速飞跃作
from tkinter import *
import pygame
import sys
from pygame.locals import *

do_sound = pygame.mixer.Sound("do.ogg")
ri_sound = pygame.mixer.Sound("ri.ogg")
mi_sound = pygame.mixer.Sound("mi.ogg")
fa_sound = pygame.mixer.Sound("fa.ogg")

bg_size = width, height = 3, 2
screen = pygame.display .set_mode(bg_size)
pygame.display.set_caption("MUSIC")

pygame.init()

root = Tk()


def pd():
    do_sound.play()


def pr():
    ri_sound.play()


def pm():
    mi_sound.play()


def pf():
    fa_sound.play()


Button(root, text="do", width=10, command=pd)\
    .grid(row=3, column=0, padx=10, pady=5)
Button(root, text="ri", width=10, command=pr)\
    .grid(row=3, column=1, padx=10, pady=5)
Button(root, text="mi", width=10, command=pm)\
    .grid(row=3, column=2, padx=10, pady=5)
Button(root, text="fa", width=10, command=pf)\
    .grid(row=3, column=3, padx=10, pady=5)

pygame.init()
mainloop()

错误如下:
Traceback (most recent call last):
  File "D:\my.code\piano\piano.py", line 7, in <module>
    do = pygame.mixer.music.load("do.ogg")
pygame.error: mixer not initialized
最佳答案
2020-7-28 08:27:43



初始化下音乐播放器,代码前面加上:pygame.mixer.init()
# 曲速飞跃作
from tkinter import *
import pygame
import sys
from pygame.locals import *

pygame.mixer.init()

do_sound = pygame.mixer.Sound("do.ogg")
ri_sound = pygame.mixer.Sound("ri.ogg")
mi_sound = pygame.mixer.Sound("mi.ogg")
fa_sound = pygame.mixer.Sound("fa.ogg")

bg_size = width, height = 3, 2
screen = pygame.display .set_mode(bg_size)
pygame.display.set_caption("MUSIC")

pygame.init()

root = Tk()


def pd():
    do_sound.play()


def pr():
    ri_sound.play()


def pm():
    mi_sound.play()


def pf():
    fa_sound.play()


Button(root, text="do", width=10, command=pd)\
    .grid(row=3, column=0, padx=10, pady=5)
Button(root, text="ri", width=10, command=pr)\
    .grid(row=3, column=1, padx=10, pady=5)
Button(root, text="mi", width=10, command=pm)\
    .grid(row=3, column=2, padx=10, pady=5)
Button(root, text="fa", width=10, command=pf)\
    .grid(row=3, column=3, padx=10, pady=5)

pygame.init()
mainloop()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-7-28 08:27:43 | 显示全部楼层    本楼为最佳答案   



初始化下音乐播放器,代码前面加上:pygame.mixer.init()
# 曲速飞跃作
from tkinter import *
import pygame
import sys
from pygame.locals import *

pygame.mixer.init()

do_sound = pygame.mixer.Sound("do.ogg")
ri_sound = pygame.mixer.Sound("ri.ogg")
mi_sound = pygame.mixer.Sound("mi.ogg")
fa_sound = pygame.mixer.Sound("fa.ogg")

bg_size = width, height = 3, 2
screen = pygame.display .set_mode(bg_size)
pygame.display.set_caption("MUSIC")

pygame.init()

root = Tk()


def pd():
    do_sound.play()


def pr():
    ri_sound.play()


def pm():
    mi_sound.play()


def pf():
    fa_sound.play()


Button(root, text="do", width=10, command=pd)\
    .grid(row=3, column=0, padx=10, pady=5)
Button(root, text="ri", width=10, command=pr)\
    .grid(row=3, column=1, padx=10, pady=5)
Button(root, text="mi", width=10, command=pm)\
    .grid(row=3, column=2, padx=10, pady=5)
Button(root, text="fa", width=10, command=pf)\
    .grid(row=3, column=3, padx=10, pady=5)

pygame.init()
mainloop()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-1-19 17:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表