【问题标题】:kivy button ID's within functions函数中的 kivy 按钮 ID
【发布时间】:2016-04-21 17:17:15
【问题描述】:

我正在为一个学校项目开发一个梦幻足球类型的应用程序。 我们创建了一个滚动视图,其中包含一个团队中的角色列表,每个角色都分配给一个按钮。按下按钮时,会显示一个新的滚动视图,其中显示第二个“非活动角色按钮”列表,允许用户按一个按钮将第一个和第二个角色从一个团队交换到另一个团队。

我们的问题来自于难以“定位”按下哪个按钮以告诉我们的交换函数在列表中交换哪两个字符。是否可以保留按钮的 id 并在按下所述按钮时将其调用为新功能?

我们的代码有点乱,但显示如下:

class SMApp(App):
    teamlist = []
    idvar = ""
    btnlist = []

    def popupfunc(self, event):

        """
        creates a popup asking if the user wishes to swap a character from team to subs
        then proceeds to allow user to choose who swaps
        """

        def subscroll(self):
            """
            opens scroll list of substitute characters in a popup
            """
            sublist = []
            curs.execute('SELECT * FROM Subs')
            for row in curs:
                sublist.append([row[0], row[2]])

            layout = GridLayout(cols=2, spacing=10, size_hint_y=None)
            layout.bind(minimum_height=layout.setter('height'))
            for i in range(len(sublist)):
                btn = Button(text=str(sublist[i][0]), size_hint_y=None, height=40)
                layout.add_widget(btn)
                lbl = Label(text=str(sublist[i][1]), size_hinty=None, height=40)
                layout.add_widget(lbl)
            root = ScrollView(size_hint=(None, None), size=(400, 400))
            root.add_widget(layout)
            popup2 = Popup(content=root, size=(7, 10), size_hint=(0.55, 0.8), title="list of subs")
            popup2.open()

        box = BoxLayout()
        btn1 = Button(text='yeah ok')
        btn2 = Button(text='nope')
        popup1 = Popup(content=box, size=(10, 10), size_hint=(0.3, 0.3), title="add to team?")
        btn2.bind(on_press=popup1.dismiss)
        btn1.bind(on_press=subscroll)
        box.add_widget(btn1)
        box.add_widget(btn2)

        popup1.open()


    def build(self):

        curs.execute('SELECT * FROM Team')
        for row in curs:
            self.teamlist.append([row[0], row[2]])

        layout = GridLayout(cols=2, spacing=10, size_hint_y=None)
        layout.bind(minimum_height=layout.setter('height'))

        for i in range(len(self.teamlist)):
            btn = Button(text=str(self.teamlist[i][0]), size_hint_y=None, height=40, id=str(i))
            btn.bind(on_press=self.popupfunc)
            self.btnlist.append(btn)
            layout.add_widget(btn)
            lbl = Label(text=str(self.teamlist[i][1]), size_hinty=None, height=40)
            layout.add_widget(lbl)
        for item in self.btnlist:
            print item.id
        root = ScrollView(size_hint=(None, None), size=(400, 400),
            pos_hint={'center_x':.5, 'center_y':.5})
        root.add_widget(layout)
        return root

if __name__ == '__main__':
    SMApp().run()

【问题讨论】:

    标签: button kivy


    【解决方案1】:

    您创建的每个btn = Button(...) 都是不同的对象,因此您可以分辨出哪个被按下。问题是你会选择什么样的方式。

    你可以使用:

    • str(你的按钮)并获得一个特定的对象地址(?)像 0xAABBCCEE
      • 不好,不要那样做
    • 按钮(id='something', ...)
    • 来自 kv 语言的 ID

    或使用特定标识符的属性创建自己的小部件。然后你会为父母的孩子使用一个循环来检查标识符并做一些事情:

    for child in layout.children:
        if child.id == 'something':
            # do something
    

    看来您需要在子滚动中使用此循环,或者以其他方式访问 layout

    【讨论】:

      猜你喜欢
      • 2018-11-11
      • 1970-01-01
      • 2016-02-08
      • 2017-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-20
      相关资源
      最近更新 更多