468,256 Members | 1,437 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,256 developers. It's quick & easy.

Implementing a Rubber Banding Script Question.

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>:
Title: wxPython Graphics - Drawing rubberbands over a canvas
Submitter: Anand Pillai (other recipes)
Last Updated: 2003/12/30
Version no: 1.1
Category: Image
Description:

This recipe talks about how to implement rubberbanding code for canvas
objects in a wxPython application. Canvas classes in wxPython include
wxPanel, wxStaticBitmap, wxGlCanvas etc.
Expand|Select|Wrap|Line Numbers
  1. # This class describes a generic method of drawing rubberbands
  2. # on a wxPython canvas object (wxStaticBitmap, wxPanel etc) when
  3. # the user presses the left mouse button and drags it over a rectangular
  4. # area. It has methods to return the selected area by the user as 
  5. # a rectangular 4 tuple / Clear the selected area.
  6.  
  7. # Beginning of code
  8.  
  9. class wxPyRubberBander:
  10.     """ A class to manage mouse events/ rubberbanding of a wxPython
  11.         canvas object """
  12.  
  13.     def __init__(self, canvas):
  14.  
  15.         # canvas object
  16.         self._canvas = canvas
  17.         # mouse selection start point
  18.         self.m_stpoint=wxPoint(0,0)
  19.         # mouse selection end point
  20.         self.m_endpoint=wxPoint(0,0)
  21.         # mouse selection cache point
  22.         self.m_savepoint=wxPoint(0,0)
  23.  
  24.         # flags for left click/ selection
  25.         self._leftclicked=false
  26.         self._selected=false
  27.  
  28.         # Register event handlers for mouse
  29.         self.RegisterEventHandlers()
  30.  
  31.     def RegisterEventHandlers(self):
  32.         """ Register event handlers for this object """
  33.  
  34.         EVT_LEFT_DOWN(self._canvas, self.OnMouseEvent)
  35.         EVT_LEFT_UP(self._canvas, self.OnMouseEvent)
  36.         EVT_MOTION(self._canvas, self.OnMouseEvent)
  37.  
  38.  
  39.     def OnMouseEvent(self, event):
  40.         """ This function manages mouse events """
  41.  
  42.  
  43.         if event:
  44.  
  45.             # set mouse cursor
  46.             self._canvas.SetCursor(wxStockCursor(wxCURSOR_ARROW))
  47.             # get device context of canvas
  48.             dc= wxClientDC(self._canvas)
  49.  
  50.             # Set logical function to XOR for rubberbanding
  51.             dc.SetLogicalFunction(wxXOR)
  52.  
  53.             # Set dc brush and pen
  54.             # Here I set brush and pen to white and grey respectively
  55.             # You can set it to your own choices
  56.  
  57.             # The brush setting is not really needed since we
  58.             # dont do any filling of the dc. It is set just for 
  59.             # the sake of completion.
  60.  
  61.             wbrush = wxBrush(wxColour(255,255,255), wxTRANSPARENT)
  62.             wpen = wxPen(wxColour(200, 200, 200), 1, wxSOLID)
  63.             dc.SetBrush(wbrush)
  64.             dc.SetPen(wpen)
  65.  
  66.  
  67.         if event.LeftDown():
  68.  
  69.            # Left mouse button down, change cursor to
  70.            # something else to denote event capture
  71.            self.m_stpoint = event.GetPosition()
  72.            cur = wxStockCursor(wxCURSOR_CROSS)  
  73.            self._canvas.SetCursor(cur)
  74.  
  75.            # invalidate current canvas
  76.            self._canvas.Refresh()
  77.            # cache current position
  78.            self.m_savepoint = self.m_stpoint
  79.            self._selected = false
  80.            self._leftclicked = true
  81.  
  82.         elif event.Dragging():   
  83.  
  84.             # User is dragging the mouse, check if
  85.             # left button is down
  86.  
  87.             if self._leftclicked:
  88.  
  89.                 # reset dc bounding box
  90.                 dc.ResetBoundingBox()
  91.                 dc.BeginDrawing()
  92.                 w = (self.m_savepoint.x - self.m_stpoint.x)
  93.                 h = (self.m_savepoint.y - self.m_stpoint.y)
  94.  
  95.                 # To erase previous rectangle
  96.                 dc.DrawRectangle(self.m_stpoint.x, self.m_stpoint.y, w, h)
  97.  
  98.                 # Draw new rectangle
  99.                 self.m_endpoint =  event.GetPosition()
  100.  
  101.                 w = (self.m_endpoint.x - self.m_stpoint.x)
  102.                 h = (self.m_endpoint.y - self.m_stpoint.y)
  103.  
  104.                 # Set clipping region to rectangle corners
  105.                 dc.SetClippingRegion(self.m_stpoint.x, self.m_stpoint.y, w,h)
  106.                 dc.DrawRectangle(self.m_stpoint.x, self.m_stpoint.y, w, h) 
  107.                 dc.EndDrawing()
  108.  
  109.                 self.m_savepoint = self.m_endpoint # cache current endpoint
  110.  
  111.         elif event.LeftUp():
  112.  
  113.             # User released left button, change cursor back
  114.             self._canvas.SetCursor(wxSTOCK_CURSOR(wxCURSOR_ARROW))       
  115.             self._selected = true  #selection is done
  116.             self._leftclicked = false # end of clicking  
  117.  
  118.  
  119.     def GetCurrentSelection(self):
  120.         """ Return the current selected rectangle """
  121.  
  122.         # if there is no selection, selection defaults to
  123.         # current viewport
  124.  
  125.         left = wxPoint(0,0)
  126.         right = wxPoint(0,0)
  127.  
  128.         # user dragged mouse to right
  129.         if self.m_endpoint.y > self.m_stpoint.y:
  130.             right = self.m_endpoint
  131.             left = self.m_stpoint
  132.         # user dragged mouse to left
  133.         elif self.m_endpoint.y < self.m_stpoint.y:
  134.             right = self.m_stpoint
  135.             left = self.m_endpoint
  136.  
  137.         return (left.x, left.y, right.x, right.y)
  138.  
  139.  
  140.     def ClearCurrentSelection(self):
  141.         """ Clear the current selected rectangle """
  142.  
  143.         box = self.GetCurrentSelection()
  144.  
  145.         dc=wxClientDC(self._canvas)
  146.  
  147.         w = box[2] - box[0]
  148.         h = box[3] - box[1]
  149.         dc.SetClippingRegion(box[0], box[1], w, h)
  150.         dc.SetLogicalFunction(wxXOR)
  151.  
  152.         # The brush is not really needed since we
  153.         # dont do any filling of the dc. It is set for 
  154.         # sake of completion.
  155.  
  156.         wbrush = wxBrush(wxColour(255,255,255), wxTRANSPARENT)
  157.         wpen = wxPen(wxColour(200, 200, 200), 1, wxSOLID)
  158.         dc.SetBrush(wbrush)
  159.         dc.SetPen(wpen)
  160.         dc.DrawRectangle(box[0], box[1], w,h)
  161.         self._selected = false 
  162.  
  163.         # reset selection to canvas size
  164.         self.ResetSelection()    
  165.  
  166.     def ResetSelection(self):
  167.         """ Resets the mouse selection to entire canvas """
  168.  
  169.         self.m_stpoint = wxPoint(0,0)
  170.         sz=self._canvas.GetSize()
  171.         w,h=sz.GetWidth(), sz.GetHeight()
  172.         self.m_endpoint = wxPoint(w,h)
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")

rubberbandscript(........)

print output

root.mainloop()



Thanks alot,
JP
Jun 21 '07 #1
3 2203
bartonc
6,596 Expert 4TB
root=Tk()
im=Image.open("filename")

rubberbandscript(........)

print output

root.mainloop()
You won't be able to use this class with Tkinter. It's designed to run in an old-style wxPython environment from back when they used to use
Expand|Select|Wrap|Line Numbers
  1. from wx import *
It would need a little tweaking to run in a modern wxPython installation.
Jun 21 '07 #2
Damn,

Well thanks for the input bud, guess it's back to the drawing board. Let me know if you know of any rubber banding area selection scripts for tkinter.

Thanks,

JP
Jun 22 '07 #3
bartonc
6,596 Expert 4TB
Damn,

Well thanks for the input bud, guess it's back to the drawing board. Let me know if you know of any rubber banding area selection scripts for tkinter.

Thanks,

JP
OR... you could install wxPython 2.8. Tkinter is really very limited. It's a good learning experience, but one soon finds that support and features are greatly lacking.
Jun 22 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by phl | last post: by
1 post views Thread by =?Utf-8?B?Sm9obiBPbGJlcnQ=?= | last post: by
2 posts views Thread by ssubbarayan | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.