472,958 Members | 2,209 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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 2418
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

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

Similar topics

6
by: lkrubner | last post by:
I'm offering users the ability to type weblog posts into a form and post them. They type the text into a TEXTAREA which is on a form. The form, when submitted, hits a PHP script. Before it is...
2
by: ern | last post by:
My command-line application must be able to run text scripts (macros). The scripts have commands, comments, and flags. Comments are ignored (maybe they are any line beginning with " ; ") Commands...
4
by: phl | last post by:
hi, My question is: 1. To avoid possible memory leaks, when you use this pattern, after you have dealth with the unmanaged resources and before you take your object off the finalize queue,...
0
by: Ben3eeE | last post by:
I got one too hard for me to fix problem. I know the cause and i know why but i cant fix it. I am using a "Rubber band" like function, drawing a rectangle on an image in a picturebox that i...
6
by: Joseph Geretz | last post by:
I have the following class which I am serializing and passing back and forth between my Web Service application and the client. public class Token : SoapHeader { public string SID; public...
1
by: =?Utf-8?B?Sm9obiBPbGJlcnQ=?= | last post by:
Rubber Banding in Net2.0 Is there any functionality built into Net2.0 for Rubber Banding (selection by click, drag and release) on the Control based classes such as the Pane class or other classes...
3
by: sigkill9 | last post by:
I've been working on this script all weekend and am almost done with it accept for one last detail; implementing a "process another number?" prompt after a user input is processed. The script...
2
by: ssubbarayan | last post by:
Dear all, I am in the process of implementing pageup/pagedown feature in our consumer electronics project.The idea is to provide feature to the customers to that similar to viewing a single sms...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.