gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:21:11 26 4
gpt4 key购买 nike

我想为 wx.Panel 的背景着色(SetBackgroundColour),颜色为蓝色到黑色。

我怎样才能做到?

最佳答案

改编自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)

关于python - 如何在 wx.Panel 背景上做 dregradê?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2375158/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com