Hi everyone!
Earlier I asked a question about mouse interaction with a GUI. I have found a pretty comprehensive script that is supposed to work <mod edit: link removed, source inserted>:
[CODE=python]# This class describes a generic method of drawing rubberbands
# on a wxPython canvas object (wxStaticBitmap , wxPanel etc) when
# the user presses the left mouse button and drags it over a rectangular
# area. It has methods to return the selected area by the user as
# a rectangular 4 tuple / Clear the selected area.
# Beginning of code
class wxPyRubberBande r:
""" A class to manage mouse events/ rubberbanding of a wxPython
canvas object """
def __init__(self, canvas):
# canvas object
self._canvas = canvas
# mouse selection start point
self.m_stpoint= wxPoint(0,0)
# mouse selection end point
self.m_endpoint =wxPoint(0,0)
# mouse selection cache point
self.m_savepoin t=wxPoint(0,0)
# flags for left click/ selection
self._leftclick ed=false
self._selected= false
# Register event handlers for mouse
self.RegisterEv entHandlers()
def RegisterEventHa ndlers(self):
""" Register event handlers for this object """
EVT_LEFT_DOWN(s elf._canvas, self.OnMouseEve nt)
EVT_LEFT_UP(sel f._canvas, self.OnMouseEve nt)
EVT_MOTION(self ._canvas, self.OnMouseEve nt)
def OnMouseEvent(se lf, event):
""" This function manages mouse events """
if event:
# set mouse cursor
self._canvas.Se tCursor(wxStock Cursor(wxCURSOR _ARROW))
# get device context of canvas
dc= wxClientDC(self ._canvas)
# Set logical function to XOR for rubberbanding
dc.SetLogicalFu nction(wxXOR)
# Set dc brush and pen
# Here I set brush and pen to white and grey respectively
# You can set it to your own choices
# The brush setting is not really needed since we
# dont do any filling of the dc. It is set just for
# the sake of completion.
wbrush = wxBrush(wxColou r(255,255,255), wxTRANSPARENT)
wpen = wxPen(wxColour( 200, 200, 200), 1, wxSOLID)
dc.SetBrush(wbr ush)
dc.SetPen(wpen)
if event.LeftDown( ):
# Left mouse button down, change cursor to
# something else to denote event capture
self.m_stpoint = event.GetPositi on()
cur = wxStockCursor(w xCURSOR_CROSS)
self._canvas.Se tCursor(cur)
# invalidate current canvas
self._canvas.Re fresh()
# cache current position
self.m_savepoin t = self.m_stpoint
self._selected = false
self._leftclick ed = true
elif event.Dragging( ):
# User is dragging the mouse, check if
# left button is down
if self._leftclick ed:
# reset dc bounding box
dc.ResetBoundin gBox()
dc.BeginDrawing ()
w = (self.m_savepoi nt.x - self.m_stpoint. x)
h = (self.m_savepoi nt.y - self.m_stpoint. y)
# To erase previous rectangle
dc.DrawRectangl e(self.m_stpoin t.x, self.m_stpoint. y, w, h)
# Draw new rectangle
self.m_endpoint = event.GetPositi on()
w = (self.m_endpoin t.x - self.m_stpoint. x)
h = (self.m_endpoin t.y - self.m_stpoint. y)
# Set clipping region to rectangle corners
dc.SetClippingR egion(self.m_st point.x, self.m_stpoint. y, w,h)
dc.DrawRectangl e(self.m_stpoin t.x, self.m_stpoint. y, w, h)
dc.EndDrawing()
self.m_savepoin t = self.m_endpoint # cache current endpoint
elif event.LeftUp():
# User released left button, change cursor back
self._canvas.Se tCursor(wxSTOCK _CURSOR(wxCURSO R_ARROW))
self._selected = true #selection is done
self._leftclick ed = false # end of clicking
def GetCurrentSelec tion(self):
""" Return the current selected rectangle """
# if there is no selection, selection defaults to
# current viewport
left = wxPoint(0,0)
right = wxPoint(0,0)
# user dragged mouse to right
if self.m_endpoint .y > self.m_stpoint. y:
right = self.m_endpoint
left = self.m_stpoint
# user dragged mouse to left
elif self.m_endpoint .y < self.m_stpoint. y:
right = self.m_stpoint
left = self.m_endpoint
return (left.x, left.y, right.x, right.y)
def ClearCurrentSel ection(self):
""" Clear the current selected rectangle """
box = self.GetCurrent Selection()
dc=wxClientDC(s elf._canvas)
w = box[2] - box[0]
h = box[3] - box[1]
dc.SetClippingR egion(box[0], box[1], w, h)
dc.SetLogicalFu nction(wxXOR)
# The brush is not really needed since we
# dont do any filling of the dc. It is set for
# sake of completion.
wbrush = wxBrush(wxColou r(255,255,255), wxTRANSPARENT)
wpen = wxPen(wxColour( 200, 200, 200), 1, wxSOLID)
dc.SetBrush(wbr ush)
dc.SetPen(wpen)
dc.DrawRectangl e(box[0], box[1], w,h)
self._selected = false
# reset selection to canvas size
self.ResetSelec tion()
def ResetSelection( self):
""" Resets the mouse selection to entire canvas """
self.m_stpoint = wxPoint(0,0)
sz=self._canvas .GetSize()
w,h=sz.GetWidth (), sz.GetHeight()
self.m_endpoint = wxPoint(w,h)[/CODE]
The problem is I'm not to savy with classes yet and I don't know how to call it and use it. Can someone guide me through this, I would be pretty grateful.
The main objective would be to set the image that is loaded as the canvas of the rubberbander and output the result.
Says we start with some pseudocode:
root=Tk()
im=Image.open(" filename")
rubberbandscrip t(........)
print output
root.mainloop()
Thanks alot,
JP
Earlier I asked a question about mouse interaction with a GUI. I have found a pretty comprehensive script that is supposed to work <mod edit: link removed, source inserted>:
Originally posted by ASPN
# on a wxPython canvas object (wxStaticBitmap , wxPanel etc) when
# the user presses the left mouse button and drags it over a rectangular
# area. It has methods to return the selected area by the user as
# a rectangular 4 tuple / Clear the selected area.
# Beginning of code
class wxPyRubberBande r:
""" A class to manage mouse events/ rubberbanding of a wxPython
canvas object """
def __init__(self, canvas):
# canvas object
self._canvas = canvas
# mouse selection start point
self.m_stpoint= wxPoint(0,0)
# mouse selection end point
self.m_endpoint =wxPoint(0,0)
# mouse selection cache point
self.m_savepoin t=wxPoint(0,0)
# flags for left click/ selection
self._leftclick ed=false
self._selected= false
# Register event handlers for mouse
self.RegisterEv entHandlers()
def RegisterEventHa ndlers(self):
""" Register event handlers for this object """
EVT_LEFT_DOWN(s elf._canvas, self.OnMouseEve nt)
EVT_LEFT_UP(sel f._canvas, self.OnMouseEve nt)
EVT_MOTION(self ._canvas, self.OnMouseEve nt)
def OnMouseEvent(se lf, event):
""" This function manages mouse events """
if event:
# set mouse cursor
self._canvas.Se tCursor(wxStock Cursor(wxCURSOR _ARROW))
# get device context of canvas
dc= wxClientDC(self ._canvas)
# Set logical function to XOR for rubberbanding
dc.SetLogicalFu nction(wxXOR)
# Set dc brush and pen
# Here I set brush and pen to white and grey respectively
# You can set it to your own choices
# The brush setting is not really needed since we
# dont do any filling of the dc. It is set just for
# the sake of completion.
wbrush = wxBrush(wxColou r(255,255,255), wxTRANSPARENT)
wpen = wxPen(wxColour( 200, 200, 200), 1, wxSOLID)
dc.SetBrush(wbr ush)
dc.SetPen(wpen)
if event.LeftDown( ):
# Left mouse button down, change cursor to
# something else to denote event capture
self.m_stpoint = event.GetPositi on()
cur = wxStockCursor(w xCURSOR_CROSS)
self._canvas.Se tCursor(cur)
# invalidate current canvas
self._canvas.Re fresh()
# cache current position
self.m_savepoin t = self.m_stpoint
self._selected = false
self._leftclick ed = true
elif event.Dragging( ):
# User is dragging the mouse, check if
# left button is down
if self._leftclick ed:
# reset dc bounding box
dc.ResetBoundin gBox()
dc.BeginDrawing ()
w = (self.m_savepoi nt.x - self.m_stpoint. x)
h = (self.m_savepoi nt.y - self.m_stpoint. y)
# To erase previous rectangle
dc.DrawRectangl e(self.m_stpoin t.x, self.m_stpoint. y, w, h)
# Draw new rectangle
self.m_endpoint = event.GetPositi on()
w = (self.m_endpoin t.x - self.m_stpoint. x)
h = (self.m_endpoin t.y - self.m_stpoint. y)
# Set clipping region to rectangle corners
dc.SetClippingR egion(self.m_st point.x, self.m_stpoint. y, w,h)
dc.DrawRectangl e(self.m_stpoin t.x, self.m_stpoint. y, w, h)
dc.EndDrawing()
self.m_savepoin t = self.m_endpoint # cache current endpoint
elif event.LeftUp():
# User released left button, change cursor back
self._canvas.Se tCursor(wxSTOCK _CURSOR(wxCURSO R_ARROW))
self._selected = true #selection is done
self._leftclick ed = false # end of clicking
def GetCurrentSelec tion(self):
""" Return the current selected rectangle """
# if there is no selection, selection defaults to
# current viewport
left = wxPoint(0,0)
right = wxPoint(0,0)
# user dragged mouse to right
if self.m_endpoint .y > self.m_stpoint. y:
right = self.m_endpoint
left = self.m_stpoint
# user dragged mouse to left
elif self.m_endpoint .y < self.m_stpoint. y:
right = self.m_stpoint
left = self.m_endpoint
return (left.x, left.y, right.x, right.y)
def ClearCurrentSel ection(self):
""" Clear the current selected rectangle """
box = self.GetCurrent Selection()
dc=wxClientDC(s elf._canvas)
w = box[2] - box[0]
h = box[3] - box[1]
dc.SetClippingR egion(box[0], box[1], w, h)
dc.SetLogicalFu nction(wxXOR)
# The brush is not really needed since we
# dont do any filling of the dc. It is set for
# sake of completion.
wbrush = wxBrush(wxColou r(255,255,255), wxTRANSPARENT)
wpen = wxPen(wxColour( 200, 200, 200), 1, wxSOLID)
dc.SetBrush(wbr ush)
dc.SetPen(wpen)
dc.DrawRectangl e(box[0], box[1], w,h)
self._selected = false
# reset selection to canvas size
self.ResetSelec tion()
def ResetSelection( self):
""" Resets the mouse selection to entire canvas """
self.m_stpoint = wxPoint(0,0)
sz=self._canvas .GetSize()
w,h=sz.GetWidth (), sz.GetHeight()
self.m_endpoint = wxPoint(w,h)[/CODE]
The problem is I'm not to savy with classes yet and I don't know how to call it and use it. Can someone guide me through this, I would be pretty grateful.
The main objective would be to set the image that is loaded as the canvas of the rubberbander and output the result.
Says we start with some pseudocode:
root=Tk()
im=Image.open(" filename")
rubberbandscrip t(........)
print output
root.mainloop()
Thanks alot,
JP
Comment