【问题标题】:Add a widget that was created in kv from py添加一个从 py 以 kv 创建的小部件
【发布时间】:2020-09-09 21:27:17
【问题描述】:

有没有办法从 py 文件中引用自定义小部件?

我在 kv 中制作了一个小部件,但我想从 py 中引用它,然后将其再次添加到 kv 中的另一个小部件中。 我尝试使用 id 执行此操作,但出现错误 (KeyError: 'words_entry')。

这是我尝试过的:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.uix.textinput import TextInput
import os


class GetCount(Screen):

    count_input = ObjectProperty(None)

    def next(self):

        # Setup next screen
        text_inputs = [self.ids.words_entry for i in range(int(self.count_input.text))]

        for text_input in text_inputs:
            self.manager.ids.get_input.ids.grid.add_widget(text_input)

        # Switch to next screen
        self.manager.current = "get_input"


class GetInput(Screen):
    pass


kv_file = Builder.load_string("""
ScreenManager:

    GetCount:
        name: "get_count"
        id: get_count

    GetInput:
        name: "get_input"
        id: get_input

<WordEntry@TextInput>:
    id: words_entry

    multiline: False
    size_hint: (self.width, None)

<GetCount>:
    
    count_input: count_input
    
    FloatLayout:
        
        Label:
            text: "count"
            size_hint: 1, 0.05
            pos_hint: {"top":0.9}

        TextInput:
            id: count_input 

            size_hint: 0.8, 0.05
            pos_hint: {"top":0.7, "x":0.1}
            multiline: False

        Button:
            text: "Next"
            on_release: root.next()
            size_hint: 0.8, 0.05
            pos_hint: {"top":0.5, "x":0.1}

<GetInput>:
    
    ScrollView:

        GridLayout:
            size_hint_y: None
            height: self.minimum_height
            id: grid

            cols: 1
""")


class MainApp(App):

    def build(self):
        return kv_file


if __name__ == "__main__":

    app = MainApp()
    app.run()

在这段代码中,我想将WordEntry添加到py中GetInput中的GridLayout(原因是我需要根据用户的输入添加多个)。

【问题讨论】:

    标签: python python-3.x kivy kivy-language


    【解决方案1】:

    您可以使用Factory 创建已在kv 中定义的类的实例。所以你的GetCount 类可以是:

    from kivy.factory import Factory
    
    class GetCount(Screen):
        count_input = ObjectProperty(None)
    
        def next(self):
            # Setup next screen
            for _ in range(int(self.count_input.text)):
                new_word_entry = Factory.WordEntry()
                self.manager.ids.get_input.ids.grid.add_widget(new_word_entry)
    
            # Switch to next screen
            self.manager.current = "get_input"
    

    【讨论】:

    • 如果我的自定义小部件是&lt;WordEntry@GridLayout&gt;: 并且其中有一个TextInput,我如何从py 中引用Textinput 的文本? (感谢您的精彩回答!)
    • 由于您是从 Python 添加这些小部件,因此 WordEntry 实例将没有 ids 条目。但是,每个WordEntry 实例可以有自己的ids 字典,基于kv 中的WordEntry> 规则。当然,每个WordEntry 实例中的ids 将是相同的。要访问 WordEntry 实例,您需要保留对它们的引用,可能在某个列表中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-09
    • 1970-01-01
    相关资源
    最近更新 更多