我已经创建了两个屏幕的代码,并需要其中一个有一个扩展面板。不幸的是,我无法让面板显示其中的内容。相反,我脑子里的混乱和偏头痛的一面,所以这是我的代码,一个例子,我希望它的样子,以及我设法创建了减去我的全部代码。
视频示例: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
第3段代码与第2段代码基本相同,但我自己编写了:
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: box的效果相同。
总之,我希望能够在我的主应用程序的设置屏幕中创建类似于第2段代码的内容,或者将第3段代码复制并粘贴到我的主应用程序中。
哦,我以后可能会自己问这个问题,但是如果够简单的话,我该怎么做呢?当主题被改变时,它变成了默认的?
发布于 2022-10-06 02:57:35
花了一段时间,但在我的帮助下,我终于得到了。下面是一个示例,我认为这是代码#Comments#中的一些关键注释。
希望这能帮上忙。
from kivymd.app import MDApp
from kivy.lang import Builder
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.expansionpanel import MDExpansionPanel, MDExpansionPanelOneLine
KV = '''
#THE CONTENT GOES ABOVE EVERYTHING ELSE EVEN THE 1ST SCREEN.#
#IT IS REFERENCED LATER IN WHICH EVER SCREEN IT IS TO APPEAR USING MDGridLayout AND A def LATER ON#
#It should also always have these angle brackets <>#
<Content>
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
OneLineIconListItem:
text: 'Dark theme'
on_release:app.theme_changer2()
divider: None
IconLeftWidget:
icon: 'weather-night'
on_release:app.theme_changer2()
OneLineIconListItem:
text: 'Light theme'
on_release:app.theme_changer()
divider: None
IconLeftWidget:
icon: 'white-balance-sunny'
on_release:app.theme_changer()
<Content2>
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
OneLineIconListItem:
text: 'I try to explain what i think are key points to note'
on_release:app.theme_changer2()
divider: None
IconLeftWidget:
icon: 'weather-night'
on_release:app.theme_changer2()
OneLineIconListItem:
text: 'Hope this helps someone else'
on_release:app.theme_changer()
divider: None
IconLeftWidget:
icon: 'white-balance-sunny'
on_release:app.theme_changer()
MDScreen:
MDNavigationLayout:
ScreenManager:
id: manager
MDScreen:
name: 'Home'
#The location of this MDGridLayout shows it appears in the 1st screen.#
MDGridLayout:
cols: 1
adaptive_height: True
#This id is used to reference what content goes to which expansion panel#
id: box2
MDRaisedButton:
pos_hint: {"center_x": 0.5, "center_y": 0.5}
text: "PRESS ME"
on_release: manager.current = 'Home2'
MDScreen:
name: 'Home2'
MDBoxLayout:
orientation: "vertical"
MDToolbar:
id: mdt_color
elevation: 10
ScrollView:
divider: 'None'
#The location of this MDGridLayout shows it appears in the 2nd screen.#
MDGridLayout:
cols: 1
adaptive_height: True
id: box
MDIconButton:
pos_hint: {"center_x": 0.05, "center_y": 0.945}
icon: "keyboard-backspace"
on_release: manager.current = 'Home'
'''
# Every expansion panel should have a class corresponding to it's name e.g.#
class Content(MDBoxLayout):
pass
# The class name is referenced in the contents= section below#
class Content2(MDBoxLayout):
pass
class MainApp(MDApp):
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):
return Builder.load_string(KV)
def on_start(self):
# Here you see the id box used#
self.root.ids.box.add_widget(
MDExpansionPanel(
icon="theme-light-dark",
# Here the content name Content is referenced in content=#
content=Content(),
panel_cls=MDExpansionPanelOneLine(
text="Theme",
)
)
)
# Here you see the id box2 used#
self.root.ids.box2.add_widget(
MDExpansionPanel(
icon="theme-light-dark",
# Here the content name Content2 is referenced in content=#
content=Content2(),
panel_cls=MDExpansionPanelOneLine(
text="Read the code #comments# for details",
)
)
)
ma = MainApp()
ma.run()
https://stackoverflow.com/questions/73312902
复制相似问题