[英]Python pygame “an integer is required”
I have a function using gTTS, pygame and os modules: 我有一个使用gTTS,pygame和os模块的功能:
from gtts import gTTS
import pygame as pg
from os import remove as OSremove
music = pg.mixer.music
def speak(x):
sound = gTTS(text=x, lang="pl")
sound.save("temp.mp3")
pg.mixer.init()
music.load("temp.mp3")
music.play("temp.mp3")
pg.quit()
OSremove("temp.mp3")
And I'm getting an error: TypeError: an integer is required (got type str). 而且我收到一个错误:TypeError:必须为整数(类型为str)。 Image here because I have 5 rep
图片在这里,因为我有5个代表
Okay, the answer above is correct as far as it goes, but let's look a little deeper at it since you're clearly new to this. 好的,上面的答案就目前而言是正确的,但是让我们更深入地了解它,因为您显然是新手。
First of all, when you get an error message, read it . 首先,当您收到错误消息时, 请阅读它 。 In this case, it's telling you that it wants an integer and you're giving it a string.
在这种情况下,它告诉您它需要一个整数,并且您要给它一个字符串。 So, clearly, you misunderstood something.
因此,很明显,您误解了一些东西。
Next, have a look at the documentation. 接下来,看看文档。 Google
pygame.mixer.music.play
and it'll pop right up. Google
pygame.mixer.music.play
,它将立即弹出。
https://www.pygame.org/docs/ref/music.html#pygame.mixer.music.play https://www.pygame.org/docs/ref/music.html#pygame.mixer.music.play
Next, look at the documentation. 接下来,查看文档。 In this case it says:
在这种情况下,它说:
play
takes two arguments, loops
and start
, both of them defaulting to 0. No strings -- so you're giving it the wrong argument. play
需要两个参数, loops
和start
,它们都默认为0。没有字符串-因此您给它指定了错误的参数。
Look further on the Google results and you'll see an SO question: How play mp3 with pygame 进一步查看Google搜索结果,您会看到一个SO问题: 如何使用pygame播放mp3
And that will lead you to some example code in another SO answer. 这将导致您在另一个SO答案中找到一些示例代码。
You don't need to pass the name of the music file to the play
function. 您无需将音乐文件的名称传递给
play
功能。 Just call play()
without parameters and it should work. 只需调用不带参数的
play()
即可。
See the documentation of the play
function: 请参阅
play
功能的文档:
http://www.pygame.org/docs/ref/music.html#pygame.mixer.music.play http://www.pygame.org/docs/ref/music.html#pygame.mixer.music.play
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.