# -*- coding: utf-8 -*-
import wx, wx.media
import sqlite3
import os
import time
class MainFream(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None)
self.leftPanel = wx.Panel(self, size=(0,0))
self.media = wx.media.MediaCtrl(self,
#szBackend=wx.media.MEDIABACKEND_GSTREAMER,
szBackend=wx.media.MEDIABACKEND_WMP10,
size=(0,0),
style=wx.SIMPLE_BORDER | wx.ALIGN_CENTER_HORIZONTAL)
#Penael Sizer
self.hBoxSizer = wx.BoxSizer(wx.HORIZONTAL)
self.hBoxSizer.Add(self.leftPanel, 1, wx.EXPAND, 1)
self.hBoxSizer.Add(self.media, 1, wx.EXPAND, 1)
self.SetSizer(self.hBoxSizer)
#Video Image File Folder Serach & Load Play
self.mediaCounter = 0
self.folderSerach = [f for f in os.listdir() if f.endswith(('.wmv','mp4','avi','.jpg'))]
self.media.Load(self.folderSerach[self.mediaCounter])
self.media.Bind(wx.media.EVT_MEDIA_LOADED, self.mediaPlay, self.media)
self.media.Bind(wx.media.EVT_MEDIA_FINISHED, self.mediaLoop, self.media)
def auto(self):
self.TextShow()
wx.CallLater(1000, self.auto) #60min
def mediaPlay(self, event):
self.folderSerach = [x for f in os.listdir() for x in os.path.splitext(f) if x.endswith(('.wmv','mp4','avi','.jpg'))]
for i in self.folderSerach:
if i == '.jpg':
self.media.Play()
time.sleep(10)
print('test1')
else:
self.media.Play()
print('test2')
def mediaLoop(self, evnet):
self.media.Stop()
self.mediaCounter += 1
if self.mediaCounter > len(self.folderSerach) -1: self.mediaCounter = 0
self.folderSerach = [f for f in os.listdir() if f.endswith(('.wmv','mp4','avi','.jpg'))]
self.media.Load(self.folderSerach[self.mediaCounter])
if __name__ == '__main__':
app = wx.App()
fream = MainFream()
fream.Show(True)
app.MainLoop()
我正在创建一个面板来搜索和播放文件夹中的jpg,avi,mp4等。
在def mediaPlay()
部分,jpg使图片文件可见10秒,并播放电影直到播放时间结束。
问题是if = '.jpg'
,只需要执行一个print ('test1')
语句,同时会显示'test2'。因此time.sleep()
适用于if or else
语句。这是为什么?
答案 0 :(得分:0)
在此功能中,您将使用文件后缀列表覆盖self.folderSerach
。
def mediaPlay(self, event):
self.folderSerach = [x for f in os.listdir() for x in os.path.splitext(f) if x.endswith(('.wmv','mp4','avi','.jpg'))]
for i in self.folderSerach:
if i == '.jpg':
self.media.Play()
time.sleep(10)
print('test1')
else:
self.media.Play()
print('test2')
将其替换为:
def mediaPlay(self, event):
if self.folderSerach[self.mediaCounter].endswith('.jpg'):
self.media.Play()
time.sleep(10)
print('test1')
else:
self.media.Play()
print('test2')
覆盖self.folderSerach
意味着您必须在def medialoop
中重新加载它(删除该代码)
此外,os.listdir()
(至少在Linux上)需要一个参数,即os.listdir('./')
并删除未使用的def auto
函数