在我的kivymd文件中显示MDList不起作用

时间:2020-05-06 02:22:47

标签: python kivy

我正在使用python和kivymd开发一个项目,我想在我的文件kv(list.kv)中显示MDList。 但是它不起作用,我不知道问题出在哪里!有什么建议 这是main.py

from kivy.lang import Builder
from mysql.connector import Error
from baseclass.start import Start
from kivy.lang import Builder  
from threading import Thread
from kivy.uix.screenmanager import Screen
from kivy.utils import get_color_from_hex
from spin_load import ProgressSpinner
from kivymd.color_definitions import colors
from kivy.uix.recycleview import RecycleView
from kivy.core.window import Window
from kivy.uix.recycleview import RecycleView
import mysql.connector
from kivymd.uix.menu import MDDropdownMenu
from kivy.properties import OptionProperty
from kivy.properties import ObjectProperty
from kivymd.uix.bottomsheet import MDListBottomSheet
from kivymd.uix.expansionpanel import MDExpansionPanel
from kivymd.uix.boxlayout import BoxLayout
from kivymd.uix.list import OneLineListItem


Window.size = (360, 600)


class Codebarre(Screen):
    def on_start(self):
        for i in range(20):
            self.root.ids.container.add_widget(
                OneLineListItem(text=f"Single-line item {i}")
            )        
class MyApp(MDApp):
    def build(self):
        self.theme_cls.primary_palette = "DeepPurple"
        return Builder.load_file("main.kv")

MyApp().run() 

这是我要显示MDList的文件

list.kv

#:import utils kivy.utils

<Codebarre>:
    name: 'codebarre'
    ScrollView:
        MDList:
            id: container ```


1 个答案:

答案 0 :(得分:0)

事实证明,从未调用过on_start()方法。 main.py的工作版本如下。我没有更改list.kv

from kivy.lang import Builder
from threading import Thread
import kivy.clock
from kivy.uix.screenmanager import Screen
from kivy.utils import get_color_from_hex
from kivymd.color_definitions import colors
from kivy.uix.recycleview import RecycleView
from kivy.core.window import Window
from kivy.uix.recycleview import RecycleView
from kivymd.uix.menu import MDDropdownMenu
from kivy.properties import OptionProperty
from kivy.properties import ObjectProperty
from kivymd.uix.bottomsheet import MDListBottomSheet
from kivymd.uix.expansionpanel import MDExpansionPanel
from kivymd.uix.boxlayout import BoxLayout
from kivymd.uix.list import OneLineListItem
from kivymd.app import MDApp
from kivy.uix.stacklayout import StackLayout


Window.size = (360, 600)


class Codebarre(Screen):

    def __init__(self, **kvargs):
        self.app = MDApp.get_running_app()
        super().__init__(**kvargs)
        kivy.clock.Clock.schedule_once(self.build)

    def build(self, *args):
        for i in range(20):
            self.ids.container.add_widget(
                OneLineListItem(text=f"Single-line item {i}")
            )        

class MyApp(MDApp):
    def build(self):
        self.theme_cls.primary_palette = "DeepPurple"
        Builder.load_file("list.kv")
        return Codebarre()

MyApp().run()