change state of action buttons on the toolbar in KivyMD

355 Views Asked by At

I'm trying to write an program for the phone in python using kivy md. The program dynamically adds modules to a page to be counted. I've already got that part sorted in simple kivy.
I'm now trying to replicate it in KivyMD because I can use a navigation drawer and it's screen manager. The program has 2 screens.
Is there way to change the state of the buttons in the action items according to the screen that is active?
So I'm trying to have 2 overlapped buttons on the right side of the toolbar and show button1 when screen 1 is active and hide button2 and vice-versa.
I used the example from KivyMD documentation on the NavDrawer with the Screen manager and built on that. The left action items is used for the navigation drawer and I am trying to add 2 more buttons on the right side an change their state according to the active screen.
I only managed to add one but I don't know how to add the second one and how to change their state.
Please see below the code I am using:

  1. python file

     from kivy.lang import Builder
     from kivy.properties import ObjectProperty
     from kivymd.uix.boxlayout import MDBoxLayout
     from kivymd.app import MDApp
     from kivymd.uix.scrollview import MDScrollView
    
    
     class ContentNavigationDrawer(MDScrollView):
         screen_manager = ObjectProperty()
         nav_drawer = ObjectProperty()
    
    
     class AbacApp(MDApp):
         def build(self):
             self.theme_cls.primary_palette = "Orange"
             self.theme_cls.theme_style = "Dark"
             return Builder.load_file('main.kv')
    
    
     if __name__ == '__main__':
         AbacApp().run()
    
  2. kv file

     <ContentNavigationDrawer>
    
         MDBoxLayout:
             orientation: "vertical"
    
             MDAnchorLayout:
                 anchor_y: "top"
                 MDList:
                     TwoLineAvatarListItem:
                         text: "Abacu' lu' Lysu"
                         secondary_text: "Lisandru numara"
                         ImageLeftWidget:
                             source: "img.png"
    
             MDList:
    
                 OneLineListItem:
                     text: "Module"
                     on_press:
                         root.nav_drawer.set_state("close")
                         root.screen_manager.current = "scr 1"
    
                 OneLineListItem:
                     text: "Printare"
                     on_press:
                         root.nav_drawer.set_state("close")
                         root.screen_manager.current = "scr 2"
    
    
     MDScreen:
         MDBottomAppBar:
             MDTopAppBar:
                 pos_hint: {"bottom": 1}
                 elevation: 4
                 icon: "plus"
                 title: "Abacu' lu' Lisandru"
                 left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]
                 on_action_button:
                     id: add_module
                     app.callback()
                 on_action_button:
                     id: print_modules
                     app.callback()
                 mode: "end"
    
         MDNavigationLayout:
    
             MDScreenManager:
                 id: screen_manager
    
                 MDScreen:
                     name: "scr 1"
    
                     MDLabel:
                         text: "Screen 1"
                         halign: "center"
    
                 MDScreen:
                     name: "scr 2"
    
                     MDLabel:
                         text: "Screen 2"
                         halign: "center"
    
    
             MDNavigationDrawer:
                 id: nav_drawer
                 radius: (0, 16, 16, 0)
    
                 ContentNavigationDrawer:
                     screen_manager: screen_manager
                     nav_drawer: nav_drawer
    
0

There are 0 best solutions below