学习了一个简易的音乐播放器代码。使用Python编写。只能解析wav格式。

代码如下:

from tkinter import *
import pygame.mixer
from tkinter.messagebox import askokcancel
app = Tk()
app.title("Head First Mix")
app.geometry('250x100+200+100')
sound_file = "GT.wav"
volume = DoubleVar()
mixer = pygame.mixer
mixer.init()
def flip_it():
if flipper.get() ==1:
print("cool. I'm all ON,man!")
else:
print("Phooey. I'm OFF.")
def track_start():
track.play(loops = -1)
def track_stop():
track.stop()
def shutdown():
track.stop()
if askokcancel(title = 'Are you sure?',message = 'Do you really want to quit?'):
app.destroy()
def track_toggle():
if track_playing.get() == 1:
track.play(loops = -1)
else:
track.stop()
def change_volume(v):
track.set_volume(volume.get())
track = mixer.Sound(sound_file)
#start_button = Button(app,command = track_start,text = "Start")
#start_button.pack(side = LEFT)
#stop_button = Button(app,command = track_stop,text = "Stop")
#stop_button.pack(side = RIGHT)
track_playing = IntVar()
track_button = Checkbutton(app,variable = track_playing,command = track_toggle,text = sound_file)
track_button.pack(side = LEFT)
volume_scale = Scale(app,
variable = volume,
from_ = 0.0,
to = 1.0,
resolution = 0.1,
command = change_volume,
label = "Volume",
orient = HORIZONTAL)
volume_scale.pack(side = RIGHT)
app.protocol("WM_DELETE_WINDOW",shutdown)
app.mainloop()