How to draw a transparent frame in wxpython

2020-04-13 04:03发布

问题:

UPDATE I completely rephrased the question

I want to create a window with round corners, that's also semi-transparent.

I managed to make a frame semi-transparent by calling the SetTransparent( alpha ) method, however, I still don't know how to make the window have no background.

I tried to get the device context (dc) of the window and set the background brush to wx.TRANSPARENT_BRUSH but that doesn't seem to have any effect.

I'm seeking a kind of effect similar to this for instance, but I want to do it without using a bitmap (i.e. without any external media file).

How do I set the background to nothing?

Update:

it seems what I want is called "shaped window". I'm looking a bit further into that, but still the question is: how can I make one without using any external media file (i.e. purely in code).

回答1:

It's surprising that no one gave an answer ..

Yes, as FogleBird said, it's done with SetShape, but the question is how to get a shape for a ronded rectangle without using an image file.

I found one way to do that, which is to create an empty bitmap and draw on it a rounded rectangle using a memory dc

def GetRoundBitmap( w, h, radius ):
    maskColor = wx.Color(0,0,0)
    shownColor = wx.Color(5,5,5)
    b = wx.EmptyBitmap(w,h)
    dc = wx.MemoryDC()
    dc.SelectObject(b)    
    dc.SetBrush(wx.Brush(maskColor))
    dc.DrawRectangle(0,0,w,h)
    dc.SetBrush(wx.Brush(shownColor))
    dc.DrawRoundedRectangle(0,0,w,h,radius)
    dc.SelectObject(wx.NullBitmap)
    b.SetMaskColour(maskColor)
    return b

def GetRoundShape( w, h, r ):
    return wx.RegionFromBitmap( GetRoundBitmap(w,h,r) )

One problem I found is that the shape is quiet aliased. Not exactly a nice/fancy window, but better than nothing.



回答2:

You want the SetShape method.

http://docs.wxwidgets.org/stable/wx_wxtoplevelwindow.html#wxtoplevelwindowsetshape



回答3:

Check out wxWidgets\samples\shaped (in wxWidget), it shows a start shaped window.