问题描述
我弹出了一个窗口.基本上是一些选项行(最多5行).
I made a pop-up. It is basically some rows of options (max. 5 rows).
如果我按下"+"按钮,将会有新的选项行.
If I press the '+' button, there will be a new line of options.
如果我按下-"按钮,则最后一行将消失.不幸的是,不是.
If I press the '-' button the last row should diasappear. Unfortunatelly it doesn't.
我已经在root.remove()中尝试了以下方法:
I tried already the followings out in root.remove():
-> widget_path.pop(0):没有视觉变化,我看到的是n行而不是n-1.
--> widget_path.pop(0) :no visual change, I see n rows instead of n-1 .
-> widget_path.pop():它将删除第一行而不是最后一行.
--> widget_path.pop() :It removes the first line instead of the last one.
-> Gridlayout(列数:4)而不是StackLayout:相似的结果
--> Gridlayout (cols: 4) instead of StackLayout: similar results
你能帮我吗?
这是我的代码:
.kv -file;
.kv -file;
<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)
推荐答案
获取小部件,然后将其删除.
一个例子:
Get the widget, and remove it.
An example of that:
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()
这篇关于Kivy从Stack-/GridLayout中删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!