我做了一个弹出窗口。它基本上是一些选项行(最多5行)。
如果我按下' +'按钮,会有一个新的选项。
如果我按下' - '按钮最后一行应该是diasappear。不幸的是,它没有。
我在root.remove()中尝试了以下内容:
- > widget_path.pop(0): 没有视觉变化,我看到n行而不是n-1。
- > widget_path.pop(): 它删除了第一行而不是最后一行。
- > Gridlayout(cols:4)而不是StackLayout:类似的结果
你能帮帮我吗?
这是我的代码:
.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)
答案 0 :(得分: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()