如何从gridlayout中删除特定的行?从弹出窗口添加行

时间:2019-06-06 05:49:13

标签: python kivy kivy-language

我想使用Delete按钮从gridlayout中删除特定行。在弹出窗口中创建行。我无法在每行中添加删除按钮,但无法弄清楚如何在其中添加逻辑。

这是我尝试过的代码。我玩了一段时间。我尝试了很多方法,这就是我停下来的地方。

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
from kivy.properties import StringProperty, BooleanProperty, ObjectProperty
from kivy.uix.textinput import TextInput


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

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

    def remove_row(self):
        self.remove_widget(Row)

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, x1, x2, x3, x4):
        addbutton = self.root.get_screen('Page2').ids.empty
        addbutton.add_widget(Row(x1, x2, x3, x4))

    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
            Button:
                id:X4
                text:'Delete'

        BoxLayout:
            orientation:'horizontal'
            Button:
                text:'Lisää'
                on_release: app.add_more(X1.text, X2.text, X3.active, X4)
            Button:
                text: 'Close'
                on_press: pop.dismiss()

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

    Label:
        text: root.x1
    Label:
        text: root.x2
    CheckBox:
        active: root.x3
    Button:
        text:'poista'
        on_release: root.remove_row()


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



<MainScreen>:
    name:'Main'
    BoxLayout:
        orientation:'vertical'
        GridLayout:
            id:container
            cols:2
            Label:
                text:'testfield1'
            TextInput:
                id: textfield1
            Label:
                text:'testfield2'
            TextInput:
                id: textfield2

        Button:
            text:'Next Page'
            on_release: app.root.current ='Page2'




<SecondScreen>:
    name:'Page2'


    BoxLayout:
        orientation:'vertical'
        BoxLayout:
            orientation:'vertical'
            Label:
                text:'Popup Test'
            ScrollView:
                bar_width: 5
                bar_color: 1,0,0,1 #red
                bar_inactive_color: 0,0,1,1 #blue
                effect_cls: 'ScrollEffect'
                scroll_type:['bars','content']
                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:'remove'
                    on_release: app.remove()



1 个答案:

答案 0 :(得分:1)

问题2-弹出消息以确认删除

  

...希望该Poista(Delete)按钮打开弹出窗口,询问“您是您吗?   当然?是或否”我应该如何绑定remove_row?

解决方案

kv文件

  • 创建一个继承有Popup小部件的类规则<ConfirmDeleteRow>:
  • 弹出窗口小部件的内容是一个{'1}}小部件,文本为“您确定吗?”,两个Label小部件,文本分别为“是”和“否”。
  • 使用Button事件绑定“是”按钮以调用on_release方法
  • 使用remove_row()事件绑定两个按钮以通过调用on_release方法关闭弹出窗口
  • 绑定dismiss()按钮以调用新方法delete

摘要-kv文件

confirm_delete()

Py文件

  • 用新的类属性<ConfirmDeleteRow>: # class rule size_hint: .4, .4 auto_dismiss: False title: 'Delete' BoxLayout: # content orientation:'vertical' Label: text: 'Are you sure?' BoxLayout: size_hint: 1, 0.2 Button: text: 'Yes' on_release: root.dismiss() root.row.remove_row() Button: text: 'No' on_release: root.dismiss() ... <Row>: x1:'' ... Button: text:'poista' on_release: root.confirm_delete() 和{接受附加参数class ConfirmDeleteRow()的构造函数实施row = ObjectProperty(None)
  • 实施一种新方法row以实例化confirm_delete()并将ConfirmDeleteRow()(即行)作为参数传递

代码段-py文件

self

问题1-删除特定行

  

...想要通过删除从网格布局中删除特定行   按钮

解决方案

当按下按钮“ poista”(删除)时,class ConfirmDeleteRow(Popup): row = ObjectProperty(None) def __init__(self, row, **kwargs): super(ConfirmDeleteRow, self).__init__(**kwargs) self.row = row # save row object class Row(BoxLayout): ... def confirm_delete(self): confirm_delete_row = ConfirmDeleteRow(self) # pass self / row object confirm_delete_row.open() 方法中的self指向remove_row()中该特定行的实例化Row对象。因此,要删除该特定行,必须引用其父行才能从父行中删除它/子行。

ScrollView替换为self.remove_widget(Row)

摘要

self.parent.remove_widget(self)

输出

Before removing second row After second row removed