渐变色Panel构造方法的重写

发布时间 2023-06-01 13:47:50作者: No九五二七9527
#此类用于设置渐变色panel
class MyPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, wx.ID_ANY)
        self.SetBackgroundStyle(wx.BG_STYLE_PAINT)
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Bind(wx.EVT_SIZE, self.OnSize)       
        
    def OnPaint(self, event):
        dc = wx.AutoBufferedPaintDC(self)
        gc = wx.GraphicsContext.Create(dc)
        width, height = self.GetSize()        
        #CreateLinearGradientBrush(x1, y1, x2, y2, colour1, colour2)
        '''
        参数要求:
        x1: 渐变的起始点横坐标
        y1: 渐变的起始点纵坐标
        x2: 渐变的结束点横坐标
        y2: 渐变的结束点纵坐标
        colour1: 渐变起始点的颜色(wx.Colour对象)
        colour2: 渐变结束点的颜色(wx.Colour对象)
        '''
        gradient = gc.CreateLinearGradientBrush(0, 0, 0, height,
                 ThemeColorBox.TEm_panel15_MyPanelBegin,
                 ThemeColorBox.TEm_panel15_MyPanelEnd)
        
        gc.SetBrush(gradient)
        gc.DrawRectangle(0, 0, width, height)
        
    def OnSize(self, event):  
        self.Refresh()  # 刷新窗口显示
        self.Layout()  # 调用Layout()重新布局,否则内部控件可能可能因为尺寸变动挤成一坨