FileChooser的字体颜色

时间:2016-10-17 15:37:08

标签: python kivy

我想知道如何更改FileChooserListView和FileChooserIconView的字体颜色(文本颜色)。

我可以更改背景颜色(白色),我想将字体颜色更改为黑色。

我怎么能这样做?

3 个答案:

答案 0 :(得分:5)

Kivy小部件的默认样式位于kivy/data/style.kv文件中。您可以复制其条目并根据自己的喜好进行更改。例如:

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()

答案 1 :(得分:0)

在回答this问题时,我想出了不同的方式来设计Kivy文件选择器。可以使用绑定到on_entry_addedon_subentry_to_entry事件的自定义函数来更改添加的文件条目,而不必全局覆盖样式。这种方法产生的代码更加清晰:

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()

答案 2 :(得分:0)

Nykakin在2019年的回应似乎是最好的方法,但是,我不得不对其稍加修改以对我有用。我不确定这是因为我使用了FileChooserIconView,还是因为kivy自从创建示例以来就做了一些重构。

对于其他正在苦苦挣扎的人,这些是我对Kykakin的示例代码所做的更改:

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

 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`