python 制作简单的音乐播放器

如你所见,功能很简单。只有基本的播放,停止,甚至只针对一首歌曲,仅供初学者参考学习用。

代码

from tkinter import *
from tkinter import filedialog
from pygame import mixer

class MusicPlayer:
  def __init__(self, window ):
    window.geometry('320x100'); window.title('Iris Player'); window.resizable(0,0)
    Load = Button(window, text = 'Load', width = 10, font = ('Times', 10), command = self.load)
    Play = Button(window, text = 'Play', width = 10,font = ('Times', 10), command = self.play)
    Pause = Button(window,text = 'Pause', width = 10, font = ('Times', 10), command = self.pause)
    Stop = Button(window ,text = 'Stop', width = 10, font = ('Times', 10), command = self.stop)
    Load.place(x=0,y=20);Play.place(x=110,y=20);Pause.place(x=220,y=20);Stop.place(x=110,y=60) 
    self.music_file = False
    self.playing_state = False
  def load(self):
    self.music_file = filedialog.askopenfilename()
  def play(self):
    if self.music_file:
      mixer.init()
      mixer.music.load(self.music_file)
      mixer.music.play()
  def pause(self):
    if not self.playing_state:
      mixer.music.pause()
      self.playing_state=True
    else:
      mixer.music.unpause()
      self.playing_state = False
  def stop(self):
    mixer.music.stop()
root = Tk()
app= MusicPlayer(root)
root.mainloop()

以上就是python 制作简单的音乐播放器的详细内容,更多关于python 音乐播放器的资料请关注呐喊教程其它相关文章!

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。