Created
August 2, 2020 16:33
-
-
Save ShubhamJain7/68637f62c4c59ef9bfe9d2fba5d56659 to your computer and use it in GitHub Desktop.
wxPython bounding boxes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import wx | |
class Frame(wx.Frame): | |
def __init__(self,boxes): | |
super(Frame, self).__init__(None, title="Bounding boxes") | |
self.boxes = boxes | |
self.boundingBoxes = [] | |
self.status = [] | |
self.ShowFullScreen(True) | |
self.SetTransparent(50) | |
self.Bind(wx.EVT_MOTION, self.mouseMovement) | |
self.Bind(wx.EVT_KILL_FOCUS, self.onFocusLost) | |
self.Bind(wx.EVT_KEY_DOWN, self.onEsc) | |
self.InitUI() | |
def InitUI(self): | |
self.Bind(wx.EVT_PAINT, self.OnPaint) | |
self.Centre() | |
self.Show(True) | |
def OnPaint(self, e): | |
dc = wx.ScreenDC() | |
dc.SetPen(wx.Pen("blue", 5)) | |
dc.SetBrush(wx.Brush("blue", wx.TRANSPARENT)) | |
dc.SetFont(wx.Font(wx.FontInfo(10).Bold())) | |
for i, (x,y,w,h) in enumerate(self.boxes): | |
dc.DrawRectangle(x, y, w, h) | |
dc.DrawText(f"Box {i+1}", x, y-20) | |
self.boundingBoxes.append((x, y, x+w, y+h)) | |
self.status.append(True) | |
def mouseMovement(self, event): | |
x,y = event.GetPosition() | |
for i in range(len(self.boundingBoxes)): | |
bb = self.boundingBoxes[i] | |
if x>bb[0] and x<bb[2] and y>bb[1] and y<bb[3]: | |
if self.status[i]: | |
print(f"inside box {i+1}") | |
self.status[i] = False | |
else: | |
if not self.status[i]: | |
self.status[i] = True | |
def onFocusLost(self, event): | |
self.Close() | |
def onEsc(self, event): | |
key_code = event.GetKeyCode() | |
if key_code == wx.WXK_ESCAPE: | |
self.Close() | |
else: | |
event.Skip() | |
if __name__ == '__main__': | |
app = wx.App() | |
boxes = [[100, 100, 100, 100], [500, 400, 80, 60]] | |
frame = Frame(boxes) | |
app.MainLoop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import wx | |
class Frame(wx.Frame): | |
def __init__(self,boxes): | |
super(Frame, self).__init__(None, title="Bounding boxes") | |
self.boxes = boxes | |
self.boundingBoxes = [] | |
self.status = [] | |
self.ShowFullScreen(True) | |
self.SetTransparent(50) | |
self.Bind(wx.EVT_MOTION, self.mouseMovement) | |
self.Bind(wx.EVT_KILL_FOCUS, self.onFocusLost) | |
self.Bind(wx.EVT_KEY_DOWN, self.onEsc) | |
self.InitUI() | |
def InitUI(self): | |
self.Bind(wx.EVT_PAINT, self.OnPaint) | |
self.Centre() | |
self.Show(True) | |
def OnPaint(self, e): | |
dc = wx.PaintDC(self) | |
dc.SetPen(wx.Pen("blue", 5)) | |
dc.SetBrush(wx.Brush("blue", wx.TRANSPARENT)) | |
dc.SetFont(wx.Font(wx.FontInfo(10).Bold())) | |
for i, (x,y,w,h) in enumerate(self.boxes): | |
dc.DrawRectangle(x, y, w, h) | |
dc.DrawText(f"Box {i+1}", x, y-20) | |
self.boundingBoxes.append((x, y, x+w, y+h)) | |
self.status.append(True) | |
def mouseMovement(self, event): | |
x,y = event.GetPosition() | |
for i in range(len(self.boundingBoxes)): | |
bb = self.boundingBoxes[i] | |
if x>bb[0] and x<bb[2] and y>bb[1] and y<bb[3]: | |
if self.status[i]: | |
print(f"inside box {i+1}") | |
self.status[i] = False | |
else: | |
if not self.status[i]: | |
self.status[i] = True | |
def onFocusLost(self, event): | |
self.Close() | |
def onEsc(self, event): | |
key_code = event.GetKeyCode() | |
if key_code == wx.WXK_ESCAPE: | |
self.Close() | |
else: | |
event.Skip() | |
if __name__ == '__main__': | |
app = wx.App() | |
boxes = [[100, 100, 100, 100], [500, 400, 80, 60]] | |
frame = Frame(boxes) | |
app.MainLoop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import wx | |
class Frame(wx.Frame): | |
def __init__(self, boxes): | |
super(Frame, self).__init__(None, title="Bounding boxes") | |
self.boxes = boxes | |
self.boundingBoxes = [] | |
self.status = [] | |
self.Bind(wx.EVT_PAINT, self.OnPaint) | |
self.Bind(wx.EVT_MOTION, self.mouseMovement) | |
self.Bind(wx.EVT_KILL_FOCUS, self.onFocusLost) | |
self.Bind(wx.EVT_KEY_DOWN, self.onEsc) | |
self.ShowFullScreen(True) | |
self.SetTransparent(150) | |
def OnPaint(self, event): | |
w, h = self.GetSize() | |
dc = wx.PaintDC(self) | |
dc.SetBrush(wx.BLUE_BRUSH) | |
dc.SetFont(wx.Font(wx.FontInfo(10).Bold())) | |
region = wx.Region([(0, 0), (w, 0), (w, h), (0, h)]) | |
for i, (x, y, width, height) in enumerate(self.boxes): | |
window = wx.Region(x, y, width, height) | |
self.boundingBoxes.append((x, y, x+width, y+height)) | |
self.status.append(True) | |
region.Subtract(window) | |
dc.DrawText(f"Box {i+1}", x, y) | |
dc.SetDeviceClippingRegion(region) | |
dc.DrawRectangle(0, 0, w, h) | |
def mouseMovement(self, event): | |
x,y = event.GetPosition() | |
for i in range(len(self.boundingBoxes)): | |
bb = self.boundingBoxes[i] | |
if x>bb[0] and x<bb[2] and y>bb[1] and y<bb[3]: | |
if self.status[i]: | |
print(f"inside box {i+1}") | |
self.status[i] = False | |
else: | |
if not self.status[i]: | |
self.status[i] = True | |
def onFocusLost(self, event): | |
self.Close() | |
def onEsc(self, event): | |
key_code = event.GetKeyCode() | |
if key_code == wx.WXK_ESCAPE: | |
self.Close() | |
else: | |
event.Skip() | |
if __name__ == '__main__': | |
app = wx.App() | |
boxes = [[100, 100, 100, 100], [500, 400, 80, 60]] | |
frame = Frame(boxes) | |
app.MainLoop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment