import pygame |
import tkinter as tk |
from tkinter import ttk |
from tkinter import filedialog, messagebox,PhotoImage |
import os |
from ctypes import cast, POINTER |
from comtypes import CLSCTX_ALL |
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume |
#获取系统音乐音量数据 |
devices = AudioUtilities.GetSpeakers() |
interface = devices.Activate( |
IAudioEndpointVolume._iid_, CLSCTX_ALL, None ) |
volume = cast(interface, POINTER(IAudioEndpointVolume)) |
# 初始化 |
pygame.mixer.init() |
#创建tkinter窗口 |
root = tk.Tk() #窗口初始化 |
root.geometry( "500x300" ) #窗口大小 |
root.resizable( 0 , 0 ) #设置窗口大小无法调整 |
root.title( "音乐播放器 2.0(窗口版)" ) #标题 |
#设置音量为12% |
musicSize = - 30 |
volume.SetMasterVolumeLevel(musicSize, None ) |
#加载音乐并播放 |
#暂停音乐 |
def stopMusic(): |
pygame.mixer.music.stop() |
def smallMusic(): #调小声 |
try : |
global musicSize |
musicSize - = 2 |
volume.SetMasterVolumeLevel(musicSize, None ) |
except : |
messagebox.showwarning(title = '提示' , message = '音量以达到最小' ) |
|
def bigMusic(): #调大声 |
try : |
global musicSize |
musicSize + = 2 |
volume.SetMasterVolumeLevel(musicSize, None ) |
except : |
messagebox.showwarning(title = '提示' , message = '音量以达到最大' ) |
#从目录寻找并加载播放音乐 |
def findMusic(): |
file = filedialog.askopenfilename(initialdir = "C:/" , title = 'Choose an Music.' ) |
if len ( file ) ! = 0 : |
track = pygame.mixer.music.load( file ) |
pygame.mixer.music.play() |
else : |
messagebox.showwarning(title = '提示' , message = '您还没有选择歌曲哦' ) |
|
# #以下代码用于播放gif图片 |
# frame = tk.Frame(root).place(relx=0.25,rely=0.25) |
# numIdx = 6 # gif的帧数 |
# # 填充6帧内容到frames |
# frames = [PhotoImage(file='./images/abc.gif', format='gif -index %i' %(i)) for i in range(numIdx)] |
# def update(idx): # 定时器函数 |
# frame = frames[idx] |
# idx += 1 # 下一帧的序号:在0,1,2,3,4,5之间循环(共6帧) |
# label.configure(image=frame) # 显示当前帧的图片 |
# root.after(100, update, idx%numIdx) # 0.1秒(100毫秒)之后继续执行定时器函数(update) |
# |
# label = tk.Label(frame) |
# label.pack() |
# root.after(0, update, 0) # 立即启动定时器函数(update) |
#用户提示标签 |
tk.Label(root,text = "哈喽~\n欢迎来到音乐播放器。我的朋友,您要听什么音乐?" ).place(relx = 0.22 ,rely = 0 ) |
# tk.Label(root,text="歌曲列表").place(relx=0.04,rely=0.13) |
# #添加样式下拉列表 |
# cbox_E = ttk.Combobox(root,width=26) |
# #下拉选择框的内容 |
# cbox_E['values'] = ['请选择试听音乐','love is Gone','NUMB(网友自制)','Faded(纯音乐)','Way back','Neveda','Stay','Rise','Monsters(纯音乐版)','篝火旁','她的微笑','中华-FADED兵王','不回首','鱼','香肠派对S1赛季背景音乐'] |
# #下拉选择框的位置 |
# cbox_E.place(relx=0.04,rely=0.19) |
# #设置默认列表项 |
# cbox_E.current(0) |
# #设置下拉列表的只读模式 |
# cbox_E['state'] = 'readonly' |
# #判断选择 |
# cbox_E.bind('<<ComboboxSelected>>',showMsg_E) |
#按钮组件 |
tk.Button(root,text = " 点击该按钮从目录寻找歌曲 " ,bg = '#AFFEEE' ,command = findMusic).place(relx = 0.31 ,rely = 0.18 ) |
# tk.Button(root,text=" Start ",bg='#AFEEEE',command=startMusic).place(relx=0.06,rely=0.44) |
tk.Button(root,text = " 暂停 " ,bg = '#AFEEEE' ,command = stopMusic).place(relx = 0.23 ,rely = 0.84 ) |
tk.Button(root,text = " 音量大 " ,bg = '#AFEEEE' ,command = bigMusic).place(relx = 0.43 ,rely = 0.84 ) |
tk.Button(root,text = " 音量小 " ,bg = '#AFEEEE' ,command = smallMusic).place(relx = 0.63 ,rely = 0.84 ) |
#提示用户 |
messagebox.showinfo( "提示" , "已为您音乐音量调为12%" ) |
root.mainloop() |
|
|
|
|