如何在Kivy中动态创建小部件?

时间:2019-11-15 06:32:23

标签: python kivy kivy-language

我正在用Kivy开发MCQ应用程序,该应用程序有2个问题和相关选项。我在应用程序中使用了用于问题的标签,用于回答的标签和复选框。

代码运行正常。但是,问题是应用程序现在只有两个问题,所以还可以。假设,如果应用程序有数百个问题,那么我使用的方法将过于繁琐且可能效率不高。应该有一种方法,我可以使用较少的代码行数来创建动态窗口小部件。请提出一个替代方案,在该方案中,我可以创建动态小部件并改进代码

我的代码:

tut13.py

from kivy.app import App
from kivy.uix.scrollview import ScrollView
from kivy.lang import Builder

class Scroll_2(ScrollView):
    pass


class Demo_12(App):

    def build(self):
        return Builder.load_file("kv\Design11.kv")

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

Design11.ky

<Mygrid@GridLayout>:
    canvas.before:
        Color:
            rgba: 0.52, 0.8, 1, 1

        Rectangle:
            pos: self.pos
            size: self.size

    size_hint: 0.95, None
    pos_hint: {'x': 0.025}
    height: 200
    padding: 20

<Mycheck@CheckBox>:
    canvas.before:           
        Color:
            rgba: 0, 0, 1, 1
        Rectangle:
            pos: self.center_x-8, self.center_y-8
            size: 16, 16
    color: 1, 1, 0, 1            

<Mylabel@ButtonBehavior+Label>:
    color: 0, 0, 0, 1
    font_size: 18
    bold: True

Scroll_2:
    BoxLayout:
        orientation: 'vertical'
        size_hint_y: None
        height: self.minimum_height
        spacing: 20

        Mygrid:
            cols: 1 

            Mylabel: 
                text: 'Question1'                

            BoxLayout:
                Mycheck:
                    id: q1opt1cb1
                    on_press: if self.active: print(q1opt1.text)

                Mylabel:
                    id: q1opt1
                    text: 'Option 1'
                    on_press: 
                        q1opt1cb1._do_press()
                        if q1opt1cb1.active: print(self.text)

                Mycheck:
                    id: q1opt1cb2
                    on_press: if self.active: print(q1opt2.text)

                Mylabel:
                    id: q1opt2
                    text: 'Option 2'
                    on_press: 
                        q1opt1cb2._do_press()
                        if q1opt1cb2.active: print(self.text)

                Mycheck:
                    id: q1opt1cb3
                    on_press: if self.active: print(q1opt3.text)

                Mylabel:
                    id: q1opt3
                    text: 'Option 3'
                    on_press: 
                        q1opt1cb3._do_press()
                        if q1opt1cb3.active: print(self.text)


        Mygrid:
            cols: 1 

            Mylabel: 
                text: 'Question2'                

            BoxLayout:
                Mycheck:
                    id: q2opt1cb1
                    on_press: if self.active: print(q2opt1.text)

                Mylabel:
                    id: q2opt1
                    text: 'Option 1a'
                    on_press: 
                        q2opt1cb1._do_press()
                        if q2opt1cb1.active: print(self.text)

                Mycheck:
                    id: q2opt1cb2
                    on_press: if self.active: print(q2opt2.text)

                Mylabel:
                    id: q2opt2
                    text: 'Option 2a'
                    on_press: 
                        q2opt1cb2._do_press()
                        if q2opt1cb2.active: print(self.text)

                Mycheck:
                    id: q2opt1cb3
                    on_press: if self.active: print(q2opt3.text)

                Mylabel:
                    id: q2opt3
                    text: 'Option 3a'
                    on_press: 
                        q2opt1cb3._do_press()
                        if q2opt1cb3.active: print(self.text)

1 个答案:

答案 0 :(得分:0)

也许这会有所帮助:

.py代码

class Scroll_2(ScrollView):

    def __init__(self, **kwargs):
        super(Scroll_2, self).__init__(**kwargs)
        self.setup_questions()

    def setup_questions(self):
        data = [["Question 1", ["Option 1", "Option 2", "Option 3"]],
                ["Question 2", ["Option 1", "Option 2"]],
                ["Question 3", ["Option 1", "Option 2", "Option 3", "Option 4"]],
                ["Question 4", ["Option 1", "Option 2", "Option 3"]],
                ]
        for i in data:
            question = MyGrid(q_text=i[0])
            for j in i[1]:
                question.ids.o_box.add_widget(Option(text=j))
            self.ids.q_box.add_widget(question)

class MyGrid(GridLayout):
    q_text = StringProperty()


class Option(BoxLayout):
    text = StringProperty()

class Demo_12(App):

    def build(self):
        return Scroll_2()

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

和.kv代码

<Mycheck@CheckBox>:
    canvas.before:           
        Color:
            rgba: 0, 0, 1, 1
        Rectangle:
            pos: self.center_x-8, self.center_y-8
            size: 16, 16
    color: 1, 1, 0, 1            

<Mylabel@ButtonBehavior+Label>:
    color: 0, 0, 0, 1
    font_size: 18
    bold: True

<Scroll_2>:
    BoxLayout:
        id: q_box
        orientation: 'vertical'
        size_hint_y: None
        height: self.minimum_height
        spacing: 20

<Option>:
    Mycheck:
        id: opt1cb1
        on_press: if self.active: print(root.text)
    Mylabel:
        text: root.text
        on_press: 
            opt1cb1._do_press()
            if opt1cb1.active: print(self.text)

<MyGrid>:
    canvas.before:
        Color:
            rgba: 0.52, 0.8, 1, 1

        Rectangle:
            pos: self.pos
            size: self.size

    size_hint: 0.95, None
    pos_hint: {'x': 0.025}
    height: 200
    padding: 20
    cols: 1
    Mylabel: 
        text: root.q_text
    BoxLayout:
        id: o_box