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.
- # 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 wxPyRubberBander:
-
""" 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_savepoint=wxPoint(0,0)
-
-
# flags for left click/ selection
-
self._leftclicked=false
-
self._selected=false
-
-
# Register event handlers for mouse
-
self.RegisterEventHandlers()
-
-
def RegisterEventHandlers(self):
-
""" Register event handlers for this object """
-
-
EVT_LEFT_DOWN(self._canvas, self.OnMouseEvent)
-
EVT_LEFT_UP(self._canvas, self.OnMouseEvent)
-
EVT_MOTION(self._canvas, self.OnMouseEvent)
-
-
-
def OnMouseEvent(self, event):
-
""" This function manages mouse events """
-
-
-
if event:
-
-
# set mouse cursor
-
self._canvas.SetCursor(wxStockCursor(wxCURSOR_ARROW))
-
# get device context of canvas
-
dc= wxClientDC(self._canvas)
-
-
# Set logical function to XOR for rubberbanding
-
dc.SetLogicalFunction(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(wxColour(255,255,255), wxTRANSPARENT)
-
wpen = wxPen(wxColour(200, 200, 200), 1, wxSOLID)
-
dc.SetBrush(wbrush)
-
dc.SetPen(wpen)
-
-
-
if event.LeftDown():
-
-
# Left mouse button down, change cursor to
-
# something else to denote event capture
-
self.m_stpoint = event.GetPosition()
-
cur = wxStockCursor(wxCURSOR_CROSS)
-
self._canvas.SetCursor(cur)
-
-
# invalidate current canvas
-
self._canvas.Refresh()
-
# cache current position
-
self.m_savepoint = self.m_stpoint
-
self._selected = false
-
self._leftclicked = true
-
-
elif event.Dragging():
-
-
# User is dragging the mouse, check if
-
# left button is down
-
-
if self._leftclicked:
-
-
# reset dc bounding box
-
dc.ResetBoundingBox()
-
dc.BeginDrawing()
-
w = (self.m_savepoint.x - self.m_stpoint.x)
-
h = (self.m_savepoint.y - self.m_stpoint.y)
-
-
# To erase previous rectangle
-
dc.DrawRectangle(self.m_stpoint.x, self.m_stpoint.y, w, h)
-
-
# Draw new rectangle
-
self.m_endpoint = event.GetPosition()
-
-
w = (self.m_endpoint.x - self.m_stpoint.x)
-
h = (self.m_endpoint.y - self.m_stpoint.y)
-
-
# Set clipping region to rectangle corners
-
dc.SetClippingRegion(self.m_stpoint.x, self.m_stpoint.y, w,h)
-
dc.DrawRectangle(self.m_stpoint.x, self.m_stpoint.y, w, h)
-
dc.EndDrawing()
-
-
self.m_savepoint = self.m_endpoint # cache current endpoint
-
-
elif event.LeftUp():
-
-
# User released left button, change cursor back
-
self._canvas.SetCursor(wxSTOCK_CURSOR(wxCURSOR_ARROW))
-
self._selected = true #selection is done
-
self._leftclicked = false # end of clicking
-
-
-
def GetCurrentSelection(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 ClearCurrentSelection(self):
-
""" Clear the current selected rectangle """
-
-
box = self.GetCurrentSelection()
-
-
dc=wxClientDC(self._canvas)
-
-
w = box[2] - box[0]
-
h = box[3] - box[1]
-
dc.SetClippingRegion(box[0], box[1], w, h)
-
dc.SetLogicalFunction(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(wxColour(255,255,255), wxTRANSPARENT)
-
wpen = wxPen(wxColour(200, 200, 200), 1, wxSOLID)
-
dc.SetBrush(wbrush)
-
dc.SetPen(wpen)
-
dc.DrawRectangle(box[0], box[1], w,h)
-
self._selected = false
-
-
# reset selection to canvas size
-
self.ResetSelection()
-
-
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)
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
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
It would need a little tweaking to run in a modern wxPython installation.
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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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:
|
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
|
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...
|
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;
}
| |
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
|
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...
|
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...
|
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...
|
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,...
|
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...
| |
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |