【问题标题】:Minimize my code with My TOOLBAR and SCREEN MANAGER使用 My TOOLBAR 和 SCREEN MANAGER 最小化我的代码
【发布时间】:2021-11-24 14:51:36
【问题描述】:

我尝试减少我的代码。我正在使用 kivyMD。我在 MDNavigationLayout 中创建了一个带有菜单的 MDToolbar。我还使用 ScreenManager 创建了不同的屏幕。

喜欢:家庭、个人资料、联系人。

问题是我将 MDToolbar 的所有代码复制/粘贴到我的 kv 文件中的每个屏幕中。

但我只想为我的所有屏幕设置一个“标题”。而且阅读文档对我来说并不容易......我没有找到我想要的。

所以,我把代码分享给你:

Python 代码:

class HomeScreen(Screen):
    pass

class ProfilScreen(Screen):
    pass

class ContactScreen(Screen):
    pass

sm = ScreenManager()
sm.add_widget(HomeScreen(name='home'))
sm.add_widget(ProfilScreen(name='profil'))
sm.add_widget(ContactScreen(name='contact'))



class DemoApp(MDApp):
    def build(self):
        self.theme_cls.primary_palette = 'Red'
        screen = Builder.load_string(navigation_helper)
        return screen

    def nav_drawer(self):
        pass

DemoApp().run()

KV代码:

ScreenManager:
    HomeScreen:
    ProfilScreen:
    ContactScreen:


<HomeScreen>:
    name: 'home'

    BoxLayout:
        orientation: 'vertical'
        padding: 35

        MDLabel:
            text: "My Content"

    Screen:
        MDNavigationLayout:
            ScreenManager:
                Screen:
                    BoxLayout:
                        orientation: 'vertical'

                        MDToolbar:
                            title: 'Demo Application'
                            left_action_items: [["menu", lambda x: nav_drawer.set_state('toggle')]]
                            elevation: 10
                        Widget:
                
        
            MDNavigationDrawer:
                id: nav_drawer
                BoxLayout:
                    orientation: 'vertical'
                    spacing: '8dp'
                    padding: '8dp'

                    Image:
                        source: 'img\logo.png'
                        size_hint: .5, .5
                        pos_hint: {"center_x": 0.5}

                    MDLabel:
                        text: 'Le Réseau Foncier'
                        font_style: 'Subtitle1'
                        size_hint_y: None
                        height: self.texture_size[1]

                    MDLabel:
                        text: 'contact@lereseaufoncier.fr'
                        font_style: 'Caption'
                        size_hint_y: None
                        height: self.texture_size[1]

                    ScrollView:
                        MDList:

                            OneLineIconListItem:
                                text: 'Home'
                                on_press: root.manager.current= 'home'

                                IconLeftWidget:
                                    icon: 'home'
                                    on_press: root.manager.current= 'home'

                            OneLineIconListItem:
                                text: 'Profile'
                                on_press: root.manager.current= 'profil'

                                IconLeftWidget:
                                    icon: 'android'
                                    on_press: root.manager.current= 'profil'
                            
                            OneLineIconListItem:
                                text: 'Contact'
                                on_press: root.manager.current= 'contact'

                                IconLeftWidget:
                                    icon: 'phone'
                                    on_press: root.manager.current= 'contact'

这与和的代码相同。

我希望有人可以帮助我。

谢谢。

【问题讨论】:

    标签: kivy kivy-language kivymd


    【解决方案1】:

    Re,所以我找到了解决方案:

    这里,所有的代码:

    from kivymd.app import MDApp
    from kivy.lang import Builder
    from kivy.core.window import Window
    
    
    Window.size = (800, 800)
    
    navigation_helper = """
    
    
    
    Screen:
        BoxLayout:
            orientation: 'vertical'
            MDToolbar:
                id: title_bar
                title: 'Demo Application'
                left_action_items: [["menu", lambda x: nav_drawer.set_state('toggle')]]
                elevation: 10
                height:'50dp'
            Widget:
    
    
        MDNavigationLayout:
    
            ScreenManager:
                id: scr
            
                MDScreen:
                    name: 'home'
                    BoxLayout:
                        orientation: 'vertical'
    
                        MDLabel:
                            text: 'Some home text'
                    
                    
                MDScreen:
                    name: 'profil'
                    BoxLayout:
                        orientation: 'vertical'
    
                        MDLabel:
                            text: 'Some profil text'
    
                MDScreen:
                    name: 'contact'
                    BoxLayout:
                        orientation: 'vertical'
    
                        MDLabel:
                            text: 'Some contact text'
    
        
            MDNavigationDrawer:
                id: nav_drawer
            
                BoxLayout:
                    orientation: 'vertical'
                    spacing: '8dp'
                    padding: '8dp'
    
                    Image:
                        source: 'img\logo.png'
                        size_hint: .5, .5
                        pos_hint: {"center_x": 0.5}
    
                    MDLabel:
                        text: 'Le Réseau Foncier'
                        font_style: 'Subtitle1'
                        size_hint_y: None
                        height: self.texture_size[1]
    
                    MDLabel:
                        text: 'contact@lereseaufoncier.fr'
                        font_style: 'Caption'
                        size_hint_y: None
                        height: self.texture_size[1]
    
                    ScrollView:
                        MDList:
    
                            OneLineIconListItem:
                                text: 'Home'
                                on_press: scr.current= 'home'
    
                                IconLeftWidget:
                                    icon: 'home'
                                    on_press: scr.current= 'home'
    
                            OneLineIconListItem:
                                text: 'Profile'
                                on_press: scr.current= 'profil'
    
                                IconLeftWidget:
                                    icon: 'android'
                                    on_press: scr.current= 'profil'
                            
                            OneLineIconListItem:
                                text: 'Contact'
                                on_press: scr.current= 'contact'
    
                                IconLeftWidget:
                                    icon: 'phone'
                                    on_press: scr.currentt= 'contact'
    
    
    
                
    """
    
    
    class DemoApp(MDApp):
        def build(self):
            self.theme_cls.primary_palette = 'Red'
            screen = Builder.load_string(navigation_helper)
            return screen
    
        def nav_drawer(self):
            pass
    
    DemoApp().run()
    

    祝你有美好的一天。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-29
      • 2011-08-23
      • 2019-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多