primitiveProgrammer
primitiveProgrammer

Reputation: 151

Kivy: Navigation bar on all screens

I want to keep the Navigationbar on all my screen, I am able to switch screen and display what I want but I don't seem to keep the Navigationbar.

Also for some reason my side menu seems to appear twice, overlapping each other and I'm not sure why, I've tried refactoring the code and it just seems to appear again

Can anyone offer any help?

*.py

import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.properties import StringProperty
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.clock import Clock
from kivy.uix.button import Button
from kivy.core.window import Window
from kivymd.app import MDApp
from kivymd.uix.card import MDCard
from kivymd.uix.label import MDLabel

class FirstWindow(Screen):
    pass

class ThirdWindow(Screen):

    def on_pre_enter(self):
        anchor = AnchorLayout(size=(1,1))
        test = "This is the new window"
        card = MDCard(orientation='vertical',padding="8dp",size_hint=(1,0.5),pos_hint={'top': 0.1,'right':1},radius=[5, ])
        card.test = test
        card.add_widget(MDLabel(text=test, halign="center"))
        anchor.add_widget(card)
        self.anchorID.add_widget(anchor)
            

class WindowManager(ScreenManager):
    pass

class NearMeApp(MDApp):
    def build(self):
        self.theme_cls.theme_style ="Dark"
        self.theme_cls.accent_palette = "Red"
        self.theme_cls.primary_palette = "Purple"
        self.root = Builder.load_file('NearMe.kv')

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

*.kv

#:import hex kivy.utils.get_color_from_hex
#:kivy 1.10.1
WindowManager:
    FirstWindow:
    ThirdWindow:
<FirstWindow>:
    name:"FirstWindow"
    Screen:
        MDNavigationLayout:
            ScreenManager:
                Screen:
                    BoxLayout:
                        orientation: 'vertical'
                        MDToolbar:
                            title: "NearMe Application"
                            elevation: 10
                            left_action_items: [['menu', lambda x: nav_drawer.set_state("open")]]
                        Widget:
            MDNavigationDrawer:
                id: nav_drawer
                BoxLayout:
                    orientation:'vertical'
                Button:
                    text:"ThirdWindow"
                    on_release: root.manager.current = "ThirdWindow"
<ThirdWindow>:
    name:"ThirdWindow"      
    anchorID:anchorID

    AnchorLayout:
        id:anchorID
        canvas.before:
            Color:
                rgba: .2, .2, .2, 1
            Rectangle:
                pos: self.pos
                size: self.size

Upvotes: 0

Views: 2018

Answers (1)

Paman90
Paman90

Reputation: 118

Your md toolbar needs to be above the main windowmanager.

With your mdtoolbar as a child of firstwindow (child of windowmanager), your windowmanager acts on it and the other children of firstwindow at same during change in current window. By moving the mdtoolbar out of firstwindow and above windowmanager the mdtoolbar will remain present.

from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager
from kivy.uix.screenmanager import Screen


class FirstWindow(Screen):
    pass


class ThirdWindow(Screen):
    pass


class WindowManager(ScreenManager):
    pass


kv = """
Screen:
    MDNavigationDrawer:
        id: nav_drawer
        BoxLayout:
            orientation:'vertical'
            Button:
                text:"ThirdWindow"
                on_release: root.ids.manager.current = "ThirdWindow"
    StackLayout:
        MDToolbar:
            title: "NearMe Application"
            size_hint: (1, 0.1)
            elevation: 10
            left_action_items: [['menu', lambda x: nav_drawer.set_state("open")]]
        WindowManager:
            id: manager
            FirstWindow:
            ThirdWindow:

<FirstWindow>:
    name:"FirstWindow"
    Screen:
        BoxLayout:
            orientation: 'vertical'
            Widget:
<ThirdWindow>:
    name:"ThirdWindow"      
    anchorID:anchorID

    AnchorLayout:
        id:anchorID
        canvas.before:
            Color:
                rgba: .2, .2, .2, 1
            Rectangle:
                pos: self.pos
                size: self.size
"""


class Test(MDApp):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.screen = Builder.load_string(kv)

    def build(self):
        return self.screen


the_app = Test()
the_app.run()

The above removes the toolbar from your FirstWindow and places it on its own. In your example the toolbar is a child of FirstWindow so when you change the current window (screen) to ThirdWindow the toolbar disappears with its parent (FirstWindow). Now the toolbar is not a child of either window.

Upvotes: 1

Related Questions