473,757 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Implementing a Rubber Banding Script Question.

31 New Member
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")

rubberbandscrip t(........)

print output

root.mainloop()



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

rubberbandscrip t(........)

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
Nebulism
31 New Member
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 Recognized Expert Expert
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
8096
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 submitted, while they are typing, I'm trying to offer them some common word processing functions. I want to implement unlimited undo and redo for the textarea. I've set the textarea to onChange="addToArrayOfPastWork()"; My undo button gives me...
2
2029
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 are executed as if the user *manually* typed them in the console. Flags are special commands that tell the program where to BREAK, LOOP, START. A typical script may look like this: ; This is my script. It will test my mp3 player... START:
4
3334
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, how are you sure that your managed object resources are completely freed up of resources it's might be using? In my case below I have a private bool variable. Are there any other managed resource that you might need to explicitly free up in
0
1551
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 loaded. Now i want to Cut out the part i marked with the "Rubber band" and load that image in the picturebox. This works great and i can save that image as i want to. The problem is that the image loaded is smaller cause when i load the image i use...
6
3990
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 string UID; public string PWD; }
1
1518
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 hosting work surfaces? Ideally after selection it would return a collection of the controls inside the selected area (textboxes, labels, buttons, etc). -- John Olbert
3
6298
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 works like this: First, the user enters a number to test (to find the two prime numbers that make up that number). Next the program finds the numbers, outputs them, then quits. What I want to do is create a prompt after the number has been processed...
2
2858
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 message in a mobile device.With in the given view area if the whole message does not fit,we need to provide the ability for users to scroll through the entire message using pageup/pagedown or key up and key down.In our case we have the whole...
0
9487
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9297
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10069
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8736
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7285
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5324
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3828
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3395
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2697
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.