开开心心happyheart 2023-01-05 20:31 采纳率: 81.8%
浏览 26
已结题

pygame—mixer错误

最近和我亲爱的同学一起搞一个python项目。
为了帮他改代码,于是学了pygame——mixer(以前是自学python)。
可是我们遇到了一个一个报错,我没看懂!
报错提示

AttributeError: 'Sound' object has no attribute 'pause'
下面是完整报错提示。

Exception in thread Thread-2:
Traceback (most recent call last):
  File "C:\Program Files\Python37\lib\threading.py", line 926, in _bootstrap_inner
    self.run()
  File "C:\Program Files\Python37\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:/Users/段禹珩/Desktop/桌宠2023新年/main.py", line 41, in window
    Carrot(Carrot_time=5)   #运行“播放声音”的函数,播放时长:5s
  File "C:/Users/段禹珩/Desktop/桌宠2023新年/main.py", line 17, in Carrot
    Carrot.pause()    #停止播放声音“Carrot”
AttributeError: 'Sound' object has no attribute 'pause'

我用百度翻译了一下

“Sound”对象没有“unpause”属性
我根本没看懂
报错的代码

    def Carrot (Carrot_time):   #将声音代码放入函数里面,既省去了不必要的报错,又省去了不必要的麻烦
        Carrot = pygame.mixer.Sound("兔子.胡萝卜.mp3")  # 进入声音,并命名为“Carrot”
        Carrot.play()   #播放声音“Carrot”
        time.sleep(Carrot_time) #设置播放时长(变量:“Carrot_time”)
        Carrot.pause()    #停止播放声音“Carrot”


Carrot(Carrot_time=5)   #运行“播放声音”的函数,播放时长:5s

完整代码

import pygame, sys, time, threading, os, signal    #引库”pygame“、”sys“、”time“、“threading”、“os”、“signal”
from pygame.constants import *  #引库“pygame——按键检测”的所有部分

def window ():   #创建函数:主程序(为多线程用),命名为“window”

    pygame.init()  # 初始化pygame
    pygame.mixer.init() #初始化pygame——mixer

    list = []  # 创建列表,命名为“list”
    pygame.display.set_caption("桌面宠物2023春节限定")  # 设置窗口标题
    screen = pygame.display.set_mode((300, 300))  # 设置窗口大小

    def Carrot (Carrot_time):   #将声音代码放入函数里面,既省去了不必要的报错,又省去了不必要的麻烦
        Carrot = pygame.mixer.Sound("兔子.胡萝卜.mp3")  # 进入声音,并命名为“Carrot”
        Carrot.play()   #播放声音“Carrot”
        time.sleep(Carrot_time) #设置播放时长(变量:“Carrot_time”)
        Carrot.pause()    #停止播放声音“Carrot”

    myfont = pygame.font.Font(None, 60)  # 设置字体大小

    bg = pygame.image.load("bg.jpg")  # 加载图片“bg.jpg“命名为”bg“
    mouth_open = pygame.image.load("mouth_open.png")  # 加载图片“mouth_open.png”并命名为”mouth_open“
    mouth_close = pygame.image.load("mouth_close.png")  # 加载图片“mouth_close.png”并命名为”mouth_close“
    screen.blit(bg, (0, 0))  # 显示背景图片“bg.jpg”
    screen.blit(mouth_close, (0, 0))  # 显示背景图片“mouth_close.png”
    pygame.display.update()  # 更新pygame窗口

    while True: #开启循环

        for event in pygame.event.get():    #开启键盘监测

            if event.type == QUIT:   #如果点击窗口的X
                # pygame.quit()   #卸载pygame模块
                os.getpid()  #获取Python自身运行的父进程
                os.kill(os.getpid(), signal.SIGTERM)  #终止自身进程

            elif event.type == KEYDOWN:  #否则如果键盘按下
                if event.key == K_s: #如果按下“S”键
                    screen.blit(mouth_open, (0, 0)) #显示图片“mouth_open”
                    # pygame.display.update()   #更新pygame窗口
                    Carrot(Carrot_time=5)   #运行“播放声音”的函数,播放时长:5s
                    screen.blit(mouth_close, (0, 0))    #显示图片“mouth_open”
                    # pygame.display.update()   #更新pygame窗口

        # pygame.display.update() #更新pygame窗口

def refresh (): #创建函数:刷新窗口(为多线程用),命名为“refresh”
    pygame.init()  # 初始化pygame
    while True: #开启循环
        pygame.display.update()  #更新pygame窗口
        time.sleep(0.1) #设置0.1秒刷新一次pygame窗口
        # print("刷新成功!")    告诉我:刷新成功了

thread1 = threading.Thread(target= refresh) #创建多线程,运行函数:“refresh”
thread2 = threading.Thread(target= window)  #创建多线程,运行函数:“window”

thread1.start()   #启动线程1
thread2.start()   #启动线程2

还有一些图片和视频素材,就不上传了(上传不了)。
这怎么解决啊?

  • 写回答

2条回答 默认 最新

  • cjh4312 2023-01-05 20:59
    关注

    pygame.mixer.Sound没有pause()函数,只有stop().
    pause()是pygame.mixer这个有,pygame.mixer.Channel,pygame.mixer.music也有

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月14日
  • 已采纳回答 1月6日
  • 创建了问题 1月5日

悬赏问题

  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗