最近和我亲爱的同学一起搞一个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
还有一些图片和视频素材,就不上传了(上传不了)。
这怎么解决啊?