python图片播放器能够定时播放图片,现在功能比较简单,如果阅读后感兴趣,请留言,我能根据需要增加功能.
建立图片文件夹"pic"放入停止,播放按钮
建立图片文件夹"demo"放入png图片
# -*- coding: utf-8 -*-
import os
from tkinter import *
import datetime
import time
from random import randint
k=0
class baseFrame: #模板类
def __init__(self, master,user,framename):
self.root = master
self.user=user
self.framename=framename
#self.root.resizable(0,0)
self.root.title(self.framename+self.user)
width =800
height = 600
screenwidth = self.root.winfo_screenwidth()
screenheight = self.root.winfo_screenheight()
alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth-width)/2, (screenheight-height)/2)
self.root.geometry(alignstr)
#self.root.iconbitmap(".\\pic\\smart.ico")
self.root.wm_attributes("-topmost", 1)
self.baseframe_ui()
def baseframe_ui(self):
self.one = Label(self.root, text="© Powered by Will Wu "+"2020-"+str(datetime.datetime.now().year), width=30, height=2, font=("Arial", 10))
self.one.pack(side=BOTTOM)
class show_wi(baseFrame): #显示类
def __init__(self,master,user,framename):
super( ).__init__(master,user,framename)
self.photo_play=PhotoImage(file=os.getcwd()+".\\pic\\play.png")
self.photo_stop=PhotoImage(file=os.getcwd()+".\\pic\\stop.png")
self.add_ui()
self.files=self.file_list()
def add_ui(self):
self.button_back=Button(self.root,image=self.photo_play,command=self.auto_update)
self.button_back.place(relx=0.8,rely=0.91)
self.button_back=Button(self.root,image=self.photo_stop,command=self.cancel_auto)
self.button_back.place(relx=0.9,rely=0.91)
def file_list(self):
filelist=[]
path=os.getcwd()+"\\demo\\"
for i in os.listdir(path):
f=os.path.join(path,i)
if f.endswith("PNG"):
filelist.append(i)
return filelist
def play(self,filename):
self.demo=PhotoImage(file=os.getcwd()+"\\demo\\"+filename)
self.Label = Label(self.root,image=self.demo,fg="white")
self.Label.place(relx=0.2,rely=0.2)
def auto_update(self):
global k
global up
if k<len(self.files):
self.play(self.files[k])
k=k+1
up=self.root.after(500,self.auto_update)
else:
k=0
up=self.root.after(500,self.auto_update)
def cancel_auto(self):
root.after_cancel(up)
if __name__ == '__main__':
root = Tk()
show_wi(root,"usertest","图片播放")
root.mainloop()
`