如何做 dregradê在 wx.Panel 背景上?

发布于 2024-08-23 07:30:11 字数 74 浏览 6 评论 0原文

我想将 wx.Panel 的背景(SetBackgroundColour)着色为蓝色到黑色的渐变。

我怎样才能做到呢?

I want to color the background (SetBackgroundColour) of a wx.Panel with a blue to black degradê.

How can I make it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相思碎 2024-08-30 07:30:11

改编自 DaniWeb

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent=None, title=None):
        wx.Frame.__init__(self, parent, wx.ID_ANY, title)
        self.panel = wx.Panel(self, size=(350, 450))
        # this sets up the painting canvas
        self.panel.Bind(wx.EVT_PAINT, self.on_paint)
        # set frame size to fit panel
        self.Fit()

    def on_paint(self, event):
        # establish the painting canvas
        dc = wx.PaintDC(self.panel)
        x = 0
        y = 0
        w, h = self.GetSize()
        dc.GradientFillLinear((x, y, w, h), 'blue', 'black')


app = wx.App(0)
MyFrame(title='Gradient Test').Show()
app.MainLoop()

以及使用 NumPy 生成渐变位图的另一种方法(来自 wxpython.org):

import numpy as np

def GetBitmap( self, width=640, height=480, leftColour=(255,128,0), rightColour=(64,0,255) ):
        ## Create a horizontal gradient
        array = np.zeros( (height, width, 3),'uint8')
        # alpha is a one dimensional array with a linear gradient from 0.0 to 1.0
        alpha = np.linspace( 0., 1., width )
        # This uses alpha to linearly interpolate between leftColour and rightColour
        colourGradient = np.outer(alpha, leftColour) + np.outer((1.-alpha), rightColour)
        # NumPy's broadcasting rules will assign colourGradient to every row of the destination array
        array[:,:,:] = colourGradient
        image = wx.EmptyImage(width,height)
        image.SetData( array.tostring())
        return image.ConvertToBitmap()# wx.BitmapFromImage(image)

Adapted from DaniWeb:

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent=None, title=None):
        wx.Frame.__init__(self, parent, wx.ID_ANY, title)
        self.panel = wx.Panel(self, size=(350, 450))
        # this sets up the painting canvas
        self.panel.Bind(wx.EVT_PAINT, self.on_paint)
        # set frame size to fit panel
        self.Fit()

    def on_paint(self, event):
        # establish the painting canvas
        dc = wx.PaintDC(self.panel)
        x = 0
        y = 0
        w, h = self.GetSize()
        dc.GradientFillLinear((x, y, w, h), 'blue', 'black')


app = wx.App(0)
MyFrame(title='Gradient Test').Show()
app.MainLoop()

And an alternative way to generate a gradient bitmap, using NumPy (from wxpython.org):

import numpy as np

def GetBitmap( self, width=640, height=480, leftColour=(255,128,0), rightColour=(64,0,255) ):
        ## Create a horizontal gradient
        array = np.zeros( (height, width, 3),'uint8')
        # alpha is a one dimensional array with a linear gradient from 0.0 to 1.0
        alpha = np.linspace( 0., 1., width )
        # This uses alpha to linearly interpolate between leftColour and rightColour
        colourGradient = np.outer(alpha, leftColour) + np.outer((1.-alpha), rightColour)
        # NumPy's broadcasting rules will assign colourGradient to every row of the destination array
        array[:,:,:] = colourGradient
        image = wx.EmptyImage(width,height)
        image.SetData( array.tostring())
        return image.ConvertToBitmap()# wx.BitmapFromImage(image)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文