Python + Kivy(KivyMD): создание шаблонов для уменьшения кода
Есть маленькое приложение, в котором по сути два экрана главное меню, и подменю. Для каждого экрана свой KV файл.
В подменю есть ScrollView который прокручивает MDGridLayout с красивой разметкой для картинок, описание и т.д. Но когда таких красивых боксов много, код становиться просто громадным.
Как создать шаблон, чтоб я мог подставить только картинку и новое описание не плодя одинаковый код?
test.py
from kivy.lang import Builder
from kivy.properties import ObjectProperty, StringProperty
from kivymd.app import MDApp
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.gridlayout import MDGridLayout
Builder.load_file('test2.kv')
def final_text():
text = "Hello"
return text
def final_img():
img = "img.png"
return img
class ContentNavigationDrawer(MDBoxLayout):
screen_manager = ObjectProperty()
nav_drawer = ObjectProperty()
class TestApp(MDApp):
text = StringProperty(final_text())
img = StringProperty(final_img())
def build(self):
return
TestApp().run()
test.kv
<ContentNavigationDrawer>
md_bg_color: 0.231, 0.231, 0.231, 1
ScrollView:
MDList:
OneLineAvatarIconListItem:
theme_text_color: "Custom"
text_color: 1, 1, 1, 1
text: "Main Menu"
on_press:
root.nav_drawer.set_state("close")
root.screen_manager.current = "scr1"
IconLeftWidget:
theme_text_color: "Custom"
text_color: 1, 1, 1, 1
icon: "menu-open"
OneLineAvatarIconListItem:
theme_text_color: "Custom"
text_color: 1, 1, 1, 1
text: "Test"
on_press:
root.nav_drawer.set_state("close")
root.screen_manager.current = "test2"
IconLeftWidget:
theme_text_color: "Custom"
text_color: 1, 1, 1, 1
icon: "fire"
MDScreen:
md_bg_color: 0.164, 0.164, 0.164, 1
MDNavigationLayout:
x: toolbar.width
ScreenManager:
id: screen_manager
MDScreen:
name: "scr1"
MDBoxLayout:
cols: 1
orientation: 'vertical'
md_bg_color: 0.235, 0.247, 0.254, 1
Test2:
MDToolbar:
id: toolbar
title: "SCUM INFO"
pos_hint: {"top": 1}
md_bg_color: 0.231, 0.231, 0.231, 1
specific_text_color: 1, 1, 1, 1
left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]
right_action_items:
[["magnify", lambda x: banner.show()], ["cog-outline", lambda x: app.set_screen("setting")]]
MDNavigationDrawer:
id: nav_drawer
ContentNavigationDrawer:
screen_manager: screen_manager
nav_drawer: nav_drawer
test2.kv
<Test2@MDScreen>:
name: "test2"
MDBoxLayout:
cols: 1
orientation: 'vertical'
MDBoxLayout:
cols: 1
orientation: 'vertical'
size_hint: 1, .11
MDBoxLayout:
cols: 1
orientation: 'vertical'
size_hint: 1, .89
ScrollView:
MDGridLayout:
cols: 1
size_hint_y: None
height: '200dp'
padding: 5, 5, 5, 5
spacing: dp(15)
# VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV DUPLICATE, DUPLICATE, DUPLICATE, DUPLICATE ---->
MDGridLayout:
cols: 1
padding: 5, 5, 5, 5
md_bg_color: 0.231, 0.231, 0.231, 1
spacing: dp(5)
canvas.before:
Color:
rgba: .5, .5, .5, 1
Line:
width: 1
rectangle: self.x, self.y, self.width, self.height
MDBoxLayout:
cols: 1
orientation: 'vertical'
size_hint: 1, .30
md_bg_color: 0.235, 0.247, 0.254, 1
MDRelativeLayout:
cols: 1
orientation: 'vertical'
# vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
FitImage:
source: app.img # <---------------------------------
MDLabel:
text: app.text # <--------------------------------
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
theme_text_color: "Custom"
text_color: app.theme_cls.accent_color
pos_hint: {"center_y": .5}
font_style: "H6"
font_size: root.width/18