【问题标题】:How to get pressed button coordinates (gridlayout)如何获得按下的按钮坐标(gridlayout)
【发布时间】:2022-07-08 02:31:14
【问题描述】:
n = int(input())

def call_back(self):
    pass

self.layout = GridLayout(cols=n, rows=n, 
                         size_hint = (0.5625, 1), 
                         pos_hint={'center_x': 0.5, 'center_y': 0.5})

for _ in range(n ** 2):
    b = Button(on_press=self.call_back,
               background_color=(25, 25, 25, 1))

return self.layout

call_back 函数应该返回按下的按钮坐标,但我不知道如何实现

【问题讨论】:

标签: python kivy


【解决方案1】:

如果您在谈论GridLayout 的行和列中的坐标,那么您可以将该信息存储在Button 中以便于访问:

    for row in range(n):
        for col in range(n):
            b = Button(on_press=self.call_back,
                       background_color=(25, 25, 25, 1))
            b.coords = (row, col)
            self.layout.add_widget(b)

然后您可以在call_back() 中访问该信息:

def call_back(self, button):
    print('call_back:', button.coords)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-27
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多