Display Icons in MdDataTable when pulling from excel

286 views
Skip to first unread message

Ian Rajkumar

unread,
Jul 13, 2021, 8:48:49 AM7/13/21
to Kivy users support
Hey, I am trying to display icons in MdDatatable, my data is stored in an excel file (see image attached)

I am making a language app and after a quiz, I want to display the student's answer below the correct answer. In column "A", the student's response is marked either "correct" or "wrong". If the student was correct then I want to replace the word "correct" with a green checkmark and if they were incorrect I want to replace "wrong" with a red "x"

According to the guide below, I can display icons using this format: 
    (“MDicon-name”, [icon color in rgba], “Column Value”)

However, no matter what I try, kivy is only displaying the data value from excel. For example, if I enter the following, Kivy will not change them to icons:
  • "checkbox-marked-circle", [39 / 256, 174 / 256, 96 / 256, 1], "Online"
  • ['icon', 'No Signal']
How can I get kivy to replace the words "correct" & "wrong" with icons?

My Code: https://pastebin.com/bmgefwLe
Screenshot_4.png

Elliot Garbus

unread,
Jul 13, 2021, 1:19:26 PM7/13/21
to kivy-...@googlegroups.com

The code below works.  It appears that if the icon is in the first column, it is not interpreted as a icon.  I added a row number to your table, and then the icons displayed.

 

 
import openpyxl
from kivy.metrics import dp
from kivymd.uix.datatables import MDDataTable
from kivy.uix.anchorlayout import AnchorLayout
from kivymd.app import MDApp


class Example(MDApp):
   
def build(self):
        layout = AnchorLayout()
        excelLocation =
"answer2.xlsx"
       
excelWorkbook = openpyxl.load_workbook(excelLocation)
        excelSheet =
'Sheet1'
       
# workSheet = excelWorkbook[excelSheet]
        # values = workSheet.values
       
icons = {'wrong': ("alert-circle", [1, 0, 0, 1], "Wrong"),
                
'correct': ("checkbox-marked-circle", [39 / 256, 174 / 256, 96 / 256, 1], "Correct")}
        values = [[
str(i + 1), *row] for i, row in enumerate(excelWorkbook[excelSheet].values)]
       
for i in range(len(values)):
            values[i][
1] = icons.get(values[i][1], values[i][1])  # replace correct or wrong with icon tuple

       
data_tables = MDDataTable(
           
size_hint=(1, 1),
           
use_pagination=True,
           
rows_num=20,
           
column_data=[
                (
'#', dp(20)),    # added a leading row number
               
("Result", dp(30)),
                (
"Answers", dp(80)),
            ],
           
row_data=values
        )
        layout.add_widget(data_tables)
       
return layout

Example().run()

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/0587be89-c039-4c89-b747-009e52d24499n%40googlegroups.com.

 

Message has been deleted

Ian Rajkumar

unread,
Jul 13, 2021, 3:06:49 PM7/13/21
to Kivy users support
Is there a workaround to get the icon to appear in the first column or at least a way to make the numbers in the column that you added appear blank?
maybe we can change the color of the numbers in the first column to white? 

Elliot Garbus

unread,
Jul 13, 2021, 3:13:51 PM7/13/21
to kivy-...@googlegroups.com

I does not work properly with an icon in the leading column.  As a work around I set the width of the first column to 0 and the content to a null string.  I believe this works as desired.

 

import openpyxl
from kivy.metrics import dp
from kivymd.uix.datatables import MDDataTable
from kivy.uix.anchorlayout import AnchorLayout
from kivymd.app import MDApp


class Example(MDApp):
   
def build(self):
        layout = AnchorLayout()
        excelLocation =
"answer2.xlsx"
       
excelWorkbook = openpyxl.load_workbook(excelLocation)
        excelSheet =
'Sheet1'
       
# workSheet = excelWorkbook[excelSheet]
        # values = workSheet.values
       
icons = {'wrong': ("alert-circle", [1, 0, 0, 1], "Wrong"),
                
'correct': ("checkbox-marked-circle", [39 / 256, 174 / 256, 96 / 256, 1], "Correct"
)}
        values = [[
'', *row] for row in excelWorkbook[excelSheet].values]
       
for i in range(len(values)):
           values[i][
1] = icons.get(values[i][1], values[i][1])  # replace correct or wrong with icon tuple

       
data_tables = MDDataTable(
           
size_hint=(1, 1),
           
use_pagination=True,
           
rows_num=20,
           
column_data=[
                (
'', dp(0)),    # added a leading row number
               
("Result", dp(30)),
                (
"Answers", dp(80)),
            ],
           
row_data=values
        )
        layout.add_widget(data_tables)
       
return layout

Example().run()

 

 

From: Ian Rajkumar
Sent: Tuesday, July 13, 2021 12:04 PM
To: Kivy users support
Subject: Re: [kivy-users] Display Icons in MdDataTable when pulling from excel

 

Thank you ElliotG, this works amazingly. I would like to remove the column that you added since the number of the question is in the same columns as the icons, only two columns are needed. I am still fairly new to coding, when I try editing the code, it breaks it. 

 

On Tuesday, July 13, 2021 at 2:19:26 PM UTC-3 ElliotG wrote:

Ian Rajkumar

unread,
Jul 13, 2021, 3:58:01 PM7/13/21
to Kivy users support
It had not occurred to me that I could set the column to zero. Great idea. The last thing I would like to accomplish is to bold the text and add a light gray background to the row with the number. This way the student can quickly identify which were the questions. Is that possible?

Elliot Garbus

unread,
Jul 13, 2021, 5:39:24 PM7/13/21
to kivy-...@googlegroups.com
You can set text to bold or change the text color or size  using the markup described in the kivy Label.


The data table does not appear to support changing color by row. 

Sent from my iPhone

On Jul 13, 2021, at 12:58 PM, Ian Rajkumar <ianra...@gmail.com> wrote:

It had not occurred to me that I could set the column to zero. Great idea. The last thing I would like to accomplish is to bold the text and add a light gray background to the row with the number. This way the student can quickly identify which were the questions. Is that possible?
Reply all
Reply to author
Forward
0 new messages