如何在循环中获取按钮的 id,以便按下按钮时,函数会获取每个按钮的 id

发布于 2025-01-12 05:08:48 字数 987 浏览 0 评论 0原文

因此,我尝试使用在循环中创建的按钮的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

新雨望断虹 2025-01-19 05:08:48

在您的代码中,您在每次迭代时重新分配相同的 self.b 。也许你可以尝试这样的事情

self.buttons = list()
for i in range(len(mm.Storenote.ht)):
        b = Button(text= (mm.Storenote.ht[i]), font_size = "25sp")
        b.background_normal = ""
        b.background_color = 0,0,1,1
        b.ids =  {"id":mm.Storenote.nid[i]}
        b.size_hint=(1, None)
        b.size = (dp(370), dp(50))
        b.bind(on_press=self.clickk)
        self.buttons.append(b)
        self.ids.lpl.add_widget(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

self.buttons = list()
for i in range(len(mm.Storenote.ht)):
        b = Button(text= (mm.Storenote.ht[i]), font_size = "25sp")
        b.background_normal = ""
        b.background_color = 0,0,1,1
        b.ids =  {"id":mm.Storenote.nid[i]}
        b.size_hint=(1, None)
        b.size = (dp(370), dp(50))
        b.bind(on_press=self.clickk)
        self.buttons.append(b)
        self.ids.lpl.add_widget(b)

.. and use self.buttons[index] instead of self.b

流殇 2025-01-19 05:08:48

创建 onClick 回调构建器是控制循环中的 onClick 的常见模式。

def build_onClick(self, something):
  def onClick(event):
    // do with something
  return onClick

self.b.bind(on_press=self.build_onClick(something))

就你而言,

...
        self.b.bind(on_press=self.build_clickk(self, self.b)
...
def build_clickk(self, b)
  def clickk(*args):
      print(b.ids.id)
      objff = mm.Show()
      objff.getter(b.ids.id)
      self.manager.current = "Readpage"
      self.remove_widget(b)
  return clickk

Creating onClick callback builder is a common pattern to control an onClick in a loop.

def build_onClick(self, something):
  def onClick(event):
    // do with something
  return onClick

self.b.bind(on_press=self.build_onClick(something))

In your case,

...
        self.b.bind(on_press=self.build_clickk(self, self.b)
...
def build_clickk(self, b)
  def clickk(*args):
      print(b.ids.id)
      objff = mm.Show()
      objff.getter(b.ids.id)
      self.manager.current = "Readpage"
      self.remove_widget(b)
  return clickk
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文