如何在循环中获取按钮的 id,以便按下按钮时,函数会获取每个按钮的 id
因此,我尝试使用在循环中创建的按钮的 id 到具有以下代码的函数中:
def on_enter(self, *args):
objff = mm.Show()
objf = mm.Storenote()
objf.tif(mm.DB.hod)
for i in range(len(mm.Storenote.ht)):
#size = dp(150)
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.clickk)
self.ids.lpl.add_widget(self.b)
#self.ids.lpl.add_widget(self.l[i])
def clickk(self, *args):
print(self.b.ids.id)
objff = mm.Show()
objff.getter(self.b.ids.id)
self.manager.current = "Readpage"
self.remove_widget(self.b)
我希望循环中按钮的 on_press 中的 clickk 函数在按下按钮时选择每个按钮的 id 。我想使用按钮的 id 从数据库中检索数据,但是 clickk 函数中的 self.b.ids.id 仅在我按下任何按钮时选择循环中最后一个按钮的 id,我明白因为该函数位于循环之外。我该怎么做才能在每次按下按钮时获取循环中每个按钮的每个 id
so, I am trying to use the id of buttons created in a loop to in a function with the following codes:
def on_enter(self, *args):
objff = mm.Show()
objf = mm.Storenote()
objf.tif(mm.DB.hod)
for i in range(len(mm.Storenote.ht)):
#size = dp(150)
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.clickk)
self.ids.lpl.add_widget(self.b)
#self.ids.lpl.add_widget(self.l[i])
def clickk(self, *args):
print(self.b.ids.id)
objff = mm.Show()
objff.getter(self.b.ids.id)
self.manager.current = "Readpage"
self.remove_widget(self.b)
i want the clickk function in the on_press of the buttons in the loop to pick the ids of each button when the button is pressed. i want to use the ids of the button to retrieve data from the database but the self.b.ids.id in the clickk function is only picking the ids of the last button in the loop when i press any of the button and i understand cos the function is outside of the loop. what can i do to get each ids of each button in the loop every time a button is pressed
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的代码中,您在每次迭代时重新分配相同的 self.b 。也许你可以尝试这样的事情
..并使用
self.buttons[index]
而不是self.b
In your code your reassign the same self.b at each iteration. Maybe you could try something like this
.. and use
self.buttons[index]
instead ofself.b
创建 onClick 回调构建器是控制循环中的
onClick
的常见模式。就你而言,
Creating onClick callback builder is a common pattern to control an
onClick
in a loop.In your case,