import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton
from tkinter import *
from tkinter import filedialog
import pygame
class MusicPlayer:
def __init__(self, window):
self.window = window
self.window.title("知鹿音乐")
self.window.geometry("500x400")
self.play_btn = Button(window, text="播放音乐", command=self.play_music) #按钮控件;在程序中显示按钮
self.play_btn.place(x=60, y=20)
self.stop_btn = Button(window, text="停止播放", command=self.stop_music)
self.stop_btn.place(x=180, y=20)
self.music_listbox = Listbox(window)
self.music_listbox.place(x=160, y=120)
self.open_file_btn = Button(window, text="本地音乐", command=self.add_to_playlist)
self.open_file_btn.place(x=300, y=20)
def add_to_playlist(self):
file_path = filedialog.askopenfilename(title="选择音乐文件", filetypes=(("Mp3 Files", "*.mp3"),))
if file_path:
self.music_listbox.insert(END, file_path)
def play_music(self):
if len(self.music_listbox.curselection()) == 0:
return
index = int(self.music_listbox.curselection()[0])
music_file = self.music_listbox.get(index)
pygame.mixer.init()
pygame.mixer.music.load(music_file)
pygame.mixer.music.play()
def pause_music(self):
pygame.mixer.music.pause()
def stop_music(self):
pygame.mixer.music.stop()
window = Tk()
music_player = MusicPlayer(window)
window.mainloop()
利用python做一个有意思的音乐播放器,有感兴趣的自取哦
最新推荐文章于 2023-09-13 18:57:40 发布