【问题标题】:font-color of FileChooserFileChooser 的字体颜色
【发布时间】:2017-02-26 16:15:18
【问题描述】:

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

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

我该怎么做?

【问题讨论】:

    标签: python kivy


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      在回答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()
      

      【讨论】:

        【解决方案3】:

        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`
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-10-27
          • 2015-11-05
          • 2011-07-23
          • 2012-06-21
          • 1970-01-01
          • 2014-03-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多