wxpython-在透明窗口上绘图

时间:2018-05-11 21:30:19

标签: python wxpython

我正在编写需要选择屏幕区域的应用程序。我需要将光标更改为十字形,然后在用户选择上绘制一个矩形 - 这是成功的(尽管错误不断出现,我正在使用“已弃用的'函数 - 避风港&#39 ; t想到如何解决这个问题)。

主要问题 - 当我将窗口更改为完全透明(通过设置self.SetTransparent(0))时,选择矩形也变得透明,因此我无法使窗口完全透明并仍然看到选择。

以下是代码:

import wx

class SelectableFrame(wx.Frame):

    c1 = None
    c2 = None

    def __init__(self, parent=None, id=-1, title=""):
        wx.Frame.__init__(self, parent, id, title, size=wx.DisplaySize())

        self.panel = wx.Panel(self, size=self.GetSize())
        self.panel.Bind(wx.EVT_MOTION, self.OnMouseMove)
        self.panel.Bind(wx.EVT_LEFT_DOWN, self.OnMouseDown)
        self.panel.Bind(wx.EVT_LEFT_UP, self.OnMouseUp)
        self.panel.Bind(wx.EVT_PAINT, self.OnPaint)
        self.SetCursor(wx.StockCursor(wx.CURSOR_CROSS))
        self.SetTransparent(10)

    def OnMouseMove(self, event):
        if event.Dragging() and event.LeftIsDown():
            self.c2 = event.GetPosition()
            self.Refresh()

    def OnMouseDown(self, event):
        self.c1 = event.GetPosition()

    def OnMouseUp(self, event):
        self.SetCursor(wx.StockCursor(wx.CURSOR_ARROW))

    def OnPaint(self, event):
        if self.c1 is None or self.c2 is None: return

        dc = wx.PaintDC(self.panel)
        dc.SetPen(wx.Pen('red', 2)) 
        dc.SetBrush(wx.TRANSPARENT_BRUSH) 
        dc.DrawRectangle(self.c1.x, self.c1.y, self.c2.x - self.c1.x, self.c2.y - self.c1.y)

    def PrintPosition(self, pos):
        return str(pos.x) + " " + str(pos.y)

class MyApp(wx.App):

    def OnInit(self):
        frame = SelectableFrame()
        frame.Show(True)
        self.SetTopWindow(frame)

        return True

app = MyApp(0)
app.MainLoop()

如何使窗口透明但保持选择可见?

0 个答案:

没有答案