Python screen drawing

I am encoding an application that should select an area of ​​the screen. I need to change the cursor to a cross, and then draw a rectangle of the user's choice. The first thing I was looking for was how to manipulate the cursor and I came across wxPython. With wxPython, I could easily do this on a frame with a panel, the fact is that I need the window to be transparent so that the user can see its screen while selecting the desired area, but if I make the frame and the panel the objects are transparent, everything becomes badly.

So, I'm open to any solution using wxPython or not using it, because I really don't know if I use it correctly.

I am new to Python and I am not a native speaker of English, so I am very sorry if you cannot understand something.

This is what I encoded

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(), style=wx.TRANSPARENT_WINDOW) self.panel = wx.Panel(self, size=self.GetSize(), style=wx.TRANSPARENT_WINDOW) 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)) 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', 1)) dc.SetBrush(wx.Brush(wx.Color(0, 0, 0), wx.TRANSPARENT)) 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() 
+6
python wxpython
source share
1 answer

You should not use wx.TRANSPARENT in creating a window, which is mainly used for wxDC drawing commands. To make the window transparent, just call win.SetTransparent (number), where the sum from 0 to 255, 255 means opacity, 0 means completely transparent. see http://www.wxpython.org/docs/api/wx.Window-class.html#SetTransparent

I changed your code, it will work only if your platform supports transparent windows, you can check it with CanSetTransparent. I tested it on Windows XP.

 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(50) 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', 1)) dc.SetBrush(wx.Brush(wx.Color(0, 0, 0), wx.TRANSPARENT)) 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() 
+5
source share

All Articles