Python에는 다양한 애플리케이션 인터페이스 및 구성 요소를 빌드하는 데 사용되는 많은 내장 라이브러리와 모듈이 있습니다. 파이게임 비디오 게임과 음악을 설계하고 구축하는 데 사용되는 파이썬 모듈 중 하나입니다. 모든 사운드 관련 활동을 처리할 수 있는 혼합물을 제공합니다. 음악 사용 서브 모듈로 mp3, ogg 및 기타 다양한 사운드를 스트리밍할 수 있습니다.
버튼 클릭 시 소리를 재생하는 애플리케이션을 만들려면 다음 단계를 따라야 합니다.
-
Pygame 로컬 컴퓨터에 설치됩니다. 파이 게임 을 설치할 수 있습니다. pip install pygame 사용 명령.
-
파이게임 초기화 pygame.mixture.init()를 사용하여 혼합
-
음악을 재생하는 데 사용되는 버튼 위젯을 만듭니다.
-
play_sound() 함수 정의 mixture.load.music(filename)에서 파일의 위치를 지정하여 음악을 로드합니다. .
-
mixture.music.play() 추가 음악을 재생합니다.
예
# Import the required libraries
from tkinter import *
import pygame
from PIL import Image, ImageTk
# Create an instance of tkinter frame or window
win = Tk()
# Set the size of the window
win.geometry("700x500")
# Add a background image
bg = ImageTk.PhotoImage(file="music.jpg")
label = Label(win, image=bg)
label.place(x=0, y=0)
# Initialize mixer module in pygame
pygame.mixer.init()
# Define a function to play the music
def play_sound():
pygame.mixer.music.load("sample1.mp3")
pygame.mixer.music.play()
# Add a Button widget
b1 = Button(win, text="Play Music", command=play_sound)
b1.pack(pady=60)
win.mainloop() 출력
위의 코드를 실행하면 버튼이 있는 창이 표시됩니다. 이제 지정된 기능에 음악 위치를 추가하여 애플리케이션에서 일부 음악을 재생합니다.
