#效果图
#音乐播放主程序.py
import tkinter
import os
from musicTree import TreeWindows
from lyric import Lyric
from musicButton import MusicButton
import sys,time
win = tkinter.Tk()
win.title("音乐播放器")
win.geometry("950x700+300+100")
path = r"C:\Users\Administrator\Desktop\python基础\音乐播放器\歌曲"
frameL = tkinter.Frame(win)
frameL.pack(fill=tkinter.Y, side=tkinter.LEFT)
frameR = tkinter.Frame(win)
frameR.pack(fill=tkinter.Y, side=tkinter.RIGHT)
ly = Lyric(frameR)
ly.vis()
treeWin = TreeWindows(frameL, path)
button = MusicButton(frameR, path)
win.mainloop()
#musicTree.py
import tkinter
from tkinter import ttk
import os
import time
import pygame
class TreeWindows(tkinter.Frame):
def __init__(self, master, path):
self.path = path
frame = tkinter.Frame(master)
frame.pack(side=tkinter.LEFT, fill=tkinter.Y)
self.label = tkinter.Label(frame,
text="曲库",
bg="blue",
fg="white",
font=("华文琥珀", 15))
self.label.pack(fill=tkinter.X)
self.tree = ttk.Treeview(frame)
self.tree.pack(fill=tkinter.Y, side=tkinter.LEFT)
#text=os.path.splitext()将路径最后一级分离出来 windows不支持
root = self.tree.insert("", "end", text=self.getPath(path), open=True, values=(path))
#加载所有树枝
self.loadTree(root, path)
#添加滚动条
self.sy = tkinter.Scrollbar(frame)
self.sy.pack(side=tkinter.RIGHT, fill=tkinter.Y)
self.sy.config(command=self.tree.yview)
self.tree.config(yscrollcommand=self.sy.set)
#绑定事件
self.tree.bind("<>", self.func)
def func(self, event):
self.v = event.widget.selection()
for sv in self.v:
file = self.tree.item(sv)["text"]
absPath = os.path.join(self.path, file)
pygame.mixer.init()
if absPath != r"C:\Users\Administrator\Desktop\python基础\音乐播放器\歌曲\歌曲":
track = pygame.mixer.music.load(absPath)
pygame.mixer.music.play()
def loadTree(self, father, fatherPath):
#遍历所有目录
for filename in os.listdir(fatherPath):
absPath = os.path.join(fatherPath, filename)
#插入树枝
treey = self.tree.insert(father, "end", text=self.getPath(absPath), values=(absPath))
#判断是否是目录
if os.path.isdir(absPath):
#递归
self.loadTree(treey, absPath)
def getPath(self, path):
pathList = os.path.split(path)
return pathList[-1]
#musicButton.py
import tkinter
import os
import pygame
import random
class MusicButton(tkinter.Frame):
def __init__(self, master, path):
self.path = path
frame = tkinter.Frame(master)
frame.pack(side=tkinter.TOP, fill=tkinter.X)
self.label = tkinter.Label(frame)
self.label.pack(fill=tkinter.X, side=tkinter.TOP)
self.button1 = tkinter.Button(frame,
text="上一曲",
command=self.func1,
width=8,
height=2,
bg="blue",
fg="white",
font=("华文琥珀", 8))
self.button1.pack(side=tkinter.LEFT)
self.button2 = tkinter.Button(frame,
text="播放",
command=self.func2,
width=8,
height=2,
bg="blue",
fg="white",
font=("华文琥珀", 8))
self.button2.pack(side=tkinter.LEFT)
self.button3 = tkinter.Button(frame,
text="暂停",
command=self.func3,
width=8,
height=2,
bg="blue",
fg="white",
font=("华文琥珀", 8))
self.button3.pack(side=tkinter.LEFT)
self.button4 = tkinter.Button(frame,
text="下一曲",
command=self.func4,
width=8,
height=2,
bg="blue",
fg="white",
font=("华文琥珀", 8))
self.button4.pack(side=tkinter.LEFT)
self.label = tkinter.Label(frame,
text="这 里 是 进 度 条 ……",
bg="light green",
fg="blue")
self.label.pack(fill=tkinter.X, side=tkinter.BOTTOM)
def func1(self):
file = "汪苏泷、林希儿 - 专属味道.mp3"
absPath = os.path.join(self.path, file)
pygame.mixer.init()
if absPath != r"C:\Users\Administrator\Desktop\python基础\音乐播放器\歌曲\歌曲":
track = pygame.mixer.music.load(absPath)
pygame.mixer.music.play()
def func2(self):
pygame.mixer.music.play()
def func3(self):
pygame.mixer.music.pause()
def func4(self):
file = "杨宗纬 - 一次就好.mp3"
absPath = os.path.join(self.path, file)
pygame.mixer.init()
if absPath != r"C:\Users\Administrator\Desktop\python基础\音乐播放器\歌曲\歌曲":
track = pygame.mixer.music.load(absPath)
pygame.mixer.music.play()
#lyric.py
import tkinter
import os
class Lyric(tkinter.Frame):
def __init__(self, master):
frame = tkinter.Frame(master)
frame.pack(side=tkinter.TOP, fill=tkinter.Y)
self.label = tkinter.Label(frame,
text="歌词写真",
bg="blue",
fg="white",
font=("华文琥珀", 15))
self.label.pack(fill=tkinter.X)
self.txt = tkinter.Text(frame,
width=100,
height=34,
bg="light blue",
fg="white")
self.txt.pack(side=tkinter.TOP, fill=tkinter.Y)
def vis(self):
str = "\n\n\n\n\n\n\n\t\t\t\t正在搜索歌词……"
self.txt.insert(tkinter.INSERT, str)