Re: [Tutor] tkinter canvas

2009-01-15 Thread Mr Gerard Kelly
wow, that's excellent, thanks so much.

I haven't got a clue how lambda functions work, but they seem pretty
useful, so I'll try to figure them out.

- Original Message -
From: Kent Johnson 
Date: Thursday, January 15, 2009 10:24 pm
Subject: Re: [Tutor] tkinter canvas
> On Thu, Jan 15, 2009 at 12:41 AM, Mr Gerard Kelly
>  wrote:
> 
> > I want to be able to bind these boxes to an event - so that I can 
> either> click on them, or hold the mouse cursor over them, and have 
> them change
> > color.
> 
> Here is a version of your program that binds the Enter and Leave
> events to each box and changes the box color when the mouse is over
> it:
> 
> #
> 
> from Tkinter import *
> 
> master = Tk()
> 
> numboxes=6
> 
> width=40*(numboxes+2)
> height=200
> w = Canvas(master, width=width, height=height)
> w.pack()
> 
> size=width/(numboxes+2)
> 
> box=[0]*numboxes
> 
> def enter(e, i):
>e.widget.itemconfigure(box[i], fill='red')
> 
> def leave(e, i):
>e.widget.itemconfigure(box[i], fill='blue')
> 
> for i in range(numboxes):
>box[i]=w.create_rectangle((1+i)*40, 40, (2+i)*40, height-40, 
> fill="blue")w.tag_bind(box[i], '', lambda e, i=i: 
> enter(e, i))
>w.tag_bind(box[i], '', lambda e, i=i: leave(e, i))
> 
> mainloop()
> 
> ###
> 
> The 'i=i' in the lambda is needed due to the (surprising) way that
> variables are bound to closures; without it, every event would be
> bound to the same value of i. Some explanation here:
> http://code.activestate.com/recipes/502271/
> 
> Kent
> 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] tkinter canvas

2009-01-15 Thread Alan Gauld

"Kent Johnson"  wrote


for i in range(numboxes):
   box[i]=w.create_rectangle((1+i)*40, 40, (2+i)*40, height-40, 
fill="blue")

   w.tag_bind(box[i], '', lambda e, i=i: enter(e, i))
   w.tag_bind(box[i], '', lambda e, i=i: leave(e, i))


I'm wrong again. I've never noticed tag_bind() before.
Although on investigation it is used by Grayson in his Tkinter book...

I notice it can also be used in the Text widget to catch events on
specific items of text or embedded images etc.

Nice.

Alan G 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] tkinter canvas

2009-01-15 Thread Kent Johnson
On Thu, Jan 15, 2009 at 12:41 AM, Mr Gerard Kelly
 wrote:

> I want to be able to bind these boxes to an event - so that I can either
> click on them, or hold the mouse cursor over them, and have them change
> color.

Here is a version of your program that binds the Enter and Leave
events to each box and changes the box color when the mouse is over
it:

#

from Tkinter import *

master = Tk()

numboxes=6

width=40*(numboxes+2)
height=200
w = Canvas(master, width=width, height=height)
w.pack()

size=width/(numboxes+2)

box=[0]*numboxes

def enter(e, i):
e.widget.itemconfigure(box[i], fill='red')

def leave(e, i):
e.widget.itemconfigure(box[i], fill='blue')

for i in range(numboxes):
box[i]=w.create_rectangle((1+i)*40, 40, (2+i)*40, height-40, fill="blue")
w.tag_bind(box[i], '', lambda e, i=i: enter(e, i))
w.tag_bind(box[i], '', lambda e, i=i: leave(e, i))

mainloop()

###

The 'i=i' in the lambda is needed due to the (surprising) way that
variables are bound to closures; without it, every event would be
bound to the same value of i. Some explanation here:
http://code.activestate.com/recipes/502271/

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] tkinter canvas

2009-01-15 Thread Alan Gauld

"Mr Gerard Kelly"  wrote

All it does take a number (6 in this case) and draw that number of 
blue

boxes on a canvas.

I want to be able to bind these boxes to an event - so that I can 
either
click on them, or hold the mouse cursor over them, and have them 
change

color.


I don;t think you can bind events to an object on a canvas, the events
can only be bound to the canvas.

So you would need to determine which object was being highlighted
using the x,y coordinates of the event at the canvas level and then
manipulate the shapes accordingly. This might be easier if you create
a series of shape objects and hold a list of those. You can then 
iterate
over the list to lovcate which object contains the point and that 
object

can update itself. This also makes handling the case of overlapping
objects easier.

There might be a more Tkinter'ish way of doing this but I've not come
across it.

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] tkinter canvas

2009-01-14 Thread Mr Gerard Kelly
I am trying to make a simple Tkinter program using the canvas widget.
All it does take a number (6 in this case) and draw that number of blue
boxes on a canvas.

from Tkinter import *

master = Tk()

numboxes=6

width=40*(numboxes+2)
height=200
w = Canvas(master, width=width, height=height)
w.pack()

size=width/(numboxes+2)

box=[0]*numboxes

for i in range(numboxes):
  box[i]=w.create_rectangle((1+i)*40, 40, (2+i)*40, height-40, fill="blue")

mainloop()



I want to be able to bind these boxes to an event - so that I can either
click on them, or hold the mouse cursor over them, and have them change
color. However I haven't been able to find any examples of related
tkinter code on google, and the tutorials and documentation about
binding and events with canvas items is very confusing without being
able to see it used in examples. 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Tkinter Canvas Saving and Opening

2007-12-04 Thread Johnston Jiaa
I know that Tkinter's canvas can output its contents into a  
postscript file, but can that file be used in turn to restore the  
image?  How can I implement this file-saving and opening feature?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tkinter Canvas Widget

2007-11-13 Thread Alan Gauld

"Johnston Jiaa" <[EMAIL PROTECTED]> wrote

> methods.html>, but do not see how to get the coordinates of the 
> mouse
> on the canvas.

Any mouse event will give you the coordinates within the event data.
Trapping the mouse Movment will ensure you track the current position,

> Also, after I get those coordinates, which methods would be
> appropriate to draw the points onto the canvas itself?

There are lots of drawing methods depending on what kind of
thing you want to draw. You can draw lines, arcs, ovals,
rectangles etc. If you just want to draw a single point then you
can use a very short line or very small filled rectangle or circle.

If you can show us some sample code where you are having
problems we can be of more help.


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tkinter Canvas Widget

2007-11-12 Thread John Fouhy
On 13/11/2007, Johnston Jiaa <[EMAIL PROTECTED]> wrote:
> I'm trying to create a free-hand drawing area on my program using the
> Tkinter canvas widget.  I have read through all the methods on Canvas
> objects  methods.html>, but do not see how to get the coordinates of the mouse
> on the canvas.
>
> Also, after I get those coordinates, which methods would be
> appropriate to draw the points onto the canvas itself?

It's been a while since I used Tkinter, but from memory:

To get the mouse coordinates, you'll need to use events.  Check out
http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm for some
details.

Essentially, you'll need to bind a function to the B1-Motion event,
and then inspect the event object to find out the mouse location.

(I'm assuming here that you only want to draw your lines when the user
has a button depressed..)

To draw points, you could perhaps use canvas.create_oval with a small
bounding box.

HTH!

-- 
John.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Tkinter Canvas Widget

2007-11-12 Thread Johnston Jiaa
I'm trying to create a free-hand drawing area on my program using the  
Tkinter canvas widget.  I have read through all the methods on Canvas  
objects , but do not see how to get the coordinates of the mouse  
on the canvas.

Also, after I get those coordinates, which methods would be  
appropriate to draw the points onto the canvas itself?

I appreciate any pointers or links to resources or anything at all.

Johnston Jiaa
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tkinter Canvas

2006-12-08 Thread Luke Paireepinart
Asrarahmed Kadri wrote:
>
>
> Hi Folks,
>
> I have a Tkinter canvas and a yscrollbar attached to it. It is working 
> fine but what I want is that when the mouse is in the canvas region, 
> the scroll button of the mouse should be able to control the upward 
> and downward movement, How to achieve this?
You should be able to bind the events that the mouse wheel generates in 
the canvas widget to the function that causes the scroll bar to scroll 
(forget what it's called.)
Have you tried this yet?
>
> Regards,
> Asrarahmed Kadri
>
>
> -- 
> To HIM you shall return.
> 
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>   

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Tkinter Canvas

2006-12-08 Thread Asrarahmed Kadri

Hi Folks,

I have a Tkinter canvas and a yscrollbar attached to it. It is working fine
but what I want is that when the mouse is in the canvas region, the scroll
button of the mouse should be able to control the upward and downward
movement, How to achieve this?

Regards,
Asrarahmed Kadri


--
To HIM you shall return.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor