【发布时间】:2022-10-06 18:57:35
【问题描述】:
我创建了带有 2 个屏幕的代码,并且需要其中一个具有扩展面板。 不幸的是,我无法让面板显示其中的内容。取而代之的是,我陷入了混乱和偏头痛的一面,所以这是我的代码,一个我希望它看起来像的示例以及我设法创建的内容减去我的完整代码。
视频示例:https://www.kapwing.com/videos/62f4074bafd00100c829b84c
问题视频示例:https://www.kapwing.com/videos/62f41c828f6acd00521caae1
如视频示例所示:
第一个代码:
from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen
from kivymd.uix.expansionpanel import MDExpansionPanel
from kivymd.uix.expansionpanel import MDExpansionPanelOneLine
from kivymd.uix.boxlayout import MDBoxLayout
KV = \'\'\'
MDScreen:
MDNavigationLayout:
ScreenManager:
id: manager
MDScreen:
name: \'Home\'
AnchorLayout:
anchor_x: \"center\"
anchor_y: \"top\"
MDToolbar:
md_bg_color: 0, 0, 0, 0.5
title: \"Example\"
elevation: 10
left_action_items: [[\"menu\", lambda x: mud_list.set_state(\"open\")]]
right_action_items: [[\"dots-vertical\", lambda x:app.dropdown(x)]]
MDNavigationDrawer:
id: mud_list
BoxLayout:
orientation: \'vertical\'
spacing: \'5dp\'
padding: \'5dp\'
ScrollView:
MDList:
OneLineIconListItem:
text: \'[Settings]\'
on_release:
manager.current = \'Settings\'
root.ids.mud_list.set_state(new_state=\'toggle\', animation=True)
divider: None
IconLeftWidget:
icon: \'cog\'
on_release:
manager.current = \'Settings\'
root.ids.mud_list.set_state(new_state=\'toggle\', animation=True)
MDLabel:
text:\' By Author\'
size_hint_y: None
font_style: \'Button\'
height: self.texture_size[1]
MDScreen:
name: \'Settings\'
AnchorLayout:
anchor_x: \"center\"
anchor_y: \"top\"
MDToolbar:
id: mdt_color
md_bg_color: 1, 1, 1, 1
elevation: 10
MDIconButton:
icon: \"keyboard-backspace\"
pos_hint: {\"center_x\": 0.09, \"center_y\": 0.945}
on_release: manager.current = \'Home\'
MDBoxLayout:
size_hint: 1, 0.89
orientation : \'vertical\'
ScrollView:
MDBoxLayout:
orientation:\'vertical\'
adaptive_height: True
padding:[dp(15),dp(15),dp(15),dp(35)]
spacing:dp(15)
Content
adaptive_height: True
orientation: \'vertical\'
OneLineIconListItem:
text: \"Dark\"
on_release:app.theme_changer2()
divider: None
IconLeftWidget:
icon: \'weather-night\'
on_release:app.theme_changer2()
OneLineIconListItem:
text: \"Light\"
on_release:app.theme_changer()
divider: None
IconLeftWidget:
icon: \'white-balance-sunny\'
on_release:app.theme_changer()
ScrollView:
MDGridLayout:
id: box
cols: 1
adaptive_height: True
\'\'\'
class Content(MDBoxLayout):
\"\"\"Custom content.\"\"\"
def __draw_shadow__(self, origin, end, context=None):
pass
class MainApp(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.menu = None
self.menu_list = None
self.kvs = Builder.load_string(KV)
self.screen = Builder.load_string(KV)
def on_start(self):
self.root.ids.box.add_widget(
MDExpansionPanel(
icon=\"theme-light-dark\",
content=Content(),
panel_cls=MDExpansionPanelOneLine(
text=\"Theme\",
)
)
)
def theme_changer(self):
self.theme_cls.theme_style = \"Light\"
self.root.ids.mdt_color.md_bg_color = [1, 1, 1, 1]
def theme_changer2(self):
self.theme_cls.theme_style = \"Dark\"
self.root.ids.mdt_color.md_bg_color = [0, 0, 0, 1]
def build(self):
self.theme_cls.theme_style = \"Light\"
screen = Screen()
screen.add_widget(self.kvs)
return self.screen
ma = MainApp()
ma.run()
第二个代码:我从这里的 kivymd 文档中得到 https://github.com/kivymd/KivyMD/wiki/Components-Expansion-Panel
第三个代码与第二个代码大致相同,但我自己制作了:
from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.expansionpanel import MDExpansionPanel, MDExpansionPanelOneLine
KV = \'\'\'
<Content>
adaptive_height: True
orientation: \'vertical\'
OneLineIconListItem:
text: \"Dark\"
divider: None
IconLeftWidget:
icon: \'weather-night\'
OneLineIconListItem:
text: \"Light\"
divider: None
IconLeftWidget:
icon: \'white-balance-sunny\'
ScrollView:
MDGridLayout:
id: box
cols: 1
adaptive_height: True
\'\'\'
class Content(MDBoxLayout):
\"\"\"Custom content.\"\"\"
def __draw_shadow__(self, origin, end, context=None):
pass
class Test(MDApp):
def build(self):
return Builder.load_string(KV)
def on_start(self):
self.root.ids.box.add_widget(
MDExpansionPanel(
icon=\"theme-light-dark\",
content=Content(),
panel_cls=MDExpansionPanelOneLine(
text=\"Theme\",
)
)
)
Test().run()
我的问题是,如问题视频示例所示,扩展面板本身没有显示。
我一直在弄清楚这一点,所以在我尝试过的所有混乱中,我注意到“内容”的位置以及它下面的所有内容都与屏幕设置的锚布局有关\',导致面板显示,但内容不在里面。
与 \"content\" 或 \"MDGridlayout\" 是否具有 id: 框的效果相同。
总之,我希望能够在第二个代码中创建类似的东西,但在我的主应用程序的设置屏幕中,或者基本上将第三个代码复制并粘贴到我的主应用程序中。
哦,我以后可能会自己提出这个问题,但如果它足够简单,我怎样才能让它在主题改变时成为默认值?
标签: python screen panel kivymd expansion