import pygame
import tkinter as tk
from tkinter import filedialog
初始化 pygame
pygame.mixer.init()
current_song_index = 0
def play_music():
selected_indices = song_list.curselection()
if selected_indices:
global current_song_index
current_song_index = selected_indices[0]
file_path = song_list.get(current_song_index)
if file_path.endswith(’.mp3’) or file_path.endswith(’.wav’) or file_path.endswith(’.ogg’):
pygame.mixer.music.load(file_path)
pygame.mixer.music.play()
def pause_music():
pygame.mixer.music.pause()
def continue_music():
pygame.mixer.music.unpause()
def stop_music():
pygame.mixer.music.stop()
def next_song():
global current_song_index
if current_song_index < song_list.size() - 1:
current_song_index += 1
else:
current_song_index = 0
file_path = song_list.get(current_song_index)
if file_path.endswith(’.mp3’) or file_path.endswith(’.wav’) or file_path.endswith(’.ogg’):
pygame.mixer.music.load(file_path)
pygame.mixer.music.play()
def previous_song():
global current_song_index
if current_song_index > 0:
current_song_index -= 1
else:
current_song_index = song_list.size() - 1
file_path = song_list.get(current_song_index)
if file_path.endswith(’.mp3’) or file_path.endswith(’.wav’) or file_path.endswith(’.ogg’):
pygame.mixer.music.load(file_path)
pygame.mixer.music.play()
def open_file():
file_paths = filedialog.askopenfilenames(filetypes=[(“MP3 Files”, “.mp3"), (“WAV Files”, ".wav”), (“OGG Files”, “*.ogg”)])
for file_path in file_paths:
song_list.insert(tk.END, file_path)
root = tk.Tk()
root.title(“音乐播放器”)
song_list = tk.Listbox(root,background=‘cyan’, selectmode=tk.MULTIPLE)
song_list.place(x=50, y=300, width=600, height=300) # 设置播放按钮的位置为 x=100, y=200,宽度为 80,高度为 30
song_list.config(selectmode=tk.MULTIPLE)
play_button = tk.Button(root,background=‘green’, text=“播放”)
play_button.place(x=100, y=800, width=150, height=100) # 设置播放按钮的位置为 x=100, y=200,宽度为 80,高度为 30
play_button.config(command=play_music)
pause_button = tk.Button(root,background=‘yellow’,text=“暂停”, command=pause_music)
pause_button.place(x=300, y=800, width=150, height=100) # 设置播放按钮的位置为 x=100, y=200,宽度为 80,高度为 30
pause_button.config(command=pause_music)
continue_button = tk.Button(root,background=‘blue’, text=“继续”, command=continue_music)
continue_button.place(x=300, y=900, width=150, height=100) # 设置播放按钮的位置为 x=100, y=200,宽度为 80,高度为 30
continue_button.config(command=continue_music)
stop_button = tk.Button(root,background=‘red’, text=“停止”, command=stop_music)
stop_button.place(x=100, y=900, width=150, height=100) # 设置播放按钮的位置为 x=100, y=200,宽度为 80,高度为 30
stop_button.config(command=stop_music)
previous_button = tk.Button(root,background=‘orange’, text=“上一首”, command=previous_song)
previous_button.place(x=500, y=800, width=150, height=100) # 设置播放按钮的位置为 x=100, y=200,宽度为 80,高度为 30
previous_button.config(command=previous_song)
next_button = tk.Button(root,background=‘orange’, text=“下一首”, command=next_song)
next_button.place(x=500, y=900, width=150, height=100) # 设置播放按钮的位置为 x=100, y=200,宽度为 80,高度为 30
next_button.config(command=next_song)
open_button = tk.Button(root,background=‘purple’, text=“打开文件”, command=open_file)
open_button.place(x=250, y=650, width=200, height=100) # 设置播放按钮的位置为 x=100, y=200,宽度为 80,高度为 30
open_button.config(command=open_file)
root.mainloop()
标签:song,Python,button,编译器,安卓版,music,file,path,100 From: https://blog.csdn.net/qq_32257509/article/details/142149343