使用我的工具栏和屏幕管理器最小化我的代码
Minimize my code with My TOOLBAR and SCREEN MANAGER
我尝试减少我的代码。我正在使用 kivyMD。我在 MDNavigationLayout 中创建了一个带有菜单的 MDToolbar。我还使用 ScreenManager 创建了不同的屏幕。
喜欢:主页、个人资料、联系方式。
问题是我 copy/paste 我的 MDToolbar 的所有代码都进入了我的 kv 文件中的每个屏幕。
但我希望我的所有屏幕都只有一个 'Header'。而且我读文档也不是那么容易...我没有找到我想要的东西。
所以,我与您分享代码:
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'
这与 和 的代码相同。
希望有人能帮助我。
谢谢。
回复,所以我找到了解决方案:
此处,所有代码:
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()
祝你有愉快的一天。
我尝试减少我的代码。我正在使用 kivyMD。我在 MDNavigationLayout 中创建了一个带有菜单的 MDToolbar。我还使用 ScreenManager 创建了不同的屏幕。
喜欢:主页、个人资料、联系方式。
问题是我 copy/paste 我的 MDToolbar 的所有代码都进入了我的 kv 文件中的每个屏幕。
但我希望我的所有屏幕都只有一个 'Header'。而且我读文档也不是那么容易...我没有找到我想要的东西。
所以,我与您分享代码:
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'
这与 和 的代码相同。
希望有人能帮助我。
谢谢。
回复,所以我找到了解决方案:
此处,所有代码:
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()
祝你有愉快的一天。