Search code examples
pythondatatableskivykivy-languagekivymd

Change mddatatable row background color at on_row_press in kivymd data table


Description of the Feature

i have a data table like this i need to change every clicked row background or I want to change the background color of last pressed row or i want to know how to change the row_pressed background color in kivymd data table how can i make it ?

Code and Logs

  from kivy.metrics import dp
  from kivy.uix.anchorlayout import AnchorLayout
  
  from kivymd.app import MDApp
  from kivymd.uix.datatables import MDDataTable
  
  
  class Example(MDApp):
      def build(self):
          layout = AnchorLayout()
          self.data_tables = MDDataTable(
              use_pagination=True,
              check=False,
              column_data=[
                  ("No.", dp(30)),
                  ("Status", dp(30)),
                  ("Signal Name", dp(60)),
                  ("Severity", dp(30)),
                  ("Stage", dp(30)),
                  ("Schedule", dp(30)),
                  ("Team Lead", dp(30))
              ],
              row_data=[
                  ("1", ("alert", [255 / 256, 165 / 256, 0, 1], "No Signal"),
                   "Astrid: NE shared managed", "Medium", "Triaged", "0:33",
                   "Chase Nguyen"),
                  ("2", ("alert-circle", [1, 0, 0, 1], "Offline"),
                   "Cosmo: prod shared ares", "Huge", "Triaged", "0:39",
                   "Brie Furman"),
                  ("3", (
                      "checkbox-marked-circle",
                      [39 / 256, 174 / 256, 96 / 256, 1],
                      "Online"), "Phoenix: prod shared lyra-lists", "Minor",
                   "Not Triaged", "3:12", "Jeremy lake"),
  
              ],
          )
          self.data_tables.bind(on_row_press=self.on_row_press)
          layout.add_widget(self.data_tables)
          return layout
  
      def on_row_press(self, instance_table, instance_row):
          '''Called when a table row is clicked.'''
  
          print(instance_table, instance_row)
  
  
  Example().run()


Solution

  • according to Kivy DateTables Documentation, when instantiating MDDataTable, you need to add background_color_selected_cell="e4514f" to overwrite the default None value.,

    self.data_tables = MDDataTable(
        ...,
        background_color_selected_cell="e4514f",
    )