在 wxPython 中设置 ThumbnailCtrl 中的背景颜色

Setting the background color in ThumbnailCtrl in wxPython

我正在尝试使用 ThumbnailCtrl,我想更改背景颜色。

我原以为这段代码可以工作

self.thumbnail_ctrl.SetBackgroundColour('red')

不幸的是,它不起作用,我在下面的最小示例中结束了破解解决方案显示,其中我使用内部字段 _scrolled 设置了它的背景颜色。

我还是个初学者,请问是不是我上面预期的call show哪里做错了

#!/usr/bin/env python

import wx
import wx.lib.agw.thumbnailctrl as TC

class MyPanel(wx.Panel):

    def __init__(self, parent):
        super().__init__(parent=parent)
        
        self.thumbnail_ctrl = TC.ThumbnailCtrl(parent=self, imagehandler=TC.NativeImageHandler)
        self.thumbnail_ctrl.ShowFileNames(False)                        # do not show the filename under the thumbs
        self.thumbnail_ctrl.SetThumbOutline(TC.THUMB_OUTLINE_RECT)      # outline the rect

        self.main_sizer = wx.BoxSizer(wx.VERTICAL)
        self.main_sizer.Add(window=self.thumbnail_ctrl, proportion=1, flag=wx.ALL|wx.EXPAND, border=0)
        self.SetSizer(self.main_sizer)

        # Hack to set the background color of self.thumbnail_ctrl
#       self.thumbnail_ctrl.SetBackgroundColour('red')                # Calling this does not works
        self.thumbnail_ctrl._scrolled.SetBackgroundColour('red')      # Calling this the background color is set to red
        self.Refresh()

class MyFrame(wx.Frame):

    def __init__(self):
        super().__init__(parent=None, title='minimal set background example')

        self.panel = MyPanel(parent=self)
        self.Show()

if __name__ == '__main__':
    app = wx.App(redirect=True)
    frame = MyFrame()
    app.MainLoop()

据我所知,我没有以任何方式设置 Thumbnailctrl 的背景颜色 - 我猜我只是忘记了。所以你的解决方案,虽然当然是 hack,但它是一个完全有效的解决方案。您可能想打开一个问题或向 wxPython github 提交一个 PR,以便可以将适当的 SetBackgroundColour 添加到源代码中的 class。