Search code examples
wxpython

Set grid cell background transparency


How can I set the background transparency of a grid cell. Or is it impossible?

import wx
import wx.grid


class Frame(wx.Frame):

    def __init__(self, parent):
        super().__init__(parent)

        grid = wx.grid.Grid(self)
        grid.CreateGrid(3, 3)

        grid.SetCellBackgroundColour(0, 0, wx.Colour(255, 0, 0, 0))
        grid.SetCellBackgroundColour(1, 0, wx.Colour(255, 0, 0, 127))
        grid.SetCellBackgroundColour(2, 0, wx.Colour(255, 0, 0, 255))


if __name__ == '__main__':
    app = wx.App()
    frame = Frame(None)
    frame.Show()
    app.MainLoop()

enter image description here

WxPython 4.0.7.post2


Solution

  • import wx
    import wx.grid
    
    
    def getRGBA(rgb, alpha):
        """Create RGBA.
    
        Y = 255 - P * (255 - X), X - RGB item, P - aplha (0...1)
    
        :param tuple rgb:
        :param float alpha:
        :return: New RGB
        :rtype: tuple
        """
        return tuple(round(255 - alpha * (255 - v)) for v in rgb)
    
    
    class Frame(wx.Frame):
    
        def __init__(self, parent):
            super().__init__(parent)
    
            grid = wx.grid.Grid(self)
            grid.CreateGrid(3, 3)
    
            grid.SetCellBackgroundColour(0, 0, wx.Colour(255, 0, 0, 0))
            grid.SetCellBackgroundColour(1, 0, wx.Colour((255, 0, 0, 127)))
            grid.SetCellBackgroundColour(2, 0, wx.Colour(255, 0, 0, 255))
    
            grid.SetCellBackgroundColour(0, 1, wx.Colour(getRGBA((255, 0, 0), 1)))
            grid.SetCellBackgroundColour(1, 1, wx.Colour(getRGBA((255, 0, 0), 0.5)))
            grid.SetCellBackgroundColour(2, 1, wx.Colour(getRGBA((255, 0, 0), 0)))
    
    
    if __name__ == '__main__':
        app = wx.App()
        frame = Frame(None)
        frame.Show()
        app.MainLoop()
    

    enter image description here