我在使用Kivy,omxplayer + screenmanager,imagebutton时遇到了一些麻烦

时间:2018-10-08 10:26:46

标签: python kivy raspberry-pi3 kivy-language omxplayer

我是该论坛的新手,最近开始学习python和kivy。在我的程序中,我想制作2个屏幕:1-主屏幕,2-带滚动按钮的视频播放器。 我想在第二个屏幕上单击其他图像按钮时可以观看视频。 我在第二个屏幕上遇到问题。

我的代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fileencoding=utf-8
import kivy
kivy.require('1.11.0')
import os
import subprocess

from kivy.app import App 
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.button import Label
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.properties import StringProperty
from kivy.clock import Clock
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.image import Image
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.scrollview import ScrollView
from kivy.config import Config
from kivy.core.text import Label
from kivy.core.text.text_layout import layout_text
from omxplayer.player import OMXPlayer
from kivy.properties import ObjectProperty, BooleanProperty
import kivy.core.window
from random import randint
from functools import partial

Builder.load_string('''
#:kivy 1.11.0
<DemoApp>:

<ScreenOne>:
    name: 'screen1'
    canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
            source: '1.png'
    BoxLayout:
        size_hint: 0.6, 0.6
        pos_hint: {'center_x':.5, 'center_y':.5}
        Label:
            text: "Для старта нажмите на указатель"
            text_size: self.size
            font_size: '40sp'
            pos_hint: {'center_x':.5, 'center_y':.8}
        ImageButton:
            source: '4.png'
            size_hint: 0.5, 0.5
            pos_hint: {'center_x':.2, 'center_y':.5}
            font_size: 150
            on_press: root.manager.current = 'screen2'

<ScreenTwo>:
    name: 'screen2'
    canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
            source: '32.png'
    Label:
        text: 'второй2'
        font_size: 30
        background_color:(0,1,0,1)

    FloatLayout:
        size_hint: 1, 0.1
        pos_hint: {'bottom_x':.2, 'bottom_y':.2}
        ScrollView:
            scroll_distance: "30dp"
            do_scroll_y: False
            do_scroll_x: True
            effect_cls: "ScrollEffect"
            GridLayout:
                rows: 1
                size_hint_x: "4dp"
                orientation: "horizontal"
                spacing: "8dp"
                ImageButton:
                    source: '4.png'
                    text: "test1"
                    size_hint: 2, 0.2
                    on_press: root.self.play()
                ImageButton:
                    source: '4.png'
                    text: "test1"
                    size_hint: 2, 0.2
                    on_press: root.self.play()
                ImageButton:
                    source: '4.png'
                    text: "test1"
                    size_hint: 2, 0.2
                    on_press: root.self.play()
                ImageButton:
                    source: '4.png'
                    text: "test1"
                    size_hint: 2, 0.2
                    on_press: root.self.play()
                ImageButton:
                    source: '4.png'
                    text: "test1"
                    size_hint: 2, 0.2
                    on_press: root.self.play()
                ImageButton:
                    source: '4.png'
                    text: "test1"
                    size_hint: 2, 0.2
                    on_press: root.self.play()

    Button:
        text: "Главный экран"
        font_size: 16
        size:(130,50)
        pos_hint: {'top':1}
        size_hint:(None,None)
        on_press: root.manager.current = 'screen1'

<Manager>:
    id: screen_manager

    screen_one: screen_one
    screen_two: screen_two

    ScreenOne:
        id: screen_one
        name: 'screen1'
        manager: screen_manager

    ScreenTwo:
        id: screen_two
        name: 'screen2'
        manager: screen_manager

    ''')      

class ImageButton(ButtonBehavior, Image):

    def __init__(self, **kwargs):
        super(ImageButton, self).__init__(**kwargs)
        self.add_widget(VideoPlayer())   

class VideoPlayer(Screen):

    def __init__(self, instance, **kwargs):
        self.playing = False
        super(ScreenTwo, self).__init__(**kwargs)
        self.add_widget(ScreenTwo())

    def play(self, instance, **kwargs):
        print kwargs['filepath']
        self._stop(instance)
        if not self.playing:
            #self.process = subprocess.Popen('/usr/bin/omxplayer --no-osd --win 10,10,600,600 "{0}"'.format(kwargs['filepath']), shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
            self.process = subprocess.Popen('/usr/bin/omxplayer --no-osd --win 10,10,600,600 "{0}"' .format('/home/pi/kivy/kivy/3.mp4'), shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
            self.playing = True    

    def _stop(self, instance):
        if self.playing:
            self.process.stdin.write('q')
            self.playing = False
            self.pause_button.text = 'Pause'
            os.system('killall omxplayer.bin')

    def _pause(self, instance):
        if self.playing:
            self.process.stdin.write('p')

            if instance.text == 'Play':
                instance.text = 'Pause'
            else:
                instance.text = 'Play'

    def on_stop(self):
        print 'Exiting...'
        self._stop(self)


class ScreenOne(Screen):
    pass

class ScreenTwo(Screen):
    pass        

class Manager(ScreenManager):
    screen_one = ObjectProperty(None)
    screen_two = ObjectProperty(None)

class DemoApp(App):
    def build(self):
        m = Manager(transition=FadeTransition())
        m.add_widget(ScreenTwo())
        return m

if __name__ == "__main__":
    try:
        DemoApp().run()
    except KeyboardInterrupt:
        DemoApp().stop()

这是我的终端显示的内容:

Traceback (most recent call last):
   File "DemoApp3.py", line 197, in <module>
     DemoApp().run()
   File "/home/pi/kivy/kivy/app.py", line 826, in run
     runTouchApp()
   File "/home/pi/kivy/kivy/base.py", line 502, in runTouchApp
     EventLoop.window.mainloop()
   File "/home/pi/kivy/kivy/core/window/window_egl_rpi.py", line 92, in mainloop
     self._mainloop()
   File "/home/pi/kivy/kivy/core/window/window_egl_rpi.py", line 87, in _mainloop
     EventLoop.idle()
   File "/home/pi/kivy/kivy/base.py", line 340, in idle
     self.dispatch_input()
   File "/home/pi/kivy/kivy/base.py", line 325, in dispatch_input
     post_dispatch_input(*pop(0))
   File "/home/pi/kivy/kivy/base.py", line 231, in post_dispatch_input
     listener.dispatch('on_motion', etype, me)
   File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
     return handler(*largs, **kwargs)
   File "/home/pi/kivy/kivy/core/window/__init__.py", line 1364, in on_motion
     self.dispatch('on_touch_up', me)
   File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
     return handler(*largs, **kwargs)
   File "/home/pi/kivy/kivy/core/window/__init__.py", line 1400, in on_touch_up
     if w.dispatch('on_touch_up', touch):
   File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
     return handler(*largs, **kwargs)
   File "/home/pi/kivy/kivy/uix/screenmanager.py", line 1201, in on_touch_up
     return super(ScreenManager, self).on_touch_up(touch)
   File "/home/pi/kivy/kivy/uix/widget.py", line 482, in on_touch_up
     if child.dispatch('on_touch_up', touch):
   File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
     return handler(*largs, **kwargs)
   File "/home/pi/kivy/kivy/uix/relativelayout.py", line 304, in on_touch_up
     ret = super(RelativeLayout, self).on_touch_up(touch)
   File "/home/pi/kivy/kivy/uix/widget.py", line 482, in on_touch_up
     if child.dispatch('on_touch_up', touch):
   File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
     return handler(*largs, **kwargs)
   File "/home/pi/kivy/kivy/uix/widget.py", line 482, in on_touch_up
     if child.dispatch('on_touch_up', touch):
   File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
     return handler(*largs, **kwargs)
   File "/home/pi/kivy/kivy/uix/scrollview.py", line 848, in on_touch_up
     if self.dispatch('on_scroll_stop', touch):
   File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
     return handler(*largs, **kwargs)
   File "/home/pi/kivy/kivy/uix/scrollview.py", line 887, in on_scroll_stop
     self.simulate_touch_down(touch)
   File "/home/pi/kivy/kivy/uix/scrollview.py", line 607, in simulate_touch_down
     ret = super(ScrollView, self).on_touch_down(touch)
   File "/home/pi/kivy/kivy/uix/widget.py", line 460, in on_touch_down
     if child.dispatch('on_touch_down', touch):
   File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
     return handler(*largs, **kwargs)
   File "/home/pi/kivy/kivy/uix/widget.py", line 460, in on_touch_down
     if child.dispatch('on_touch_down', touch):
   File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
     return handler(*largs, **kwargs)
   File "/home/pi/kivy/kivy/uix/behaviors/button.py", line 151, in on_touch_down
     self.dispatch('on_press')
   File "kivy/_event.pyx", line 703, in kivy._event.EventDispatcher.dispatch
     if observers.dispatch(self, None, largs, kwargs, 1):
   File "kivy/_event.pyx", line 1214, in kivy._event.EventObservers.dispatch
     result = self._dispatch(
   File "kivy/_event.pyx", line 1098, in kivy._event.EventObservers._dispatch
     return f(*fargs, **kwargs)
   File "/home/pi/kivy/kivy/lang/builder.py", line 64, in custom_callback
     exec(__kvlang__.co_value, idmap)
   File "<string>", line 56, in <module>
   File "kivy/weakproxy.pyx", line 30, in kivy.weakproxy.WeakProxy.__getattr__
     return getattr(self.__ref__(), name)
 AttributeError: 'ScreenTwo' object has no attribute 'play'

0 个答案:

没有答案