使用启动画面实现 Kivy 应用程序时出错

Error while implementing a Kivy App with Splash Screen

我正在 Python 3 中使用 Kivy 实现基于 GUI 的应用程序。我的应用程序需要有 2 个屏幕。第一个是启动画面,它会停留 5 秒,然后必须出现第二个画面。第二个屏幕是 Kivy 的 Garden FileBrowser。

我尝试按如下所示实现它..

class PgSplash(Screen):


    def skip(self, dt):
        screen.switch_to(pages[1])

    def on_enter(self, *args):
        Clock.schedule_once(self.skip, 5)


class PgBrowser(Screen):

    def on_pre_enter(self, *args):
        user_path = os.path.join(browser_base.get_home_directory(), 'Documents')
        browser = browser_base.FileBrowser(select_string='Select',
                              favorites=[(user_path, 'Documents')])
        browser.bind(on_success=self._fbrowser_success,
                     on_canceled=self._fbrowser_canceled,
                     on_submit=self._fbrowser_submit)

        self.add_widget(browser)


    def _fbrowser_canceled(self, instance):
        print('cancelled, Close self.')
        self.root_window.hide()
        sys.exit(0)


    def _fbrowser_success(self, instance): # select pressed
        global file

        print(instance.selection)

        file = instance.selection[0]


    def _fbrowser_submit(self, instance): # clicked on the file
        global file

        print(instance.selection)

        file = instance.selection[0]

pages = [PgSplash(name="PgSplash"),
             PgBrowser(name="PgBrowser")]

screen = ScreenManager()
screen.add_widget(pages[0])


class myApp(App):
    def build(self):
        screen.current = "PgSplash"
        return screen

myApp().run()

虽然 运行 启动画面出现并且程序崩溃并给出如下所示的错误..

 Traceback (most recent call last):
   File "kivy\properties.pyx", line 836, in kivy.properties.ObservableDict.__getattr__ (kivy\properties.c:12509)
 KeyError: 'icon_view'

 During handling of the above exception, another exception occurred:

 Traceback (most recent call last):
   File "C:/Users/Spectre/Desktop/Project/base.py", line 219, in <module>
     myApp().run()
   File "F:\Anaconda3\lib\site-packages\kivy\app.py", line 828, in run
     runTouchApp()
   File "F:\Anaconda3\lib\site-packages\kivy\base.py", line 504, in runTouchApp
     EventLoop.window.mainloop()
   File "F:\Anaconda3\lib\site-packages\kivy\core\window\window_sdl2.py", line 663, in mainloop
     self._mainloop()
   File "F:\Anaconda3\lib\site-packages\kivy\core\window\window_sdl2.py", line 405, in _mainloop
     EventLoop.idle()
   File "F:\Anaconda3\lib\site-packages\kivy\base.py", line 339, in idle
     Clock.tick()
   File "F:\Anaconda3\lib\site-packages\kivy\clock.py", line 581, in tick
     self._process_events()
   File "kivy\_clock.pyx", line 367, in kivy._clock.CyClockBase._process_events (kivy\_clock.c:7700)
   File "kivy\_clock.pyx", line 397, in kivy._clock.CyClockBase._process_events (kivy\_clock.c:7577)
   File "kivy\_clock.pyx", line 395, in kivy._clock.CyClockBase._process_events (kivy\_clock.c:7498)
   File "kivy\_clock.pyx", line 167, in kivy._clock.ClockEvent.tick (kivy\_clock.c:3490)
   File "C:\Users\Spectre\Desktop\Project\browser_base.py", line 320, in _post_init
     self.ids.icon_view.bind(selection=partial(self._attr_callback, 'selection'),
   File "kivy\properties.pyx", line 839, in kivy.properties.ObservableDict.__getattr__ (kivy\properties.c:12654)
 AttributeError: 'super' object has no attribute '__getattr__'

这是什么问题?我做错了什么?如何解决这个问题?

解决方案

我可以通过执行以下操作让应用程序正常工作并删除了 self.root_window.hide() 因为“AttributeError: 'PgBrowser' 对象没有属性 'root_window'”。我的环境是 Ubuntu 16.04 LTS、Python 3.5 和 Kivy 1.10.0

片段

class PgBrowser(Screen):
    def on_pre_enter(self, *args):
        if sys.platform == 'win':
            user_path = dirname(expanduser('~')) + sep + 'Documents'
        else:
            user_path = expanduser('~') + sep + 'Documents'
        browser = FileBrowser(select_string='Select',
                              favorites=[(user_path, 'Documents')])

例子

main.py

import sys
from os.path import sep, expanduser, dirname
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.clock import Clock
from kivy.garden.filebrowser import FileBrowser

file = ""


class PgSplash(Screen):
    def skip(self, dt):
        self.manager.current = "PgBrowser"

    def on_enter(self, *args):
        Clock.schedule_once(self.skip, 5)


class PgBrowser(Screen):
    def on_pre_enter(self, *args):
        if sys.platform == 'win':
            user_path = dirname(expanduser('~')) + sep + 'Documents'
        else:
            user_path = expanduser('~') + sep + 'Documents'
        browser = FileBrowser(select_string='Select',
                              favorites=[(user_path, 'Documents')])
        browser.bind(on_success=self._fbrowser_success,
                     on_canceled=self._fbrowser_canceled,
                     on_submit=self._fbrowser_submit)
        self.add_widget(browser)

    def _fbrowser_canceled(self, instance):
        print('cancelled, Close self.')
        sys.exit(0)

    def _fbrowser_success(self, instance):  # select pressed
        global file
        print(instance.selection)
        file = instance.selection[0]

    def _fbrowser_submit(self, instance):   # clicked on the file
        global file
        print(instance.selection)
        file = instance.selection[0]


class MyScreenManager(ScreenManager):
    pass


class TestApp(App):
    def build(self):
        return MyScreenManager()


if __name__ == "__main__":
    TestApp().run()

test.kv

#:kivy 1.10.0

<MyScreenManager>:
    PgSplash:
    PgBrowser:

<PgSplash>:
    name: 'PgSplash'
    AnchorLayout:
        Image:
            source: 'kivyLogo.png'
            size_hint: 1,1

<PgBrowser>:
    name: 'PgBrowser'

输出