【问题标题】:Fade in/out the background color of wxpython grid cell淡入/淡出 wxpython 网格单元格的背景颜色
【发布时间】:2011-04-28 11:17:43
【问题描述】:

我有一个 wxpython 网格,我正在更改单元格的背景颜色以显示它发生了什么事。

我想淡入/淡出颜色变化(如浏览器中的 JavaScript)以获得更平滑的外观。这可以吗?

现在,我只是更改背景颜色,然后在 1.5 秒间隔后将其更改回来。

def do_stuf(self):
    # ... stuff ...
    wx.CallAfter(self.HighlightCell, row, col)

def HighlightCell(self, row, col):
    self.grid.Table.highlight = (row, col)
    self.grid.ForceRefresh()
    wx.CallLater(1500, self.ClearCellHighlight)

def ClearCellHighlight(self):
    self.grid.Table.highlight = None
    self.grid.ForceRefresh()

然后在虚拟表中,我检查单元格是否需要突出显示:

def GetAttr(self, row, col, kind):
    """
    Use this callback to set the cell's background color
    """
    attr = wx.grid.GridCellAttr()
    if (row, col) == self.highlight:
        attr.SetBackgroundColour("green")
    elif row % 2:
        attr.SetBackgroundColour("white")
    else:
        attr.SetBackgroundColour("#e7ffff")

    return attr

或者,是否有另一种漂亮的方式来指示单元格的内容已更改?

【问题讨论】:

    标签: python wxpython grid


    【解决方案1】:

    这是我不久前所做的,目的是获得一个 ListCtrl,其中的项目在删除时会淡出。将代码保存到 fade.py 并运行它以查看演示。让它适应网格应该不会太难。

    import wx
    
    class FadeMixin(object):
        ''' FadeMixin provides one public method: DeleteItem. It is meant to
        be mixed in with a ListCtrl to 'fade out' items before they are
        really deleted. Mixin like this:
    
        Assumption: the background colour of the control is wx.WHITE
    
        class MyListCtrl(FadeMixin, wx.ListCtrl):
            ...
        '''
        def __init__(self, *args, **kwargs):
            self.__bgColour = wx.WHITE
            super(FadeMixin, self).__init__(*args, **kwargs)
    
        def DeleteItem(self, index, fadeStep=10, fadeSpeed=50):
            if self.IsEnabled():
                self.__startDeleteItem(index)
            fgColour, bgColour, transparentColour = self.__getColours(index)
            if fgColour == bgColour == transparentColour:
                self.__finishDeleteItem(index)
            else:
                for colour, setColour in [(fgColour, self.SetItemTextColour), 
                                          (bgColour, self.SetItemBackgroundColour)]:
                    fadedColour = self.__fadeColour(colour, transparentColour, 
                                                    fadeStep)
                    setColour(index, fadedColour)
                wx.FutureCall(50, self.DeleteItem, index, fadeStep, fadeSpeed)
    
        def SetBackgroundColour(self, colour):
            self.__bgColour = colour
            super(FadeMixin, self).SetBackgroundColour(colour)
    
        def GetBackgroundColour(self):
            return self.__bgColour
    
        def __startDeleteItem(self, index):
            # Prevent user input during deletion. Things could get messy if
            # the user deletes another item when we're still busy fading out the 
            # first one:
            self.Disable()
            # Unselect the item that is to be deleted to make the fading visible:
            currentState = self.GetItemState(index, wx.LIST_STATE_SELECTED)
            self.SetItemState(index, ~currentState, wx.LIST_STATE_SELECTED)
    
        def __finishDeleteItem(self, index):
            super(FadeMixin, self).DeleteItem(index)
            self.Enable()
    
        def __getColours(self, index):
            fgColour = self.GetItemTextColour(index)
            bgColour = self.GetItemBackgroundColour(index)
            transparentColour = self.GetBackgroundColour()
            if not bgColour:
                bgColour = transparentColour
            return fgColour, bgColour, transparentColour
    
        def __fadeColour(self, colour, transparentColour, fadeStep):
            newColour = []
            for GetIntensity in wx.Colour.Red, wx.Colour.Green, wx.Colour.Blue:
                currentIntensity = GetIntensity(colour) 
                transparentIntensity = GetIntensity(transparentColour)
                if currentIntensity < transparentIntensity:
                    newIntensity = min(transparentIntensity,
                                       currentIntensity + fadeStep)
                elif currentIntensity > transparentIntensity:
                    newIntensity = max(transparentIntensity, 
                                    currentIntensity - fadeStep)
                else:
                    newIntensity = transparentIntensity
                newColour.append(newIntensity)
            return wx.Colour(*newColour)
    
    
    class ListCtrl(FadeMixin, wx.ListCtrl):
        pass
    
    
    class Frame(wx.Frame):
        def __init__(self, *args, **kwargs):
            super(Frame, self).__init__(*args, **kwargs)
            self.list = ListCtrl(self, style=wx.LC_REPORT)
            self.list.InsertColumn(0, 'Column 0')
            self.list.InsertColumn(1, 'Column 1')
            self.fillList()
            self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.onSelected)
            self.Bind(wx.EVT_LIST_DELETE_ITEM, self.onDeleted)
    
        def onSelected(self, event):
            self.list.DeleteItem(event.GetIndex())
    
        def onDeleted(self, event):
            if self.list.GetItemCount() == 1:
                wx.CallAfter(self.fillList, False)
    
        def fillList(self, firstTime=True):
            for row in range(10):
                self.list.InsertStringItem(row, 'Item %d, Column 0'%row)
                self.list.SetStringItem(row, 1, 'Item %d, Column 1'%row)
            self.list.SetItemBackgroundColour(1, wx.BLUE)
            self.list.SetItemTextColour(2, wx.BLUE)
            self.list.SetItemBackgroundColour(3, wx.GREEN)
            self.list.SetItemTextColour(4, wx.GREEN)
            self.list.SetItemBackgroundColour(5, wx.RED)
            self.list.SetItemTextColour(6, wx.RED)
            self.list.SetItemBackgroundColour(7, wx.BLACK)
            self.list.SetItemTextColour(7, wx.WHITE)
            self.list.SetItemBackgroundColour(8, wx.WHITE)
            self.list.SetItemTextColour(8, wx.BLACK)
            if not firstTime:
                self.list.SetBackgroundColour(wx.BLUE)
    
    
    app = wx.App(False)
    frame = Frame(None, title='Select an item to fade it out')
    frame.Show()
    app.MainLoop()
    

    【讨论】:

      【解决方案2】:

      据我所知,您只能在框架小部件上设置透明度并查看其对所有子组件的影响。我不记得为什么在单个小部件上设置透明度不起作用。无论如何,模拟这种情况的一种体面方法是使用 wx.Timer 循环遍历一种颜色的不同阴影列表,然后在迭代完成后将其重置为正常颜色。这应该可以很好地模拟外观。

      【讨论】:

        猜你喜欢
        • 2012-05-31
        • 1970-01-01
        • 1970-01-01
        • 2015-04-03
        • 1970-01-01
        • 2015-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多