【问题标题】:Creating new screens from Navigation drawer KivyMD从导航抽屉 KivyMD 创建新屏幕
【发布时间】:2021-02-11 21:27:54
【问题描述】:

大家好,我是 KivyMD 的新手,正在开发我的第一个应用程序。基本上,我需要在单击导航抽屉中的内容时打开一个新屏幕。例如,我的导航抽屉上有一个新房间按钮,我希望在单击它时打开一个新屏幕。

好的,这是我的代码

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


class EMC_WebmaApp(MDApp):
    def build(self):
        screen = Screen()

        # theme settings
        self.theme_cls.primary_palette = 'DeepPurple'
        self.theme_cls.theme_style = 'Dark'
        # end of theme declaration

        # loaded strings
        navigation_bar = Builder.load_string(toolbar)
        # end of loaded strings

        # addition of loaded strings to screen + return screen
        screen.add_widget(navigation_bar)

        return screen
        # end of addition


EMC_WebmaApp().run()

这是我的字符串

toolbar = '''
Screen:
    NavigationLayout:
        ScreenManager:
            Screen:  
                BoxLayout:
                    orientation: 'vertical'
                    
                    MDToolbar:
                        title: 'EMC_Webma'
                        left_action_items: [['menu', lambda x: navigation.set_state('open')]]
                        right_action_items: [['card-search', lambda x: print('pass')]]
                        elevation: 10
                            
                           
                    MDList:
                        TwoLineListItem:
                            text: 'Friend 1'
                            secondary_text: '[Recent text here]'
                            
                    MDList:
                        TwoLineListItem:
                            text: 'Friend 2'
                            secondary_text: '[Recent text here]'
                            
                    MDList:
                        TwoLineListItem:
                            text: 'Friend 3'
                            secondary_text: '[Recent text here]'
                        
                    MDList:
                        TwoLineListItem:
                            text: 'Friend 4'
                            secondary_text: '[Recent text here]'
                    
                    MDList:
                        TwoLineListItem:
                            text: 'Friend 5'
                            secondary_text: '[Recent text here]'
                            
                    MDList:
                        TwoLineListItem:
                            text: 'Friend 6'
                            secondary_text: '[Recent text here]'

                    Widget:
                    
        MDNavigationDrawer:
            id: navigation
            
            BoxLayout:
                orientation: 'vertical'
                spacing: '8dp'
                padding: '8dp'
                            
                Image:
                    source: 'einstein.jpg'
                    
                MDLabel:
                    text: 'Enwerem Melvin Confidence'
                    font_style: 'Subtitle2'
                    size_hint_y: None
                    height: self.texture_size[1]
                    
                MDLabel:
                    text: 'enweremmelvinconfidence@gmail.com'
                    font_style: 'Caption'
                    size_hint_y: None
                    height: self.texture_size[1]
                                    
                ScrollView:
                    MDList:
                        OneLineIconListItem:
                            text: 'New Room'
                            IconLeftWidget:
                                icon: 'theater'
                                
                        OneLineIconListItem:
                            text: 'Contacts'
                            IconLeftWidget:
                                icon: 'contacts'
                                
                        OneLineIconListItem:
                            text: 'Calls'
                            IconLeftWidget:
                                icon: 'cellphone-android'
                                
                        OneLineIconListItem:
                            text: 'Settings'
                            IconLeftWidget:
                                icon: 'settings'
                                
                        OneLineIconListItem:
                            text: 'FAQ'
                            IconLeftWidget:
                                icon: 'frequently-asked-questions'
                                
                        OneLineIconListItem:
                            text: 'Invite Friends'
                            IconLeftWidget:
                                icon: 'account-plus'
                                
                        OneLineIconListItem:
                            text: 'Toggle Theme'
                            IconLeftWidget:
                                icon: 'theme-light-dark'
'''

感谢您的帮助????????????

【问题讨论】:

    标签: python navigation-drawer kivymd


    【解决方案1】:
    from kivy.lang import Builder
    
    from kivymd.app import MDApp
    
    KV = '''
    MDScreen:
    
        MDNavigationLayout:
    
            ScreenManager:
                id: manager
    
                MDScreen:
                    name: 'screen 1'
    
                    MDBoxLayout:
                        orientation: 'vertical'
                        
                        MDToolbar:
                            title: 'EMC_Webma'
                            left_action_items: [['menu', lambda x: navigation.set_state('open')]]
                            right_action_items: [['card-search', lambda x: print('pass')]]
                            elevation: 10
                                
                        ScrollView:
    
                            MDList:
                                TwoLineListItem:
                                    text: 'Friend 1'
                                    secondary_text: '[Recent text here]'
                                    
                                TwoLineListItem:
                                    text: 'Friend 2'
                                    secondary_text: '[Recent text here]'
                                    
                                TwoLineListItem:
                                    text: 'Friend 3'
                                    secondary_text: '[Recent text here]'
                                
                                TwoLineListItem:
                                    text: 'Friend 4'
                                    secondary_text: '[Recent text here]'
                            
                                TwoLineListItem:
                                    text: 'Friend 5'
                                    secondary_text: '[Recent text here]'
                                    
                                TwoLineListItem:
                                    text: 'Friend 6'
                                    secondary_text: '[Recent text here]'
    
                MDScreen:
                    name: 'screen 2'
                    
                    MDLabel:
                        text: "Screen 2"
    
            MDNavigationDrawer:
                id: navigation
                
                MDBoxLayout:
                    orientation: 'vertical'
                    spacing: '8dp'
                    padding: '8dp'
                                
                    Image:
                        source: 'einstein.jpg'
                        
                    MDLabel:
                        text: 'Enwerem Melvin Confidence'
                        font_style: 'Subtitle2'
                        size_hint_y: None
                        height: self.texture_size[1]
                        
                    MDLabel:
                        text: 'enweremmelvinconfidence@gmail.com'
                        font_style: 'Caption'
                        size_hint_y: None
                        height: self.texture_size[1]
                                        
                    ScrollView:
    
                        MDList:
                            OneLineIconListItem:
                                text: 'New Room'
                                on_release: manager.current = 'screen 2'
                                IconLeftWidget:
                                    icon: 'theater'
                                    
                            OneLineIconListItem:
                                text: 'Contacts'
                                IconLeftWidget:
                                    icon: 'contacts'
                                    
                            OneLineIconListItem:
                                text: 'Calls'
                                IconLeftWidget:
                                    icon: 'cellphone-android'
                                    
                            OneLineIconListItem:
                                text: 'Settings'
                                IconLeftWidget:
                                    icon: 'settings'
                                    
                            OneLineIconListItem:
                                text: 'FAQ'
                                IconLeftWidget:
                                    icon: 'frequently-asked-questions'
                                    
                            OneLineIconListItem:
                                text: 'Invite Friends'
                                IconLeftWidget:
                                    icon: 'account-plus'
                                    
                            OneLineIconListItem:
                                text: 'Toggle Theme'
                                IconLeftWidget:
                                    icon: 'theme-light-dark'
    '''
    
    
    class EMC_WebmaApp(MDApp):
        def build(self):
            #self.theme_cls.primary_palette = 'DeepPurple'
            #self.theme_cls.theme_style = 'Dark'
            return Builder.load_string(KV)
    
    
    EMC_WebmaApp().run()
    

    【讨论】:

    • 非常感谢☺️
    猜你喜欢
    • 2020-09-04
    • 2021-05-20
    • 2020-11-21
    • 1970-01-01
    • 2020-09-12
    • 2014-12-14
    • 2021-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多