3

I would like to know how to change the font-color (text-color) of both the FileChooserListView and the FileChooserIconView.

I could change the background color (to white), and I would like to change the font color to black.

How could I do that?

picibucor
  • 753
  • 1
  • 8
  • 25

3 Answers3

5

Default style of Kivy widget is placed in kivy/data/style.kv file. You can copy its entries and change it to your liking. For example:

from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.app import App
from kivy.lang import Builder

Builder.load_string('''

<FileChooserListView>:
    # --------------------
    # ADD BACKGROUND COLOR
    # --------------------
    canvas.before:
        Color:
            rgb: 1, 1, 1
        Rectangle:
            pos: self.pos
            size: self.size
    layout: layout
    FileChooserListLayout:
        id: layout
        controller: root

[FileListEntry@FloatLayout+TreeViewNode]:
    locked: False
    entries: []
    path: ctx.path
    # FIXME: is_selected is actually a read_only treeview property. In this
    # case, however, we're doing this because treeview only has single-selection
    # hardcoded in it. The fix to this would be to update treeview to allow
    # multiple selection.
    is_selected: self.path in ctx.controller().selection

    orientation: 'horizontal'
    size_hint_y: None
    height: '48dp' if dp(1) > 1 else '24dp'
    # Don't allow expansion of the ../ node
    is_leaf: not ctx.isdir or ctx.name.endswith('..' + ctx.sep) or self.locked
    on_touch_down: self.collide_point(*args[1].pos) and ctx.controller().entry_touched(self, args[1])
    on_touch_up: self.collide_point(*args[1].pos) and ctx.controller().entry_released(self, args[1])
    BoxLayout:
        pos: root.pos
        size_hint_x: None
        width: root.width - dp(10)
        Label:
            # --------------
            # CHANGE FONT COLOR
            # --------------
            color: 0, 0, 0, 1
            id: filename
            text_size: self.width, None
            halign: 'left'
            shorten: True
            text: ctx.name
        Label:
            # --------------
            # CHANGE FONT COLOR
            # --------------
            color: 0, 0, 0, 1
            text_size: self.width, None
            size_hint_x: None
            halign: 'right'
            text: '{}'.format(ctx.get_nice_size())


<MyWidget>:
    FileChooserListView
''')

class MyWidget(BoxLayout):
    pass

class TestApp(App):
    def build(self):
        return MyWidget()


if __name__ == '__main__':
    TestApp().run()
Nykakin
  • 8,657
  • 2
  • 29
  • 42
1

While answering this question, I've come up with different approach to styling Kivy filechooser. Instead of overriding style globally one can alter added file entries with a custom function bind to on_entry_added and on_subentry_to_entry events. This approach results with much clearer code:

from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout


Builder.load_string("""
<MyWidget>:
    FileChooserListView
        id: filechooser
""")

class MyWidget(BoxLayout):
    def __init__(self, *args):
        Clock.schedule_once(self.init_widget, 0)
        return super().__init__(*args)

    def init_widget(self, *args):
        fc = self.ids['filechooser']
        fc.bind(on_entry_added=self.update_file_list_entry)
        fc.bind(on_subentry_to_entry=self.update_file_list_entry)

    def update_file_list_entry(self, file_chooser, file_list_entry, *args):
        file_list_entry.ids['filename'].color = (0.0, 1.0, 1.0, 1.0)


class MyApp(App):
    def build(self):
        return MyWidget()


if __name__ == '__main__':
    MyApp().run()
Nykakin
  • 8,657
  • 2
  • 29
  • 42
0

Nykakin's response in 2019 seems to be the best approach, however, I had to modify it slightly to work for me. I am not sure if it is because I used FileChooserIconView, or if kivy has done some refactoring since they made their example.

For anybody else who is struggling, these are the changes I made to Kykakin's example code:

def __init__(self, **args):
        Clock.schedule_once(self.init_widget, 0)
        return super(Loc, self).__init__(**args) # where Loc is the class I am working in

and

 def update_file_list_entry(self, file_chooser, file_list_entry, *args):
        file_list_entry.children[0].color = (0.0, 0.0, 0.0, 1.0)  # File Names
        file_list_entry.children[1].color = (0.0, 0.0, 0.0, 1.0)  # Dir Names`
mdoc-2011
  • 2,747
  • 4
  • 21
  • 43