更改Kivy FileChooserIconView的图标

时间:2019-09-26 05:24:17

标签: kivy kivy-language

我正在寻找一种更改FileChooserIconView的文件图标并以编程方式设置它的方法。我正在查看Kivy的filechooser.py源代码,但找不到设置图标的位置。

当前,FileChooserIconView的默认背景颜色为黑色,并且当前图标在该背景下运行良好。我需要将应用程序的背景更改为白色,并且当前图标在白色背景下看起来不太好,并且我还有一个特定的文件图标需要用于白色背景。

1 个答案:

答案 0 :(得分:1)

文件图标在style.kv安装的Kivy文件中定义。在FileChooserIconViewFileChooserIconLayout类中,它被引用为:

_ENTRY_TEMPLATE = 'FileIconEntry'

您可以使用以下方法重新定义该模板:

Builder.load_string('''
[FileIconEntry@Widget]:
    locked: False
    path: ctx.path
    selected: self.path in ctx.controller().selection
    size_hint: None, None

    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])
    size: '100dp', '100dp'

    canvas:
        Color:
            rgba: 1, 1, 1, 1 if self.selected else 0
        BorderImage:
            border: 8, 8, 8, 8
            pos: root.pos
            size: root.size
            source: 'atlas://data/images/defaulttheme/filechooser_selected'

    Image:
        size: '48dp', '48dp'
        source: 'atlas://data/images/defaulttheme/filechooser_%s' % ('folder' if ctx.isdir else 'file')
        pos: root.x + dp(24), root.y + dp(40)
    Label:
        text: ctx.name
        text_size: (root.width, self.height)
        halign: 'center'
        shorten: True
        size: '100dp', '16dp'
        pos: root.x, root.y + dp(16)

    Label:
        text: '{}'.format(ctx.get_nice_size())
        font_size: '11sp'
        color: .8, .8, .8, 1
        size: '100dp', '16sp'
        pos: root.pos
        halign: 'center'
''')

上面的代码仅复制了style.kv中的模板定义,但是您可以对上面的模板进行任何更改,这些更改将影响FileChooserIconViewImage是实际图标。