Scrollview在一段时间后以kivy滞后显示不同的视频

时间:2018-01-13 16:09:53

标签: python widget scrollview kivy kivy-language

我是kivy和python的初学者。我一直在尝试在屏幕上显示所选视频的页面中创建滚动视图。但过了一段时间,当我选择5-6个视频来显示,即使我删除视频小部件,它开始滞后。并且我愿意接受任何有关更好地处理此类应用程序的建议。

from kivy.config import Config

Config.set('graphics', 'width', '1920')
Config.set('graphics', 'height', '1080')
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.widget import Widget
from kivy.uix.image import Image
from kivy.properties import StringProperty
from kivy.uix.scrollview import ScrollView
from kivy.uix.label import Label
from kivy.clock import Clock
from kivy.uix.button import Button
import os


from kivy.uix.video import Video
from kivy.uix.videoplayer import VideoPlayer
from kivy.clock import mainthread
from functools import partial
from kivy.core.window import Window
Window.clearcolor = (0, 0, 0, 0)
isThereVideo=False
picture_path="/home/linux/kivyFiles/kivyLogin/assets"
video_path="/home/linux/kivyFiles/kivyLogin/videoAssets/"
class Image(Image):
    pass

class MainScreen(Screen):
    pass

class AnotherScreen(Screen):
    pass
class Selfie(Screen):
    pass    
class RootWidget(Widget):
    def __init__(self, **kwargs):
            super(RootWidget, self).__init__(**kwargs)
            Clock.schedule_once(self.createMultipleButton)

    @mainthread
    def createMultipleButton(self, dt):
            self.root = Widget()
            size_y=150;
            size_x=150;
            for i in range(1):
            folderList = os.listdir(picture_path)
            if len(folderList)==0:

                time.sleep(1)
                break
            fileList = os.listdir(picture_path)
            print fileList

            for file in fileList:


                x = (picture_path+"/"+file)

                    button = Button(id=str(file),text="" + str(file),size_hint=(None, None),height=size_y,width=size_x, pos_hint={'x': 0, 'y': 1},background_normal=x)
                    button.bind(on_release=partial(self.VideoContainer, file))      

                print file
                    self.scrollview.content_layout.add_widget(button)
                print button.id
                print("Parent of ScreenTwo: {}".format(self.parent)) 
                    #print(button.pos)

    def VideoContainer(self,name,btn):
        global isThereVideo
        if isThereVideo==True:
                #self.videocontainer.video_layout.unload()
                self.videocontainer.clear_widgets()



        mylist=name.split('.')
        emailButton = Button(id='email')
        video = Video(source="/home/linux/kivyFiles/kivyLogin/videoAssets/"+mylist[0]+".mp4", state='play',options={'eos': 'loop'})
        video.size=(self.parent.width,self.parent.height)
        video_pos=(self.parent.x,self.parent.y)
        #video.pos_hint={'x': self.parent.width /2, 'y': self.parent.height/2}

        video.play=True
        #video.pos=(self.parent.width /2 , self.parent.height/2)
        #self.videocontainer.video_layout.add_widget(emailButton)

        self.videocontainer.add_widget(emailButton)

        emailButton.add_widget(video)

        isThereVideo=True
        print("Parent of ScreenTwo: {}".format(self.parent)) 
        return 0

class ScreenManagement(ScreenManager):
    pass


class VideoContain(Widget):
    pass            


class ScrollableContainer(ScrollView):

    pass


presentation = Builder.load_file("login.kv")

class MainApp(App):
    def build(self):
        return presentation
if __name__ == '__main__':


    Window.fullscreen = True
    app=MainApp()
    app.run()

我的Kv文件

ScreenManagement:

    MainScreen:
    Selfie:


<MainScreen>:
    name: 'main'

    Button:
        on_release: app.root.current = 'selfie'
        text: 'Another Screen'
        font_size: 50



<Selfie>:
    name: 'selfie'

    Button:
        on_release: app.root.current = 'login'
        text: 'Selfie Screen'
        font_size: 10
        pos_hint: {"right": 1, 'top':1}
        size_hint: (0.1, 0.1)
    RootWidget:


<RootWidget>
    size_hint: (0.1, None)
    scrollview: scrollview
    videocontainer:videocontainer
    size:(self.parent.width, self.parent.height)
    VideoContain:
        id:videocontainer
        ##size:(self.parent.width, self.parent.height)
        size:(root.width, root.height)
    ScrollableContainer:
        id: scrollview
        size: root.size
        pos: root.pos


<VideoContain>

    video_layout:video_layout
    FloatLayout:
        cols:1
        id: video_layout



<ScrollableContainer>:
    scroll_timeout: 75
    scroll_distance: 10
    app: app
    content_layout: content_layout
    GridLayout:
        id: content_layout
        cols: 1
        size_hint: (0.1, None)
        pos: root.pos
        height: self.minimum_height
        padding: 25
        spacing: 25

我发布了所有代码,因为我不知道哪个部分可能导致我面临的问题。

1 个答案:

答案 0 :(得分:1)

解决了它。

def VideoContainer(self,name,btn):
    global isThereVideo
    if isThereVideo:
        # self.videocontainer.video_layout.unload()
        self.videocontainer.clear_widgets()

在这部分我正在清除小部件,但我还需要以某种方式卸载视频。

self.video.unload()解决了我的问题