写一个音乐播放器的gui
时间: 2023-09-20 21:08:43 浏览: 138
抱歉,我是AI语言模型,无法提供图形用户界面。以下是一个示例音乐播放器GUI的代码,你可以根据自己的需求进行修改和优化:
```
from tkinter import *
from tkinter.filedialog import askopenfilename
import pygame
class MusicPlayer:
def __init__(self, window):
window.geometry('400x200')
window.title('Music Player')
window.resizable(0,0)
self.music_file = StringVar()
self.playing = False
self.paused = False
self.create_widgets(window)
def create_widgets(self, window):
# 标签
Label(window, text='Select a music file:', font=('helvetica', 12)).grid(row=0, column=0, pady=10)
# 输入框
Entry(window, textvariable=self.music_file, width=30).grid(row=0, column=1)
# 按钮
Button(window, text='Browse', command=self.browse_file).grid(row=0, column=2, padx=10)
self.play_button = Button(window, text='Play', command=self.play_music)
self.play_button.grid(row=1, column=0, pady=10)
Button(window, text='Pause', command=self.pause_music).grid(row=1, column=1)
Button(window, text='Stop', command=self.stop_music).grid(row=1, column=2)
def browse_file(self):
self.music_file.set(askopenfilename(initialdir='/', title='Select a music file', filetypes=(('MP3 files', '*.mp3'), ('All files', '*.*'))))
def play_music(self):
if not self.playing:
pygame.mixer.init()
pygame.mixer.music.load(self.music_file.get())
pygame.mixer.music.play()
self.playing = True
elif self.paused:
pygame.mixer.music.unpause()
self.paused = False
self.play_button.config(text='Pause')
def pause_music(self):
if not self.paused:
pygame.mixer.music.pause()
self.paused = True
self.play_button.config(text='Play')
def stop_music(self):
pygame.mixer.music.stop()
self.playing = False
self.paused = False
self.play_button.config(text='Play')
root = Tk()
player = MusicPlayer(root)
root.mainloop()
```
阅读全文