我无法让列表显示在我的 kivymd 选项卡上
Posted
技术标签:
【中文标题】我无法让列表显示在我的 kivymd 选项卡上【英文标题】:I cant get lists to show up on my kivymd tabs 【发布时间】:2020-12-20 19:39:04 【问题描述】:我正在尝试使用 kivymd 创建一个聊天应用程序,但我无法让我的列表显示在聊天 tad 屏幕上。我收到错误消息:
Traceback (most recent call last):
File "kivy\properties.pyx", line 860, in kivy.properties.ObservableDict.__getattr__
KeyError: 'list'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:/Users/Allano/Desktop/help/main.py", line 70, in <module>
MainApp().run()
File "C:\Users\Allano\Anaconda3\lib\site-packages\kivy\app.py", line 854, in run
self.dispatch('on_start')
File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
File "c:/Users/Allano/Desktop/help/main.py", line 60, in on_start
self.new_message("ERIK SANDBERG", "Hello World", "kivymd_logo.png")
File "c:/Users/Allano/Desktop/help/main.py", line 65, in new_message
self.root.ids.list.add_widget(new_message)
File "kivy\properties.pyx", line 863, in kivy.properties.ObservableDict.__getattr__
AttributeError: 'super' object has no attribute '__getattr__
我希望在应用启动时加载 main.py 文件中的列表。我已经尝试了几天寻找解决方案,但没有解决方案。如果可能的话,欢迎对代码格式进行最小限度的调整。这是我的代码。为了方便起见,我删除了一些代码
main.kv
MD屏幕: 名称:“主要”
屏幕管理器: id: screen_manager
Screen:
MDBoxLayout:
orientation: "vertical"
size_hint: 1, 2
BoxLayout:
orientation: "vertical"
MDToolbar:
id: toolbar
title: "name"
size_hint: 1.0,0.05
md_bg_color: 1, 1, 1, 1
elevation: 10
MDTabs:
tab_indicator_anim: True
color_indicator: 1, 1, 1, 1
tab_indicator_height: ("3dp")
Tab:
text: "Threads"
Tab:
text: "Chats"
ScrollView:
MDList:
id: list
Tab:
text: "Post"
Screen:
BoxLayout:
orientation: "vertical"
登录.kv
MDScreen:
name: "login"
Screen:
MDBoxLayout:
orientation: "vertical"
MDRaisedButton:
text: 'Enter'
elevation: 2
font_size: 35
width: dp(200)
elevation_nomal: 8
size_hint: 0.85,0.06
pos_hint: 'center_x':0.5,'center_y':0.38
on_press:
root.manager.transition.direction = 'left'
root.manager.current = "main"
main.py
import os
from kivy.animation import Animation
from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.factory import Factory
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivymd.uix.picker import MDDatePicker
from kivy.core.window import Window
from kivy.uix.modalview import ModalView
from kivymd.uix.filemanager import MDFileManager
from kivymd.theming import ThemableBehavior
from kivy.properties import(
ListProperty,
NumericProperty,
ObjectProperty,
OptionProperty,
StringProperty,
BooleanProperty,
)
from kivymd.toast import toast
from kivymd.uix.dialog import MDDialog
from kivymd.uix.button import MDFlatButton, MDFillRoundFlatButton
from kivymd.uix.menu import MDDropdownMenu
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivymd.uix.list import TwoLineAvatarIconListItem
from kivymd.uix.snackbar import Snackbar
from kivymd.uix.bottomsheet import MDCustomBottomSheet
from kivy.uix.button import Button
from kivymd.uix.useranimationcard import MDUserAnimationCard
from kivymd.uix.tab import MDTabsBase
from kivymd.uix.list import ImageLeftWidget
class Tabs(FloatLayout, MDTabsBase):
'''Class implementing content for a tab on home screen'''
class MainApp(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.title = "Main App"
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "Cyan"
self.theme_cls.primary_hue = "600"
def change_screen(self,name):
screen_manager.current = name
def build(self):
global screen_manager
screen_manager = ScreenManager()
screen_manager.add_widget(Builder.load_file("login.kv"))
screen_manager.add_widget(Builder.load_file("Main.kv"))
return screen_manager
def on_start(self):
self.new_message("ERIK SANDBERG", "Hello World", "kivymd_logo.png")
def new_message(self, name, message, image_name):
new_message = TwoLineAvatarIconListItem(text=name, secondary_text=message)
new_message.add_widget(ImageLeftWidget(source=image_name))
self.root.ids.list.add_widget(new_message)
if __name__ == "__main__":
MainApp().run()
【问题讨论】:
【参考方案1】:Main.kv
Screen:
name: "main"
[...]
main.py
class MainApp(MDApp):
[...]
def new_message(self, name, message, image_name):
[...]
screen_manager.get_screen("main").ids.list.add_widget(new_message)
【讨论】:
以上是关于我无法让列表显示在我的 kivymd 选项卡上的主要内容,如果未能解决你的问题,请参考以下文章