如何从框布局中清除循环创建的按钮
我有一个文件夹屏幕,显示循环中的按钮,按钮具有保存的文件的标题,当我单击按钮时,它会打开一个显示文件内容的新页面,但是当我返回文件夹屏幕时,2循环中的按钮组将添加到现有按钮中。 我想要的是每次离开文件夹屏幕时,我都希望清除按钮,这样当我返回时,它将再次运行循环代码并仅显示循环中的按钮而不重复。 或者如果容纳按钮的框布局不为空,可能有一种方法可以阻止循环代码运行。这是我的代码:
def on_enter(self, *args):
objf = mm.Storenote()
objf.tif(mm.DB.hod)
for i in range(len(mm.Storenote.ht)):
self.b = Button(text= (mm.Storenote.ht[i]), font_size = "25sp")
#self.b.background_normal = ""
self.b.background_color = 0,0,1,1
self.b.ids = {"id":mm.Storenote.nid[i]}
self.b.size_hint=(1, None)
self.b.size = (dp(370), dp(50))
self.b.bind(on_press=self.build_clickk(self.b))
self.ids.lpl.add_widget(self.b)
#self.ids.lpl.add_widget(self.l[i])
def build_clickk(self, b):
def clickk(*args):
ci = b.ids.id
print(ci)
objff = mm.Show()
objff.getter(ci)
self.manager.current = "Readpage"
return clickk
def on_leave(self, *args):
self.ids.lpl.remove_widget(self.b)
def on_leave 函数仅删除其中一个按钮,并在每次返回文件夹屏幕时添加 2 组新按钮
i have a folderscreen that displays buttons from a loop, the buttons have title of the files saved, when i click the buttons, it opens a new page that shows the content of the file, but when i go back to the folder screen, 2 sets of the button in the loop are added to the existing buttons.
what i want is everytime i leave the folderscreen, i want the buttons cleared, so that when i go back, it will run the loop code again and shows only the buttons from the loop without repetition.
or may be there is a way i can stop the loop code from running if the boxlayout that house the buttons is not empty. here is my code:
def on_enter(self, *args):
objf = mm.Storenote()
objf.tif(mm.DB.hod)
for i in range(len(mm.Storenote.ht)):
self.b = Button(text= (mm.Storenote.ht[i]), font_size = "25sp")
#self.b.background_normal = ""
self.b.background_color = 0,0,1,1
self.b.ids = {"id":mm.Storenote.nid[i]}
self.b.size_hint=(1, None)
self.b.size = (dp(370), dp(50))
self.b.bind(on_press=self.build_clickk(self.b))
self.ids.lpl.add_widget(self.b)
#self.ids.lpl.add_widget(self.l[i])
def build_clickk(self, b):
def clickk(*args):
ci = b.ids.id
print(ci)
objff = mm.Show()
objff.getter(ci)
self.manager.current = "Readpage"
return clickk
def on_leave(self, *args):
self.ids.lpl.remove_widget(self.b)
the def on_leave function only remove one of the buttons and add 2 new sets of the button each time i go back to the folderscreen
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在 for 循环之前使用 self.ids.lpl.clear_widgets() 。这样,您可以确保在添加新小部件之前布局为空。
如果您添加许多小部件,请尝试使用
def on_pre_enter()
。 kivy 将在进入屏幕之前渲染小部件,这可以防止在构建所有小部件时出现“闪烁”。You can use
self.ids.lpl.clear_widgets()
right before your for-loop. This way you ensure that the layout is empty before adding new widgets.If you add many widgets, try using
def on_pre_enter()
. kivy will render the widgets before entering the screen and this may prevent "flickering" when building all widgets.