Search code examples
pythonwxpython

Setting the background color in ThumbnailCtrl in wxPython


I'm trying to use the ThumbnailCtrl and I would like to change the background color.

I would have expected this code to work

self.thumbnail_ctrl.SetBackgroundColour('red')

Unfortunately it does not works and I ended hacking a solution show in the following minimal example in which I used the internal field _scrolled setting the background color on it.

I'm still a beginner and would like to ask if I did something wrong with the expected call show above.

#!/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()

Solution

  • As far as I remember I haven’t put in any way to set the background color of Thumbnailctrl - I simply forgot I guess. So your solution, while of course a hack, it’s a perfectly valid one. You may want to open an issue or submit a PR to wxPython github so a proper SetBackgroundColour can be added to the class in the source.