开开心心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日

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)