如何从弹出窗口向 boxlayout 添加新标签?

How to add new label to boxlayout from popup?

我的屏幕有两个方框布局。第一个是空的。第二个有按钮打开弹出窗口。在弹出窗口中,我有 2 个文本输入和复选框。我也想将这些文本输入作为标签添加到该框布局和复选框。

如果我在该屏幕上有按钮,我可以将按钮添加到 boxlayout。但是一旦我们转到弹出窗口,我就不知道该怎么做了。

Py.

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.uix.screenmanager import Screen, ScreenManager


class MyPopup(Popup):
    pass


class MainScreen(Screen):
    pass

class SecondScreen(Screen):
    def fire_popup(self):
        pops = MyPopup()
        pops.open()

class ScreenManagement(ScreenManager):
    def changescreen(self, value):

        try:
            if value !='main':
                self.current = value
        except:
            print('No Screen named'+ value)




class testiApp(App):
    def build(self):
        self.title = 'Hello'

    def add_more(self):
        addbutton = self.root.get_screen('Page2').ids.empty
        addbutton.add_widget(Button(text='Hello'))

    def remove(self):
        container = self.root.get_screen('Page2').ids.empty
        if len(container.children) > 0:
            container.remove_widget(container.children[0])

testiApp().run()



Kv.


<MyPopup>:
    id:pop
    size_hint: .4, .4
    auto_dismiss: False
    title: 'XXX!!'
    BoxLayout:
        orientation:'vertical'
        BoxLayout:
            orientation:'horizontal'
            Label:
                text:'X1'
            TextInput:
                id: X1

            Label:
                text:'X2'
            TextInput:
                id:X2

            CheckBox:
                id:X3

        BoxLayout:
            orientation:'horizontal'
            Button:
                text:'Add'
                on_release: app.add_more()
            Button:
                text: 'Close'
                on_press: pop.dismiss()


ScreenManagement:
    MainScreen:
        name:'Main'
    SecondScreen:
        name:'Page2'



<MainScreen>:
    name:'Main'
    BoxLayout:
        Button:
            text:'Next Page'
            on_release: app.root.current ='Page2'



<SecondScreen>:
    name:'Page2'


    BoxLayout:
        orientation:'vertical'
        BoxLayout:
            orientation:'vertical'
            Label:
                text:'Popup Test'
            ScrollView:
                GridLayout:
                    orientation: "vertical"
                    size_hint_y: None
                    height: self.minimum_height
                    row_default_height: 60
                    cols:1
                    id:empty
            BoxLayout:
                Button:
                    text:'Open Popup'
                    on_press: root.fire_popup()

                Button:
                    text:'Add'
                    on_release: app.add_more()

                Button:
                    text:'remove'
                    on_release: app.remove()

如果我明白你想做什么,你可以通过保存对 [=15] 中创建的 MyPopup 的引用,从 TextInput 中获取 text =]:

class SecondScreen(Screen):
    def fire_popup(self):
        self.pops = MyPopup()
        self.pops.open()

然后在您的 add_more() 方法中使用该引用:

def add_more(self):
    addbutton = self.root.get_screen('Page2').ids.empty
    addbutton.add_widget(Button(text=self.root.current_screen.pops.ids.X1.text))

当然也可以类似的引用X2

问题

I want to add those textinputs as a labels to that boxlayout and checkbox as well.

解决方案

解决方案涉及对 kv 文件和 Python 脚本的增强。

kv 文件

  • 实施 class 规则的视图,<Row>:
  • X1X2X3 的值传递给方法 add_more()
  • 可选:将属性添加到 ScrollView:

片段 - kv 文件

<Row>:
    x1: ''
    x2: ''
    x3: False

    Label:
        text: root.x1
    Label:
        text: root.x2
    CheckBox:
        active: root.x3

<MyPopup>:
    ...

        BoxLayout:
            orientation:'horizontal'
            Button:
                text:'Add'
                on_release: app.add_more(X1.text, X2.text, X3.active)
...

<SecondScreen>:
    ...

            ScrollView:
                bar_width: 10
                bar_color: 1, 0, 0, 1   # red
                bar_inactive_color: 0, 0, 1, 1   # blue
                effect_cls: "ScrollEffect"
                scroll_type: ['bars', 'content']

py文件

  • 声明继承 BoxLayoutclass Row()
  • 为类型为 StringPropertyBooleanProperty
  • x1x2x3 声明 class 属性
  • 实现构造函数来填充 x1x2x3
  • 修改方法 add_more() 以接受 x1x2x3

片段 - py 文件

class Row(BoxLayout):
    x1 = StringProperty('')
    x2 = StringProperty('')
    x3 = BooleanProperty(False)

    def __init__(self, x1, x2, x3, **kwargs):
        super(Row, self).__init__(**kwargs)
        self.x1 = x1
        self.x2 = x2
        self.x3 = x3


class testiApp(App):
    ...
    def add_more(self, x1, x2, x3):
        addbutton = self.root.get_screen('Page2').ids.empty
        addbutton.add_widget(Row(x1, x2, x3))

输出