Python-Kivy:如何在屏幕上使用Z索引?

时间:2018-08-03 10:15:29

标签: python kivy

我开发了一个带有多个屏幕和导航菜单的小型应用程序。我将不同的屏幕分成几个文件,然后使用screenmanager对其进行调用。我遇到的问题是,当我添加带有按钮或标签面板之类的小部件的屏幕时,它们会隐藏已经存在的内容,包括导航菜单。这是图像上的结果:

Sample of hidden nav_menu

但是当我使用导航菜单和所有屏幕创建单个文件时,我没有这个问题。

这是我的代码:

main.py

import os
import sys

from kivy.app import App
from kivymd.theming import ThemeManager
from kivy.properties import ObjectProperty, StringProperty
from kivy.core.window import Window
from uix.startscreen import StartScreen

from kivy.lang import Builder

class _App(App):
    title = 'Inventory'
    theme_cls = ThemeManager()
    nav_drawer = ObjectProperty()

    def __init__(self, **kvargs):
        super(_App, self).__init__(**kvargs)
        Window.bind(on_keyboard=self.events_program)
        Window.soft_input_mode = 'below_target'

        self.window = Window
        self.manager = None
        self.list_previous_screens = ['base']

    def build(self):
        self.load_all_kv_files(os.path.join(self.directory, 'uix', 'kv'))
        self.screen = StartScreen()
        self.manager = self.screen.ids.manager
        self.nav_drawer = self.screen.ids.nav_drawer
        return self.screen

    def load_all_kv_files(self, directory_kv_files):
        for kv_file in os.listdir(directory_kv_files):
            kv_file = os.path.join(directory_kv_files, kv_file)
            if os.path.isfile(kv_file):
                with open(kv_file, encoding='utf-8') as kv:
                    Builder.load_string(kv.read())

    def back_screen(self, event=None):
        bis = None
        if event in (1001, 27):
            if self.manager.current == 'base':
                return
            bis = self.list_previous_screens.pop()
            self.manager.current = self.list_previous_screens[-1] 
            if self.manager.current == 'base':
                self.screen.ids.action_bar.title = self.title
                self.screen.ids.action_bar.left_action_items = \
                [['menu', lambda x: self.root.toggle_nav_drawer()]]

    def add_name_previous_screen(self):
        name_current_screen = self.manager.current
        try:
            if self.list_previous_screens[-1] == name_current_screen:
                return
        except IndexError:
            pass
        self.list_previous_screens.append(name_current_screen)

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

startscreen.kv

#:import Toolbar kivymd.toolbar.Toolbar
#:import BaseScreen uix.basescreen.BaseScreen
#:import About uix.about.About
#:import MDNavigationDrawer kivymd.navigationdrawer.MDNavigationDrawer
#:import NavigationLayout kivymd.navigationdrawer.NavigationLayout
#:import NavigationDrawerToolbar
kivymd.navigationdrawer.NavigationDrawerToolbar

<StartScreen>
    id: nav_layout
    on_state: app.add_name_previous_screen()
    MDNavigationDrawer:
        id: nav_drawer
        NavigationDrawerToolbar:
            title: "Navigation Drawer"
        NavigationDrawerIconButton:
            text: 'Base'
        NavigationDrawerIconButton:
            text: 'About'
            on_release: app.manager.current = 'about'
        NavigationDrawerIconButton:
            icon: 'close-circle'
            text: 'close'

    BoxLayout:
        orientation: 'vertical'
        spacing: dp(5)

        Toolbar:
            id: action_bar
            background_color: app.theme_cls.primary_color
            title: app.title
            left_action_items:\
            [['menu', lambda x: app.root.toggle_nav_drawer()]]
            elevation: 10
            md_bg_color: app.theme_cls.primary_color

        ScreenManager:
            id: manager

            BaseScreen:
                id: base

            About:
                id: about

about.kv

<About>:
    name: 'about'

    FloatLayout:

        BoxLayout:
            id: box
            orientation: 'vertical'
            padding: dp(0), dp(10)

            Widget:

            Button:
                text: 'Button'

startscreen.py about.py 仅包含分别继承 NavigationLayout Screen 的类。

我读到,可以在基维中将Z索引与小部件一起使用。屏幕也可以吗?我该如何解决这个问题?

谢谢。

0 个答案:

没有答案