Acorn
Acorn

Reputation: 50497

How to draw a transparent section within an opaque area

How would you go about cutting a transparent hole into the background of a wxpython window?

Would I have to draw the non transparent area manually, leaving a hole, instead of being able to use a background colour?

Upvotes: 3

Views: 527

Answers (1)

FogleBird
FogleBird

Reputation: 76792

Adapting my answer to your previous question, though I'm not sure if this meets your needs 100%. Let me know?

Basically we're leaving the previous screen contents intact by not erasing the background. Then we handle the paint events and only draw on certain portions of the screen.

You could switch back to using SetTransparent if you need the drawn portions to be translucent and not opaque.

import wx

class Frame(wx.Frame):
    def __init__(self):
        super(Frame, self).__init__(None)
        self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
        self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
        self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
        self.Bind(wx.EVT_PAINT, self.OnPaint)
    def OnEraseBackground(self, event):
        pass # do nothing
    def OnLeftDown(self, event):
        print event.GetPosition()
    def OnKeyDown(self, event):
        if event.GetKeyCode() == wx.WXK_ESCAPE:
            self.Close()
        else:
            event.Skip()
    def OnPaint(self, event):
        w, h = self.GetSize()
        dc = wx.PaintDC(self)
        region = wx.RegionFromPoints([(0, 0), (w, 0), (w, h), (0, h)])
        box = wx.RegionFromPoints([(100, 100), (500, 100), (500, 500), (100, 500)])
        region.SubtractRegion(box)
        dc.SetClippingRegionAsRegion(region)
        dc.DrawRectangle(0, 0, w, h)

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = Frame()
    frame.ShowFullScreen(True)
    app.MainLoop()

Upvotes: 2

Related Questions