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

时间:2019-05-31 08:09:11

标签: python python-3.x popup kivy

我的屏幕上有两个BoxLayout。第一个是空的。第二个按钮具有打开弹出窗口的功能。在弹出窗口中,我有2个textinputs和复选框。我也想将这些textinputs添加为该boxlayout和checkbox的标签。

如果我在该屏幕上有按钮,则可以在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()

2 个答案:

答案 0 :(得分:1)

If I understand what you are trying to do, you can get the text from your TextInput by saving a reference to the MyPopup that gets created in your SecondScreen:

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

Then use that reference in your add_more() method:

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))

Of course, you can reference the X2 similarly.

答案 1 :(得分:0)

问题

  

我想将这些textinput作为标签添加到该boxlayout中,   复选框。

解决方案

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

kv文件

  • 实施视图以查看类规则<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文件

  • 声明继承为class Row()的{​​{1}}。
  • 声明类型为BoxLayoutx1的{​​{1}},x2x3的类属性
  • 实施构造函数以填充StringPropertyBooleanPropertyx1
  • 修改方法x2以接受x3add_more()x1

代码段-py文件

x2

输出

Result