1
2

fletで縦横スクロール付きのDataTableを表示する

Last updated at Posted at 2024-07-19
main.py
import flet as ft

def main(page: ft.Page):
    """縦横スクロール付きDataTable
    縦スクロール(コンテナのheight必須):
        ft.Container(
            content=ft.ListView(
                ft.DataTable()
            )
            height=CONTAINER_HEIGHT
            width=CONTAINER_WIDTH 
        )
    横スクロール(scroll="auto" or "always"必須):
      ft.Row(scroll="auto")
      ウィンドウ幅 < コンテンツ幅
    """
    CONTAINER_HEIGHT = 500
    CONTAINER_WIDTH = 450
    page.window_width = 400

    dt_cols = []
    for col in ["ID", "Name", "Age"]:
        dt_cols.append(ft.DataColumn(ft.Text(col))) 

    dt_rows = []
    for row in [(x, f"Test{x}", x) for x in range(1, 101)]:
        cells = []
        for cell in row:
            cells.append(ft.DataCell(ft.Text(cell)))
        dt_rows.append(ft.DataRow(cells))

    dt = ft.DataTable(
        columns=dt_cols,
        rows=dt_rows
    )

    lv = ft.ListView([dt], expand=1, spacing=10, padding=20)

    page.add(
        ft.Row(
            [
                ft.Container(
                    content=lv,
                    border=ft.border.all(1),
                    height=CONTAINER_HEIGHT,
                    width=CONTAINER_WIDTH,
                ),
            ],
            scroll="auto",
        )
    )

ft.app(target=main)
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2