在 KivyMD 数据表中禁用悬停效果

问题描述 投票:0回答:1

我在这里提供了一个简单的数据表示例,我想禁用鼠标悬停时的突出显示效果。

    import os
    os.environ['KIVY_GL_BACKEND'] = 'angle_sdl2'
    from kivy.metrics import dp
    from kivy.uix.boxlayout import BoxLayout
    from kivymd.app import MDApp
    from kivymd.uix.datatables import MDDataTable
    
    
    class DynamicTableExample(MDApp):
        def build(self):
            layout = BoxLayout(orientation='vertical')
    
            # Create a data table with custom cell height
            table = MDDataTable(
                #background_color_header="#65275d",
                #background_color_cell="#451938",
                #background_color_selected_cell="#451938",
                size_hint=(1, 1),
                use_pagination=True,
                check=True,
                column_data=[
                    ("Header 1", dp(50)),  # Adjust the width as needed
                    ("Header 2", dp(50)),
                    ("Header 3", dp(50)),
                ],
                row_data=[
                    ("This is a long text for Row 1 Data 1.\n " * 5, "This is a long text for without next line This is a long text for without next line This is a long text for without next line This is a long text for without next line ", "This is a long text for Row 1 Data 3"),
                    ("51", "28", "387"),
                    ("58", "42", "78"),
                ]
            )
    
            layout.add_widget(table)
    
            return layout
    if __name__ == "__main__":
        DynamicTableExample().run() 

我想这样做是为了尽可能提高性能。

python frontend kivy mouseevent kivymd
1个回答
0
投票

您可以通过调整底层组件的视觉效果来改变数据表的外观,以消除将鼠标悬停在 KivyMD

MDDataTable
中的单元格上时的突出显示效果。这可以通过更改
HoverBehavior
HoverBehaviorColor
类的
DataTableRow
DataTable
属性的外观来实现。具体方法如下:

import os
os.environ['KIVY_GL_BACKEND'] = 'angle_sdl2'
from kivy.metrics import dp
from kivy.uix.boxlayout import BoxLayout
from kivymd.app import MDApp
from kivymd.uix.datatables import MDDataTable
from kivymd.uix.datatable import DataTableRow


class MyDataTableRow(DataTableRow):
    def on_enter(self, *args):
        pass

    def on_leave(self, *args):
        pass


class DynamicTableExample(MDApp):
    def build(self):
        layout = BoxLayout(orientation='vertical')

        # Create a data table with custom cell height
        table = MDDataTable(
            size_hint=(1, 1),
            use_pagination=True,
            check=True,
            column_data=[
                ("Header 1", dp(50)),
                ("Header 2", dp(50)),
                ("Header 3", dp(50)),
            ],
            row_data=[
                ("This is a long text for Row 1 Data 1.\n " * 5, "This is a long text for without next line This is a long text for without next line This is a long text for without next line This is a long text for without next line ", "This is a long text for Row 1 Data 3"),
                ("51", "28", "387"),
                ("58", "42", "78"),
            ],
            row_cls=MyDataTableRow  # Use the customized row class
        )

        layout.add_widget(table)

        return layout


if __name__ == "__main__":
    DynamicTableExample().run()

在上面的代码中创建一个自定义

DataTableRow
类 (
MyDataTableRow
),通过重写
on_enter
on_leave
方法不执行任何操作,禁用鼠标悬停在单元格上时的突出显示效果。然后,将我们的自定义行类分配给
row_cls
MDDataTable
属性。

© www.soinside.com 2019 - 2024. All rights reserved.