如何使用on_row_press()方法从kivymd数据表中的行获取值



我正试图从我将按下的行获取数据。但是我没有得到任何相关的数据在我的表格中。这是我的代码

from kivymd.app import MDApp
from kivymd.uix.screen import Screen
from kivy.metrics import dp
from kivymd.uix.datatables import MDDataTable
class Example(MDApp):
def build(self):
screen =Screen()
table = MDDataTable(
column_data=[('roll no',dp(15)),
('name',dp(30))],
row_data=[(1,'Hasib'),
(2,'shihab')])
table.bind(on_row_press=self.row_press)
screen.add_widget(table)
return screen
def row_press(self,instance_table,instance_row):
print(instance_row)
Example().run()

我得到了这个& lt; kivymd.uix.datatables.datatables。

CellRow对象在0x0000020A467307B0>如果我按任意键,我想要得到roll和name。我看了医学文档,但没有找到答案请帮帮我

如果使用"print(instance_row.text)"您将获得该特定列和行的文本。

index是表中单元格的索引。除以行数得到row_data索引

ind = instance_row.index // 2 # number of rows 
row_data = instance_table.row_data[ind]

最新更新