wx.grid表格样式重绘(超链接样式)

发布时间 2023-08-16 09:41:00作者: No九五二七9527

首先声明,这里达不到超链接的效果,仅仅是显示出来像超链接的样式而已!!!

如果需要实现超链接,就尝试绑定一下单元格点击事件。

 

重绘表格类构建

class BlueUnderlineRenderer(wx.grid.PyGridCellRenderer):
    def __init__(self):
        gridlib.GridCellRenderer.__init__(self)
        
    def Draw(self, grid, attr, dc, rect, row, col, isSelected):
        # Clear the rectangle with the background color
        #清除带有背景色的矩形
        dc.SetBrush(wx.Brush("#e4f9f5", wx.BRUSHSTYLE_SOLID))
        dc.SetPen(wx.TRANSPARENT_PEN)
        dc.DrawRectangle(rect)

        text = grid.GetCellValue(row, col)

        # Calculate the text size
        #计算文本大小
        dc.SetFont(wx.Font(10, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, underline=True))
        text_width, text_height = dc.GetTextExtent(text)

        # Calculate the centered position
        #计算中心位置
        x_centered = rect.x + (rect.width - text_width) // 2
        y_centered = rect.y + (rect.height - text_height) // 2

        # Draw the text in blue and centered
        #以蓝色绘制文本并居中
        dc.SetTextForeground(wx.BLUE)
        dc.DrawText(text, x_centered, y_centered)

    def GetBestSize(self, grid, attr, dc, row, col):
        return wx.Size(100, 30)

  

使用方法:

#构建表格
self.grid12 = wx.grid.Grid(self.baseinfopanel)
self.grid12.CreateGrid(60, 7)  # 创建一个 初始60行 7 列的表格
#设置列表头
self.grid12.SetColLabelValue(0, "产品编号")
#设置列宽
self.grid12.SetColSize(0,180)       
……




#设置单元格内容
self.grid12.SetCellValue(RowCount, ColCount, str("……")]))

#设置调用BlueUnderlineRenderer()类的实例,作为参数渲染单元格样式
renderer = BlueUnderlineRenderer()
self.grid12.SetCellRenderer(RowCount,ColCount, renderer)  # 设置特定单元格的渲染器