弹出窗口中BoxLayout未发布的视频对象

2024-05-07 16:08:37 发布

您现在位置:Python中文网/ 问答频道 /正文

如标题所述,当我尝试显示包含一个视频和两个按钮的BoxLayout时,我被卡住了-我创建了一个应用程序,当用户单击一个按钮时,它将接收用户的输入我将显示一个包含一个视频和两个按钮的弹出窗口-现在的问题是,第一次单击它可以正常工作,但是第二次单击它时,它会显示以下错误:

kivy.uix.widget.WidgetException:无法在0x0000000005C7EB40处添加kivy.uix.video.video对象,它已在0x0000000005DB7B40处具有父kivy.uix.boxlayout.boxlayout对象

当我尝试处理异常,但在视频在后台播放并显示消息Cannot add kivy.uix.video.video object at 0x0000000005C7EB40后,它已经在0x0000000005DB7B40处有一个父kivy.uix.boxlayout.boxlayout对象

from __future__ import print_function
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.popup import Popup
from kivy.properties import StringProperty
from kivy.properties import ObjectProperty
from kivy.uix.progressbar import ProgressBar
from kivy.uix.video import Video
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import WidgetException
import re, wmi


kv = """
<MainPage>:
    manager:screen_mgr
    do_default_tab: False
    textinputtext: waybill.text
    ScreenManager:
        id: screen_mgr
        #transition: FadeTransition
        Screen:
            manager: screen_mgr
            id: intro
            orientation: 'horizontal'
            name: 'User'
            canvas.before:
                Rectangle:
                    pos: self.pos
                    size: self.size
                    source: 'index.png'
            AnchorLayout:
                anchor_x: 'center'
                anchor_y: 'center'
                BoxLayout:
                    orientation:'horizontal'
                    size_hint: .5, .1
                    canvas:
                        Color:
                            rgb: 1, 1, 1
                        Rectangle:
                            pos: self.pos
                            size: self.size
                    spacing: 20
                    pos_hint: {'center_x':.8, 'center_y': .8}
                    AnchorLayout:
                        anchor_x: 'left'
                        size_hint_x: .5
                        TextInput:
                            id: waybill
                            width: 20
                            text: root.textinputtext
                            multiline: False
                            height: self.minimum_height
                            size_hint_y: None
                            font_size: 30
                            focus: True
                            on_text: self.text
                            on_text_validate:app.on_waybill()
                    AnchorLayout:
                        anchor_x: 'right'
                        size_hint_x: None
                        Button:
                            size_hint: None, None
                            height: 50
                            width: self.texture_size[0]
                            padding: 10, 10
                            text: "Compare"
                            on_release: root.compareClicked()
            AnchorLayout:
                anchor_x: 'right'
                anchor_y: 'bottom'
                BoxLayout:
                    orientation:'horizontal'
                    size_hint: .5, .1
                    canvas:
                        Color:
                            rgb: 1, 1, 1
                        Rectangle:
                            pos: self.pos
                            size: self.size
                    spacing: 50
                    pos_hint: {'center_x':.8, 'center_y': .8}
                    AnchorLayout:
                        anchor_x: 'right'
                        size_hint_x: None
                        Button:
                            id: statebtn
                            size_hint: None, None
                            height: 50
                            width: self.texture_size[0]
                            padding: 10, 10
                            text: root.getusbdevices()
            AnchorLayout:
                anchor_x: 'left'
                anchor_y: 'top'
                BoxLayout:
                    orientation:'horizontal'
                    size_hint: .5, .1
                    canvas:
                        Color:
                            rgb: 1, 1, 1
                        Rectangle:
                            pos: self.pos
                            size: self.size
                    spacing: 50
                    pos_hint: {'center_x':.5, 'center_y': .5}
                    AnchorLayout:
                        anchor_x: 'right'
                        size_hint_x: None
                        Button:
                            size_hint: None, None
                            height: 50
                            width: self.texture_size[0]
                            padding: 10, 10
                            on_release:root.current = root.switch_screen()
                            text: "Admin"
    TabbedPanelHeader:
        text: intro.name
        # store a screen name to link the tab to a screen
        screen: intro.name
"""

Builder.load_string(kv)
waybill = TextInput(text="Enter Waybill No.", multiline=False)
adwaybill = TextInput(text="Enter Waybill No.", multiline=False)
statebtn = Button(text="INSTRUMENT_NOT_FOUND")
player1 = Video(source='connect.mp4')
player2 = Video(source='place.mp4')
waybills = []

class MainPage(TabbedPanel):

    screen_mgr = ObjectProperty(None)
    textinputtext = StringProperty()

    def __init__(self, **kwargs):
        super(MainPage, self).__init__(**kwargs)
        self.register_event_type('on_text')
        self.textinputtext = "Enter Waybill No."

    def on_text(self):
        return self.textinputtext

    def on_text_validate(self):
        return self.textinputtext

    def on_focus(self, obj, focused):
        if not focused:
            self.dispatch('on_text_validate')

    def compareClicked(self):
        self.progress_bar = ProgressBar()
        self.progress_bar.value = 1
        print (self.on_text())
        if self.on_text()!='Enter Waybill No.' and self.getusbdevices() == u"CONNECTED":
            self.popup_1()

    def popup_1(self):
        self.box = BoxLayout(orientation='vertical', padding=(10))
        self.box.add_widget(self.playPlace())
        popup = Popup(title='Warning: Please Place', title_size=(30),
                      title_align='center', content=self.box,
                      size_hint=(None, None), size=(400, 400),
                      auto_dismiss=False)
        self.box.add_widget(Button(text="YES TO COMPARE", on_release=popup.dismiss))
        self.box.add_widget(Button(text="NO TO GO BACK", on_release=popup.dismiss))
        popup.bind()
        popup.open()

    def playConnect(self):
        player1.state = 'play'
        return player1

    def playPlace(self):
        player2.state = 'play'
        return player2

    def getusbdevices(self):
        c = wmi.WMI()
        statebtn.text = u"INSTRUMENT_NOT_FOUND"
        wql = "Select * From Win32_USBControllerDevice"
        for item in c.query(wql):
            if "USB Device" in item.Dependent.Caption:
                statebtn.text = u"CONNECTED"
            else:
                statebtn.text = u"INSTRUMENT_NOT_FOUND"
        return statebtn.text

    def switch_screen(self):
        self.manager.transition = FadeTransition()
        self.manager.current = self.manager.previous()
        return self.manager.current

    def switch_to(self, header):
        self.manager.current = header.screen
        self.current_tab.state = "normal"
        header.state = 'down'
        self._current_tab = header

class KartScanApp(App):
    def build(self):
        return MainPage()

    def on_waybill(self):
        waybill.bind(text=MainPage.on_text)
        return waybill.text

if __name__ == '__main__':
    KartScanApp().run()

上面是代码,我漏了什么


Tags: textfromposimportselfnonesizeon
1条回答
网友
1楼 · 发布于 2024-05-07 16:08:37

我知道问题出在哪里了,视频对象不是全局发布的

所以我将这两个声明player1=Video(source='connect.mp4')、player2=Video(source='place.mp4')移到了各自的方法中

相关问题 更多 >