【发布时间】:2021-08-25 18:14:44
【问题描述】:
我在 Kivy 中设置了一个 CheckBox,当勾选它时,会添加一个带有一些标签和 TextInputs 的布局小部件,但我希望它在未勾选该框时删除布局。勾选复选框会添加具有正确大小和位置的布局(在带有 CheckBox 的 GridLayout 下方,在 input_layout BoxLayout 内)但是当我取消勾选它时,布局不会消失。我检查了它是否会删除 input_layout 布局,它确实会删除,但不是通过 python 代码添加的。
取消选中该框时打印语句打印 False,因此我知道它工作正常,但 self.ids.input_layout.remove_widget(self.layout) 没有删除布局。
我尝试设置一个模板 GridLayout,然后通过 CheckBox 和 python 代码将标签和 TextInputs 添加到该模板中。此设置部分有效; Checkbox 会添加小部件,然后取消勾选它然后删除它们,但我无法再次添加它们,因为整个 GridLayout 都消失了。
其他复选框将添加/删除不同的布局,所以我的第一次设置尝试更理想,如果它真的可以工作的话。
更新:
使用问题的可运行示例更新代码。
Python
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.metrics import dp
Builder.load_file("process_data_example_design.kv")
class ProcessDataEx(BoxLayout):
def add_layout(self, checkbox, value):
self.layout = GridLayout(cols=2, rows=4, size_hint_y=None, height=self.height*0.275, spacing=dp(8), padding=dp(8))
self.layout.add_widget(Label(text="Histogram particle label:", size_hint_x=None, width=self.width*0.33))
self.layout.add_widget(TextInput())
self.layout.add_widget(Label(text="Dumps to iterate:", size_hint_x=None, width=self.width*0.33))
self.layout.add_widget(TextInput())
self.layout.add_widget(Label(text="X-axis max:", size_hint_x=None, width=self.width*0.33))
self.layout.add_widget(TextInput())
self.layout.add_widget(Label(text="Y-axis max:", size_hint_x=None, width=self.width*0.33))
self.layout.add_widget(TextInput())
if value:
self.ids.input_layout.add_widget(self.layout)
else:
self.ids.input_layout.remove_widget(self.layout)
print(value)
class ProcessDataApp(App):
def build(self):
return ProcessDataEx()
if __name__ == '__main__':
ProcessDataApp().run()
基维
#:kivy 2.0.0
<ProcessDataEx>:
BoxLayout:
orientation: 'vertical'
BoxLayout:
id: input_layout
orientation: 'vertical'
GridLayout:
cols: 3
rows: 2
height: self.minimum_height
spacing: dp(8)
padding: dp(8)
Label:
text: 'Move data'
size_hint_y: None
height: self.texture_size[1]
halign: 'right'
Label:
text: 'Histograms'
size_hint_y: None
height: self.texture_size[1]
halign: 'right'
Label:
text: 'Log Histograms'
size_hint_y: None
height: self.texture_size[1]
halign: 'right'
CheckBox:
size_hint_y: None
height: root.height * 0.05
CheckBox:
size_hint_y: None
height: root.height * 0.05
CheckBox:
size_hint_y: None
height: root.height * 0.05
on_active: root.add_layout(*args)
【问题讨论】:
-
能否添加一个简单的可运行示例
-
@HussamF.Alkdary 用可运行的示例更新了帖子中的代码
标签: python kivy kivy-language