基于Tkinter的ikun播放器(含Pyinstaller打包)
ikun
资源:
download.csdn.net/download/weixin_53403301/86749998
基于Tkinter的ikun播放器(软件、开源代码及资源包)
先上效果和代码
基于Tkinter简易GUI的ikun播放器(小黑子终于露出了鸡脚)
# -*- coding: utf-8 -*-
import tkinter as tk # 调用窗口tk
from PIL import Image, ImageTk
from threading import Thread
from time import sleep # 调用系统时间戳库
import pygame.mixer as mixer
import sys
import os
audio_path = "root/Audio/"
audio_format = ".mp3"
gif_path = "root/Gif/"
gif_format = ".png"
pic_path = "root/Pic/"
pic_format = ".jpg"
cxk_list = ["鸡","你","太","美","C","t","r","l","制作人","大家好","我是","坤坤","你干嘛","哈哈","哎哟","鸡你太美"]
normal_pic = "normal"
mixer.init()
mixer.music.set_volume(1.0)
def get_resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)
def get_gif(img_path):
img = Image.open(img_path)
gif_num=0
try:
while True:
curr = img.tell()
name = gif_path+str(curr)+gif_format
img.save(name)
img.seek(curr+1)
gif_num=curr+1
except:
gif_num=curr+1
return gif_num
def central_win(win):
win.resizable(0,0) # 不可缩放
screenwidth = win.winfo_screenwidth() # 获取屏幕分辨率宽
screenheight = win.winfo_screenheight() # 获取屏幕分辨率高
win.update() # 更新窗口
width = win.winfo_width() # 重新赋值
height = win.winfo_height()
size = '+%d+%d' % ((screenwidth - width)/2, (screenheight - height)/2)
# 重新赋值大小 大小为屏幕大小/2
win.geometry(size) # 以新大小定义窗口
def cxk_gui():
root=tk.Tk()
root.title("ikun Player By 网易独家音乐人Mike Zhou")
root.iconphoto(False, tk.PhotoImage(file=get_resource_path('root/cxk.png')))
mainfram=tk.Frame(root,width=500, height=550)
mainfram.grid_propagate(0)
mainfram.grid()
central_win(root)
img=Image.open(get_resource_path("root/star.png"))
photo = img.resize((200,200)) # 限制最大高度为580 等比缩放显示
tkImage_cxk = ImageTk.PhotoImage(image=photo)
tk.Label(mainfram, image=tkImage_cxk).place(x=150, y=50, width=200, height=200)
global gif_stop_flag
gif_stop_flag = 0
def play_gif(img_path,delay,w,h):
global gif_stop_flag
global gif_list
gif_list = []
gif_num = get_gif(img_path)
j=0
for i in range(0,gif_num):
img=Image.open(gif_path+str(i)+gif_format)
photo = img.resize((w,h)) # 限制最大高度为580 等比缩放显示
tkImage_cxk = ImageTk.PhotoImage(image=photo)
gif_list.append(tkImage_cxk)
while True:
tk.Label(mainfram, image=gif_list[j]).place(x=(500-w)/2, y=50, width=w, height=h)
sleep(delay)
j=j+1
if j==gif_num:
j=0
if gif_stop_flag==1:
break
def play_gif_pics(img_path,delay,w,h):
global gif_stop_flag
global gif_list
gif_list = []
gif_num = 20
j=0
for i in range(0,gif_num):
img=Image.open(img_path+str(i)+gif_format)
photo = img.resize((w,h)) # 限制最大高度为580 等比缩放显示
tkImage_cxk = ImageTk.PhotoImage(image=photo)
gif_list.append(tkImage_cxk)
while True:
tk.Label(mainfram, image=gif_list[j]).place(x=(500-w)/2, y=50, width=w, height=h)
sleep(delay)
j=j+1
if j==gif_num:
j=0
if gif_stop_flag==1:
break
def gif_start():
# play_gif(get_resource_path("root/0.gif"),0.1,200,200)
play_gif_pics(get_resource_path(gif_path),0.1,200,200)
Thread(target=gif_start).setDaemon(True)
Thread(target=gif_start).start()
def play_audio(num):
name=get_resource_path(audio_path+str(cxk_list[num])+audio_format)
mixer.music.load(name)
mixer.music.play()
def show_pic(num):
global tkImage_cxk
img=Image.open(get_resource_path(pic_path+str(cxk_list[num])+pic_format))
photo = img.resize((200,200)) # 限制最大高度为580 等比缩放显示
tkImage_cxk = ImageTk.PhotoImage(image=photo)
tk.Label(mainfram, image=tkImage_cxk).place(x=150, y=50, width=200, height=200)
def play_cxk(num):
global gif_stop_flag
gif_stop_flag=1
play_audio(num)
show_pic(num)
tk.Button(mainfram,width=30,text=cxk_list[0],command=lambda:play_cxk(0)).place(x=50+0*100, y=300+50*0, width=100, height=50)
tk.Button(mainfram,width=30,text=cxk_list[1],command=lambda:play_cxk(1)).place(x=50+1*100, y=300+50*0, width=100, height=50)
tk.Button(mainfram,width=30,text=cxk_list[2],command=lambda:play_cxk(2)).place(x=50+2*100, y=300+50*0, width=100, height=50)
tk.Button(mainfram,width=30,text=cxk_list[3],command=lambda:play_cxk(3)).place(x=50+3*100, y=300+50*0, width=100, height=50)
tk.Button(mainfram,width=30,text=cxk_list[4],command=lambda:play_cxk(4)).place(x=50+0*100, y=300+50*1, width=100, height=50)
tk.Button(mainfram,width=30,text=cxk_list[5],command=lambda:play_cxk(5)).place(x=50+1*100, y=300+50*1, width=100, height=50)
tk.Button(mainfram,width=30,text=cxk_list[6],command=lambda:play_cxk(6)).place(x=50+2*100, y=300+50*1, width=100, height=50)
tk.Button(mainfram,width=30,text=cxk_list[7],command=lambda:play_cxk(7)).place(x=50+3*100, y=300+50*1, width=100, height=50)
tk.Button(mainfram,width=30,text=cxk_list[8],command=lambda:play_cxk(8)).place(x=50+0*100, y=300+50*2, width=100, height=50)
tk.Button(mainfram,width=30,text=cxk_list[9],command=lambda:play_cxk(9)).place(x=50+1*100, y=300+50*2, width=100, height=50)
tk.Button(mainfram,width=30,text=cxk_list[10],command=lambda:play_cxk(10)).place(x=50+2*100, y=300+50*2, width=100, height=50)
tk.Button(mainfram,width=30,text=cxk_list[11],command=lambda:play_cxk(11)).place(x=50+3*100, y=300+50*2, width=100, height=50)
tk.Button(mainfram,width=30,text=cxk_list[12],command=lambda:play_cxk(12)).place(x=50+0*100, y=300+50*3, width=100, height=50)
tk.Button(mainfram,width=30,text=cxk_list[13],command=lambda:play_cxk(13)).place(x=50+1*100, y=300+50*3, width=100, height=50)
tk.Button(mainfram,width=30,text=cxk_list[14],command=lambda:play_cxk(14)).place(x=50+2*100, y=300+50*3, width=100, height=50)
tk.Button(mainfram,width=30,text=cxk_list[15],command=lambda:play_cxk(15)).place(x=50+3*100, y=300+50*3, width=100, height=50)
play_audio(0)
root.mainloop()
if __name__ == "__main__":
cxk_gui()
mixer.music.stop()
try:
sys.exit()
except:
pass
打包参考:
https://blog.csdn.net/weixin_53403301/article/details/127276209
play_gif函数用于播放gif gif要先分解成每一帧的图片 然后循环
而play_gif_pics直接对目录下的每一帧图片进行循环播放
最后打包
而后改进 完善函数 并把“鸡你太美”按钮改成播放gif动画
# -*- coding: utf-8 -*-
import tkinter as tk # 调用窗口tk
from PIL import Image, ImageTk
from threading import Thread
from time import sleep # 调用系统时间戳库
import pygame.mixer as mixer
import sys
import os
audio_path = "root/Audio/"
audio_format = ".mp3"
gif_path = "root/Gif/"
gif_format = ".png"
pic_path = "root/Pic/"
pic_format = ".jpg"
cxk_list = ["鸡","你","太","美","C","t","r","l","制作人","大家好","我是","坤坤","你干嘛","哈哈","哎哟","鸡你太美"]
normal_pic = "normal"
mixer.init()
mixer.music.set_volume(1.0)
def get_resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)
def get_gif(img_path,num):
img = Image.open(img_path)
gif_num=0
try:
while True:
curr = img.tell()
name = gif_path+str(num)+'/'+str(curr)+gif_format
img.save(name)
img.seek(curr+1)
gif_num=curr+1
except:
gif_num=curr+1
return gif_num
def central_win(win):
win.resizable(0,0) # 不可缩放
screenwidth = win.winfo_screenwidth() # 获取屏幕分辨率宽
screenheight = win.winfo_screenheight() # 获取屏幕分辨率高
win.update() # 更新窗口
width = win.winfo_width() # 重新赋值
height = win.winfo_height()
size = '+%d+%d' % ((screenwidth - width)/2, (screenheight - height)/2)
# 重新赋值大小 大小为屏幕大小/2
win.geometry(size) # 以新大小定义窗口
def cxk_gui():
root=tk.Tk()
root.title("ikun Player By 网易独家音乐人Mike Zhou")
root.iconphoto(False, tk.PhotoImage(file=get_resource_path('root/cxk.png')))
mainfram=tk.Frame(root,width=500, height=550)
mainfram.grid_propagate(0)
mainfram.grid()
central_win(root)
img=Image.open(get_resource_path("root/star.png"))
photo = img.resize((200,200)) # 限制最大高度为580 等比缩放显示
tkImage_cxk = ImageTk.PhotoImage(image=photo)
tk.Label(mainfram, image=tkImage_cxk).place(x=150, y=50, width=200, height=200)
global gif_stop_flag
gif_stop_flag = 0
def play_gif(img_path,num,delay,w,h):
global gif_stop_flag
global gif_list
gif_stop_flag=0
gif_list = []
gif_num = get_gif(img_path,num)
j=0
for i in range(0,gif_num):
img=Image.open(gif_path+str(num)+'/'+str(i)+gif_format)
photo = img.resize((w,h)) # 限制最大高度为580 等比缩放显示
tkImage_cxk = ImageTk.PhotoImage(image=photo)
gif_list.append(tkImage_cxk)
while True:
tk.Label(mainfram, image=gif_list[j]).place(x=(500-w)/2, y=50, width=w, height=h)
sleep(delay)
j=j+1
if j==gif_num:
j=0
if gif_stop_flag==1:
break
def play_gif_pics(img_path,num,delay,w,h):
global gif_stop_flag
global gif_list
gif_stop_flag=0
gif_list = []
gif_num=len(os.listdir(img_path+str(num)+'/'))
j=0
for i in range(0,gif_num):
img=Image.open(img_path+str(num)+'/'+str(i)+gif_format)
photo = img.resize((w,h)) # 限制最大高度为580 等比缩放显示
tkImage_cxk = ImageTk.PhotoImage(image=photo)
gif_list.append(tkImage_cxk)
while True:
tk.Label(mainfram, image=gif_list[j]).place(x=(500-w)/2, y=50, width=w, height=h)
sleep(delay)
j=j+1
if j==gif_num:
j=0
if gif_stop_flag==1:
break
def gif_0():
try:
play_gif_pics(get_resource_path(gif_path),0,0.1,200,200)
except:
os.mkdir(get_resource_path(gif_path)+"0")
play_gif(get_resource_path("root/0.gif"),0,0.1,200,200)
def gif_0_start():
Thread(target=gif_0).setDaemon(False)
Thread(target=gif_0).start()
def gif_1():
try:
play_gif_pics(get_resource_path(gif_path),1,0.1,353,200)
except:
os.mkdir(get_resource_path(gif_path)+"1")
play_gif(get_resource_path("root/1.gif"),1,0.1,353,200)
def gif_1_start():
Thread(target=gif_1).setDaemon(False)
Thread(target=gif_1).start()
def play_audio(num):
name=get_resource_path(audio_path+str(cxk_list[num])+audio_format)
mixer.music.load(name)
mixer.music.play()
def show_pic(num):
global tkImage_cxk
img=Image.open(get_resource_path(pic_path+str(cxk_list[num])+pic_format))
photo = img.resize((200,200)) # 限制最大高度为580 等比缩放显示
tkImage_cxk = ImageTk.PhotoImage(image=photo)
tk.Label(mainfram, image=tkImage_cxk).place(x=150, y=50, width=200, height=200)
def play_cxk(num):
global gif_stop_flag
gif_stop_flag=1
play_audio(num)
show_pic(num)
if num==15:
gif_1_start()
tk.Button(mainfram,width=30,text=cxk_list[0],command=lambda:play_cxk(0)).place(x=50+0*100, y=300+50*0, width=100, height=50)
tk.Button(mainfram,width=30,text=cxk_list[1],command=lambda:play_cxk(1)).place(x=50+1*100, y=300+50*0, width=100, height=50)
tk.Button(mainfram,width=30,text=cxk_list[2],command=lambda:play_cxk(2)).place(x=50+2*100, y=300+50*0, width=100, height=50)
tk.Button(mainfram,width=30,text=cxk_list[3],command=lambda:play_cxk(3)).place(x=50+3*100, y=300+50*0, width=100, height=50)
tk.Button(mainfram,width=30,text=cxk_list[4],command=lambda:play_cxk(4)).place(x=50+0*100, y=300+50*1, width=100, height=50)
tk.Button(mainfram,width=30,text=cxk_list[5],command=lambda:play_cxk(5)).place(x=50+1*100, y=300+50*1, width=100, height=50)
tk.Button(mainfram,width=30,text=cxk_list[6],command=lambda:play_cxk(6)).place(x=50+2*100, y=300+50*1, width=100, height=50)
tk.Button(mainfram,width=30,text=cxk_list[7],command=lambda:play_cxk(7)).place(x=50+3*100, y=300+50*1, width=100, height=50)
tk.Button(mainfram,width=30,text=cxk_list[8],command=lambda:play_cxk(8)).place(x=50+0*100, y=300+50*2, width=100, height=50)
tk.Button(mainfram,width=30,text=cxk_list[9],command=lambda:play_cxk(9)).place(x=50+1*100, y=300+50*2, width=100, height=50)
tk.Button(mainfram,width=30,text=cxk_list[10],command=lambda:play_cxk(10)).place(x=50+2*100, y=300+50*2, width=100, height=50)
tk.Button(mainfram,width=30,text=cxk_list[11],command=lambda:play_cxk(11)).place(x=50+3*100, y=300+50*2, width=100, height=50)
tk.Button(mainfram,width=30,text=cxk_list[12],command=lambda:play_cxk(12)).place(x=50+0*100, y=300+50*3, width=100, height=50)
tk.Button(mainfram,width=30,text=cxk_list[13],command=lambda:play_cxk(13)).place(x=50+1*100, y=300+50*3, width=100, height=50)
tk.Button(mainfram,width=30,text=cxk_list[14],command=lambda:play_cxk(14)).place(x=50+2*100, y=300+50*3, width=100, height=50)
tk.Button(mainfram,width=30,text=cxk_list[15],command=lambda:play_cxk(15)).place(x=50+3*100, y=300+50*3, width=100, height=50)
play_audio(0)
gif_0_start()
root.mainloop()
if __name__ == "__main__":
cxk_gui()
mixer.music.stop()
try:
sys.exit()
except:
pass
附录:列表的赋值类型和py打包
列表赋值
BUG复现
闲来无事写了个小程序 代码如下:
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 19 19:47:01 2021
@author: 16016
"""
a_list = ['0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15']
#print(len(a_list))
#b_list = ['','','','','','','','','','','','','','','','']
c_list = [[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]
#for i in range(16):
if len(a_list):
for j in range(16):
a_list[j]=str(a_list[j])+'_'+str(j)
print("序号:",j)
print('a_list:\n',a_list)
c_list[j]=a_list
print('c_list[0]:\n',c_list[0])
print('\n')
# b_list[j]=a_list[7],a_list[8]
# print(b_list[j])
# 写入到Excel:
#print(c_list,'\n')
我在程序中 做了一个16次的for循环 把列表a的每个值后面依次加上"_"和循环序号
比如循环第x次 就是把第x位加上_x 这一位变成x_x 我在输出测试中 列表a的每一次输出也是对的
循环16次后列表a应该变成[‘0_0’, ‘1_1’, ‘2_2’, ‘3_3’, ‘4_4’, ‘5_5’, ‘6_6’, ‘7_7’, ‘8_8’, ‘9_9’, ‘10_10’, ‘11_11’, ‘12_12’, ‘13_13’, ‘14_14’, ‘15_15’] 这也是对的
同时 我将每一次循环时列表a的值 写入到空列表c中 比如第x次循环 就是把更改以后的列表a的值 写入到列表c的第x位
第0次循环后 c[0]的值应该是[‘0_0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘10’, ‘11’, ‘12’, ‘13’, ‘14’, ‘15’] 这也是对的
但是在第1次循环以后 c[0]的值就一直在变 变成了c[x]的值
相当于把c_list[0]变成了c_list[1]…以此类推 最后得出的列表c的值也是每一项完全一样
我不明白这是怎么回事
我的c[0]只在第0次循环时被赋值了 但是后面它的值跟着在改变
如图:
第一次老出bug 赋值以后 每次循环都改变c[0]的值 搞了半天都没搞出来
无论是用appen函数添加 还是用二维数组定义 或者增加第三个空数组来过渡 都无法解决
代码改进
后来在我华科同学的指导下 突然想到赋值可以赋的是个地址 地址里面的值一直变化 导致赋值也一直变化 于是用第二张图的循环套循环深度复制实现了
代码如下:
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 19 19:47:01 2021
@author: 16016
"""
a_list = ['0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15']
#print(len(a_list))
#b_list = ['','','','','','','','','','','','','','','','']
c_list = [[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]
#for i in range(16):
if len(a_list):
for j in range(16):
a_list[j]=str(a_list[j])+'_'+str(j)
print("序号:",j)
print('a_list:\n',a_list)
for i in range(16):
c_list[j].append(a_list[i])
print('c_list[0]:\n',c_list[0])
print('\n')
# b_list[j]=a_list[7],a_list[8]
# print(b_list[j])
# 写入到Excel:
print(c_list,'\n')
解决了问题
优化
第三次是请教了老师 用copy函数来赋真值
代码如下:
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 19 19:47:01 2021
@author: 16016
"""
a_list = ['0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15']
#print(len(a_list))
#b_list = ['','','','','','','','','','','','','','','','']
c_list = [[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]
#for i in range(16):
if len(a_list):
for j in range(16):
a_list[j]=str(a_list[j])+'_'+str(j)
print("序号:",j)
print('a_list:\n',a_list)
c_list[j]=a_list.copy()
print('c_list[0]:\n',c_list[0])
print('\n')
# b_list[j]=a_list[7],a_list[8]
# print(b_list[j])
# 写入到Excel:
#print(c_list,'\n')
同样能解决问题
最后得出问题 就是指针惹的祸!
a_list指向的是个地址 而不是值 a_list[i]指向的才是单个的值 copy()函数也是复制值而不是地址
如果这个用C语言来写 就直观一些了 难怪C语言是基础 光学Python不学C 遇到这样的问题就解决不了
C语言yyds Python是什么垃圾弱智语言
总结
由于Python无法单独定义一个值为指针或者独立的值 所以只能用列表来传送
只要赋值是指向一个列表整体的 那么就是指向的一个指针内存地址 解决方法只有一个 那就是将每个值深度复制赋值(子列表内的元素提取出来重新依次连接) 或者用copy函数单独赋值
如图测试:
部分代码:
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 20 16:45:48 2021
@author: 16016
"""
def text1():
A=[1,2,3]
B=[[],[],[]]
for i in range(len(A)):
A[i]=A[i]+i
B[i]=A
print(B)
def text2():
A=[1,2,3]
B=[[],[],[]]
A[0]=A[0]+0
B[0]=A
print(B)
A[1]=A[1]+1
B[1]=A
print(B)
A[2]=A[2]+2
B[2]=A
print(B)
if __name__ == '__main__':
text1()
print('\n')
text2()
py打包
Pyinstaller打包exe(包括打包资源文件 绝不出错版)
依赖包及其对应的版本号
PyQt5 5.10.1
PyQt5-Qt5 5.15.2
PyQt5-sip 12.9.0
pyinstaller 4.5.1
pyinstaller-hooks-contrib 2021.3
Pyinstaller -F setup.py 打包exe
Pyinstaller -F -w setup.py 不带控制台的打包
Pyinstaller -F -i xx.ico setup.py 打包指定exe图标打包
打包exe参数说明:
-F:打包后只生成单个exe格式文件;
-D:默认选项,创建一个目录,包含exe文件以及大量依赖文件;
-c:默认选项,使用控制台(就是类似cmd的黑框);
-w:不使用控制台;
-p:添加搜索路径,让其找到对应的库;
-i:改变生成程序的icon图标。
如果要打包资源文件
则需要对代码中的路径进行转换处理
另外要注意的是 如果要打包资源文件 则py程序里面的路径要从./xxx/yy换成xxx/yy 并且进行路径转换
但如果不打包资源文件的话 最好路径还是用作./xxx/yy 并且不进行路径转换
def get_resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)
而后再spec文件中的datas部分加入目录
如:
a = Analysis(['cxk.py'],
pathex=['D:\\Python Test\\cxk'],
binaries=[],
datas=[('root','root')],
hiddenimports=[],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
而后直接Pyinstaller -F setup.spec即可
如果打包的文件过大则更改spec文件中的excludes 把不需要的库写进去(但是已经在环境中安装了的)就行
这些不要了的库在上一次编译时的shell里面输出
比如:
然后用pyinstaller --clean -F 某某.spec