【问题标题】:Kivy Removing elements from a Stack- / GridLayoutKivy 从 Stack- / GridLayout 中移除元素
【发布时间】:2017-02-28 20:18:39
【问题描述】:

我做了一个弹出窗口。它基本上是一些选项行(最多 5 行)。

如果我按下“+”按钮,就会出现新的一行选项。

如果我按下“-”按钮,最后一行应该会消失。不幸的是它没有。

我已经在 root.remove() 中尝试了以下内容:

--> widget_path.pop(0) : 没有视觉变化,我看到 n 行而不是 n-1 。

--> widget_path.pop() : 它会删除第一行而不是最后一行。

--> Gridlayout (cols: 4) 而不是 StackLayout: 类似结果

你能帮我吗?

这是我的代码:

.kv -文件;

<FormPopup>:
    size_hint: None, None
    size: '361pt', '220pt'
    BoxLayout:
        orientation: 'vertical'

    BoxLayout:
        size_hint: 1, None
        height: '20pt'
        orientation: 'horizontal'
        Label:
            text: 'column1'
        Label:
            text: 'column2'
        Label:
            text: 'column3'
        Label:
            text: 'column4'
        # list of sensors
        StackLayout:
            padding: 0
            spacing: 1
        orientation: 'lr-tb'
        pos_hint: {'center_x': .5, 'center_y': .5}
                height: self.minimum_height
                id: measure_stack

        BoxLayout:
        orientation: 'horizontal'
            MyButton:
                text: '+'
                    on_release: root.add()
            MyButton:
                    text: '-'
                    on_release: root.remove()

我的 .py 类:

class FormPopup(Popup):
    """Class: Popup for comments"""
    def __init__(self):
        super().__init__()
        self.error = 0
        self.linenumber = 0
        self.add()

    def add(self):
        """add a new option-line"""
        widget_path = self.ids
        if self.linenumber < 5 :
            sensor_id = 'sensor_' + str(self.linenumber)
            widget_path['measure_stack'].add_widget(Spinner(id = sensor_id, size_hint=(None,None), height='20pt', width='85pt', text = '---', values= ('A','B','C') ))
            measurand_id = 'measurand_' + str(self.linenumber)
            widget_path['measure_stack'].add_widget(Spinner(id = measurand_id, size_hint=(None,None), height='20pt', width='85pt', text = '---', values= ('A','B','C') ) )
            branchwise_id = 'branchwise_' + str(self.linenumber)
            widget_path['measure_stack'].add_widget(Spinner(id = branchwise_id, size_hint=(None,None), height='20pt', width='85pt', text = '---', values= ('A','B','C')))
            procedure_id = 'procedure_' + str(self.linenumber)
            widget_path['measure_stack'].add_widget(Spinner(id = procedure_id, size_hint=(None,None), height='20pt', width='85pt', text = '---', values= ('A','B','C'))) 
            self.linenumber += 1

    def remove(self):
        """remove one option-line"""
        widget_path = self.ids.measure_stack.children
        # do not remove if there is only one line
        if len(widget_path) > 4:
            self.linenumber -= 1
            for i in range(4):
                widget_path.pop(0)

【问题讨论】:

    标签: python kivy


    【解决方案1】:

    获取小部件并将其删除。
    一个例子:

    from kivy.app import App
    from kivy.lang import Builder
    
    
    root = Builder.load_string('''
    
    BoxLayout:
        GridLayout:
            id: grid
            cols: 1
            rows: 5
            Label:
                text: "label 1"
            Label:
                text: "label 2"
            Label:
                text: "label 3"
            Label:
                text: "label 4"
    
        Button:
            text: "Remove last item"
            on_release: grid.remove_widget(grid.children[0])
    
    ''')
    
    
    
    class MyApp(App):
    
        def build(self):
            return root
    
    MyApp().run()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-13
      • 1970-01-01
      相关资源
      最近更新 更多