如何获取kivy中按下的按钮的ID?

How to get ID of the button pressed in kivy?

这是我正在使用的示例代码。

class MainScreen(GridLayout):

    def __init__(self, **kwargs):
        super(MainScreen, self).__init__(**kwargs)
        self.cols = 1
        MainScreenLayout = GridLayout()
        MainScreenLayout.cols = 3
       
       #for loop creating buttons with varying IDs
        NumberToCreate = 4
        for x in range(int(NumberToCreate)):
            aButton = Button(text='button text ' + str(x), on_press=self.press_auth)
            MainScreenLayout.add_widget(aButton)
            self.ids['button' + str(x)] = aButton

        self.add_widget(MainScreenLayout)

    #function for when button is pressed
    def press_auth(self, instance):
        print(str(instance)) #ulimate goal is get ID name and x number, to use later in code
        
class MyApp(App):
    def build(self):
        return MainScreen()
    
if __name__== '__main__':
    MyApp().run()

kivy中的ID通常与kv文件结合使用,通过继承来跟踪对象。您可能不应该像您那样将实例变量 ids 用作 setter,因为它通常在内部设置为 kivy 并由开发人员用作 getter。

一个更简单的方法来做你想做的事情是在每个 Button 实例上设置一个任意变量并跟踪那里的差异。如果您计划使用深度继承,这就是您要使用 ids 的原因,那么我会在将实例添加到 MainScreen.

之前在实例上设置 id

第一种方法可以这样简单地完成:

class MainScreen(GridLayout):

    def __init__(self, **kwargs):
        super(MainScreen, self).__init__(**kwargs)
        self.cols = 1
        MainScreenLayout = GridLayout()
        MainScreenLayout.cols = 3
       
       #for loop creating buttons with varying IDs
        NumberToCreate = 4
        for x in range(int(NumberToCreate)):
            aButton = Button(text='button text ' + str(x), on_press=self.press_auth)
            aButton.my_id = x  # or 'button' + str(x) or whatever you want to use to track buttons
            MainScreenLayout.add_widget(aButton)

        self.add_widget(MainScreenLayout)

    #function for when button is pressed
    def press_auth(self, instance):
        print(str(instance.my_id)) #ulimate goal is get ID name and x number, to use later in code
        
class MyApp(App):
    def build(self):
        return MainScreen()
    
if __name__== '__main__':
    MyApp().run()

这应该适合你

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout


class MainScreen(GridLayout):

    def __init__(self, **kwargs):
        super(MainScreen, self).__init__(**kwargs)
        self.cols = 1
        MainScreenLayout = GridLayout()
        MainScreenLayout.cols = 3

        # for loop creating buttons with varying IDs

        NumberToCreate = 4
        for x in range(int(NumberToCreate)):
            aButton = Button(text='button text ' + str(x), on_press=self.press_auth)
            MainScreenLayout.add_widget(aButton)
            # let us make the id similar to the text to simplify the searching
            self.ids['button text ' + str(x)] = aButton

        self.add_widget(MainScreenLayout)

    # function for when button is pressed
    def press_auth(self, instance):
        # here we can accuses the button id using button text 
        print(self.ids[instance.text])


class MyApp(App):
    def build(self):
        return MainScreen()


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


更新

您无法通过按钮实例获取 ID,因为它存储为 MainScreen ID 我的示例是向您展示如何使用其中一个实例 属性 来获取实例,例如文字