Kivy 小部件生成非常慢

Kivy widget generation very slow

我正在制作一个 kivy 应用程序来查找用户输入的单词的押韵词。它在 kivy RecycleView 内的 MDList 中将所有押韵词显示为 OneLineListItems。单击其中一个 OneLineListItems,它会在屏幕右侧显示单词的定义。但是,当我单击 OneLineListItem 时,它的定义需要很长时间才能出现,有时它会严重滞后以至于应用程序关闭。我做错了什么还是只是我的电脑?代码如下:

from kivymd.app import MDApp
from kivy.lang import Builder
from kivymd.uix.label import MDLabel
from kivymd.uix.list import OneLineListItem
import pronouncing
import enchant
from PyDictionary import PyDictionary

dictionary = PyDictionary()
d = enchant.Dict("en_US")

kv = """
Screen:
    input:input
    scroll:scroll
    word:word
    defs:defs
    MDGridLayout:
        rows: 1
        cols: 2
        MDGridLayout:
            rows: 2
            cols: 1
            MDFloatLayout:
                MDTextField:
                    id:input
                    size_hint: (.4, None)
                    height: 26
                    multiline: False
                    on_text_validate: app.rhyme()
                    hint_text: "Search"
                    mode: "rectangle"
                    pos_hint: {"center_x": .25, "center_y": .85}
                    font_name: "DejaVuSans.ttf"
                    text_size: self.size
            MDFloatLayout:
                RecycleView:
                    size_hint: 0.85,1.5
                    bar_width: dp(15)
                    bar_height: dp(40)
                    scroll_type: ["content"]
                    pos_hint: {"center_x": 0.45, "center_y": 0.93}
                    MDList:
                        id: scroll
                
        
        MDBoxLayout:
            id:defs
            orientation: "vertical"
            md_bg_color: 0, 1, 0.2, 1
            MDLabel:
                id: word
                text: ""
                text_size: self.size
                

"""


class RhymeApp(MDApp):
    played = []
    x_turn = True

    def build(self):
        self.screen = Builder.load_string(kv)
        return self.screen

    def rhyme(self):
        raw_rhymes = pronouncing.rhymes(self.screen.input.text)
        rhymes = []
        [rhymes.append(x) for x in raw_rhymes if x not in rhymes and x[-1] != "." and d.check(x)]
        self.screen.scroll.clear_widgets()
        for i in rhymes:
            self.screen.scroll.add_widget(
                OneLineListItem(text=f"{i}".capitalize(), on_release=self.dictionary)
            )

    def dictionary(self, btn):
        nl = '\n'
        self.screen.defs.clear_widgets()
        self.screen.word.text = btn.text.capitalize()
        meaning = dictionary.meaning(btn.text, disable_errors=True)
        if meaning is None:
            self.screen.defs.add_widget(
                MDLabel(text=f"Sorry, no meaning for that word.",
                        text_size="self.size")
            )
        else:
            for key in meaning:
                self.screen.defs.add_widget(
                    MDLabel(text=f"Part of speech: {key} {nl}Meaning: {nl}{nl}{meaning[key][0].capitalize()}.",
                            text_size="self.size")
                )


if __name__ == "__main__":
    RhymeApp().run()

有人可以帮忙吗?

首先为 data-class 创建自定义 class,如下所示:

class ListItem(OneLineListItem):
    # Here define all the necessary attrs., methods apart from the defaults (if you need any).

现在在您的 .kv 中将 RecycleView 初始化为,

                RecycleView:
                    id: scroll
                    #size_hint: 0.85,1.5
                    bar_width: dp(15)
                    bar_height: dp(40)
                    scroll_type: ["content"]
                    #pos_hint: {"center_x": 0.45, "center_y": 0.93}
                    viewclass: "ListItem"
                    RecycleBoxLayout:
                        default_size: None, dp(48)
                        default_size_hint: 1, None
                        size_hint_y: None
                        height: self.minimum_height
                        orientation: 'vertical'

现在您已准备好向 RecycleView 提供您的数据,

    def rhyme(self):
        ...
        self.screen.ids.scroll.data = [
            {"text" : f"{i}".capitalize()}
        for i in rhymes]