Using text markup in ListView items

81 views
Skip to first unread message

Flavio Coelho

unread,
Jul 3, 2016, 5:15:11 PM7/3/16
to Kivy users support
Hi I want to colorize the strings in my ListView.

I have defined it in KV as follows:

ListView:
id: plist
size_hint: .99, .9
item_strings: []

I then update the item_strings variable from python code.

The problem is that markup does not work in those strings. Do I have to use some kind of Adapter  to be able to eneble markup iin my strings?

something like this?

ListView:
        adapter:
            sla.SimpleListAdapter(
            data=["[color=#ff3333]Item #{0}[/color]".format(i) for i in range(100)],
            cls=label.Label)

but how do I enable markup for such cases?

ZenCODE

unread,
Jul 4, 2016, 2:17:04 AM7/4/16
to Kivy users support
Hi

Try sub-classing the label, set it's markup property to True and then use that as the cls.

class MyLabel(Label):
   
def __init__(self, **kwargs):
       
super(MyLabel, self).__init__(**kwargs)
       
self.markup = True



Then

ListView:
        adapter
:
            sla
.SimpleListAdapter(
            data
=["[color=#ff3333]Item #{0}[/color]".format(i) for i in range(100)],

            cls
=MyLabel)





Flavio Coelho

unread,
Jul 4, 2016, 9:04:19 AM7/4/16
to kivy-...@googlegroups.com
Thanks zencode,

I am trying to do as You suggest, but the KV builder is complaining that `MyLabel` is not defined, even though I have defined the class MyLabel in my main.py. Any Idea of what I may be doing wrong?

     113:                adapter:
 >>  114:                    sla.SimpleListAdapter(
     115:                    data=["[color=#ff3333]Item #{0}[/color]".format(i) for i in range(100)],
     116:                    cls=MyLabel)
 ...
 NameError: name 'MyLabel' is not defined
   File "/usr/lib/python3/dist-packages/kivy/lang/builder.py", line 249, in create_handler
     return eval(value, idmap), bound_list
   File "/home/fccoelho/Documentos/Projects_software/BFXAssistant/main.kv", line 116, in <module>
     cls=MyLabel)


--
You received this message because you are subscribed to a topic in the Google Groups "Kivy users support" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/kivy-users/3sa2XWgmtCE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to kivy-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Flávio Codeço Coelho
================
+55(21) 3799-5551
Professor
Escola de Matemática Aplicada 
Fundação Getulio Vargas
Praia de Botafogo, 190 sala 312
Rio de Janeiro - RJ
22250-900
Brasil

Antonio Cuni

unread,
Jul 4, 2016, 9:33:21 AM7/4/16
to kivy-...@googlegroups.com
On Mon, Jul 4, 2016 at 3:03 PM, Flavio Coelho <fcco...@gmail.com> wrote:
> Thanks zencode,
>
> I am trying to do as You suggest, but the KV builder is complaining that
> `MyLabel` is not defined, even though I have defined the class MyLabel in my
> main.py. Any Idea of what I may be doing wrong?

you should declare MyLabel also in the kv file; something like this:

# kv file
MyLabel

ListView:
adapter:
sla.SimpleListAdapter(data=["[color=#ff3333]Item
#{0}[/color]".format(i) for i in range(100)], cls=MyLabel)


But note that in this case, where the only scope of MyLabel is to set
the value of an attribute, it is not necessary to define a new class
in Python; you can do it directly from the kv file:

#:import Factory kivy.factory.Factory
#:import SimpleListAdapter kivy.adapters.simplelistadapter.SimpleListAdapter

<MyLabel@Label>:
markup: True

ListView:
adapter: SimpleListAdapter(data=["[color=#ff3333]Item
#{0}[/color]".format(i) for i in range(100)], cls=Factory.MyLabel)


Note that when you define a new class in kv using the
<ClassName@SuperClass> syntax, it automatically becomes available in
the Factory object.

ciao,
Anto

Flavio Coelho

unread,
Jul 4, 2016, 9:53:46 AM7/4/16
to kivy-...@googlegroups.com
Instead of declaring, what I did to solve it was to import it explicitly in the top of the kv file

#:import main main
and then declared the adapter as follows:

ListView:
id: plist
size_hint: .99, .9
    #item_strings: []
adapter:
sla.SimpleListAdapter(

data=["[color=#ff3333]Item #{0}[/color]".format(i) for i in range(100)],
        cls=main.MyLabel)




--
You received this message because you are subscribed to a topic in the Google Groups "Kivy users support" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/kivy-users/3sa2XWgmtCE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to kivy-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages