2

I'm trying to get an MDDataTable object to expand it's column's width according to the size of the screen, but I can't find a way to do it. Here's my code:

from kivy.metrics import dp
from kivymd.app import MDApp
from kivymd.uix.datatables import MDDataTable
from kivymd.uix.screen import MDScreen

rowdata = [
    ('1', 'something'),
    ('2', 'another thing'),
    ('3', 'a waaaaaaaaaaaaaaaayyyyyyy longer thing')
]

class Test(MDApp):

    def build(self):
        self.data_tables = MDDataTable(
            size_hint=(1, 0.7),
            use_pagination=False,
            check=False,
            rows_num=15,
            column_data=[
                ("#", dp(6)),
                ("Stuff", dp(45)),
            ],
            row_data=rowdata,
        )

        screen = MDScreen()
        screen.add_widget(self.data_tables)
        return screen

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

I think the answer may be related to dp(45), where you define the column's width. Any ideas?

Eduardo C.
  • 37
  • 4

1 Answers1

0

You will need to change the datatables.kv file a little:

<TableData>
  ...
  TableRecycleGridLayout:
    ...
    size_hint_min: self.minimum_width, self.minimum_height
    size_hint: 1, None

And

  <TableHeader>
    ...
    MDGridLayout:
      ...
      size_hint_min: self.minimum_width, self.minimum_height
      adaptive_height: True # replace adaptive_size: True

With this approach, you will keep the minimum column sizes you specified, and with the minimum window size, you will still have scrolling.

But for large screen sizes, all columns will stretch to the full width of the screen