python - Kivy:未显示 MDExpansion 面板
问题描述
我需要创建一个屏幕作为结果表测试的历史记录,该屏幕位于另一个屏幕中。我想使用 MDExpansionPanel,但是当我编写代码时没有显示任何内容。屏幕是空的,我不知道为什么,有人可以帮助我吗?
在 main.py
class GoalsScreen(Screen):
pass
class Content(BoxLayout):
pass
class HistoryScreen(Screen):
def on_start(self):
names = ["test1", "test2", "test3"]
for name in names:
panel= MDExpansionPanel(icon="1.png",title= name,
content=Content())
self.root.ids.panel_container.add_widget(panel)
class MainApp(MDApp):
def build(self):
self.theme_cls.primary_palette = "Red"
self.theme_cls.primary_hue = "500"
self.theme_cls.theme_style = "Light"
screen = Builder.load_string(screen_helper)
return screen
MainApp().run()
在.kv
screen_helper = """
ScreenManager:
GoalsScreen:
HistoryScreen:
<GoalsScreen>:
name: "goals"
Button:
text: "next page"
on_press: root.manager.current= "history"
<Content>
size_hint: 1,None
height: self.minimum_height
Button:
size_hint: None, None
MDIconButton:
icon: "close"
<HistoryScreen>:
name: "history"
BoxLayout:
ScrollView:
GridLayout:
cols: 1
size_hint_y: None
height: self.minimum_height
id: panel_container
解决方案
您的代码有几个问题:
- 该
on_start()
方法永远不会被调用(它不会自动调用 aScreen
)。 - 这
title= name
不是MDExpansionPanel
. - 缺少
panel_cls
所需的参数。MDExpansionPanel
HistoryScreen
没有root
属性,因此此代码将失败:self.root.ids.panel_container.add_widget(panel)
尝试这样的事情:
class HistoryScreen(Screen):
def on_enter(self):
self.ids.panel_container.clear_widgets() # to avoid re-adding panel each time
names = ["test1", "test2", "test3"]
for name in names:
panel= MDExpansionPanel(icon="1.png",panel_cls=MDExpansionPanelOneLine(),
content=Content())
self.ids.panel_container.add_widget(panel)
推荐阅读
- javascript - 计时器和本地存储问题
- r - 从 R 中整个数据帧的行中查找配对值
- javascript - 使用 casperjs 连接到 whatsappweb
- django - DJANGO,DRF:request id 返回 NONE 值而不是用户对象
- css - 为什么 CSS 混合模式会被 jquery 插件 fullPage.js 覆盖?
- android - 重置分数 - 它不会回到零
- python - 删除矩阵中最后一个字符后的空格
- php - 如何在 Laravel 5.4 中格式化电子邮件中收到的内容?
- php - 向外部 php 发送变量的最佳方法
- css - CSS计数器不递增