tkinter canvas

John McMonagle johnmc at velseis.com.au
Tue Apr 4 20:14:12 EDT 2006


On Tue, 2006-04-04 at 16:42 -0700, fxe wrote:
> Hi John , thanks a million for the info.Looking at your solution I am sure
> this would work. After struggling with this for the past few hours I found
> another way just before reading your post. Its as simple as :
> 
> canvas.create_line(x1,y1,x2,y2,fill='#000000',state=DISABLED) 

Tom,

below is some quick and dirty code illustrating the tag_bind concept
similar to your case:

from Tkinter import *

def startMove(event):
    global startx, starty, item
    widget = event.widget
    startx, starty = widget.winfo_pointerxy()
    x = widget.canvasx(event.x)
    y = widget.canvasy(event.y)
    item = widget.find_closest(x,y)
    
def movingRect(event):
    global startx, starty, item
    widget = event.widget
    newx,newy = widget.winfo_pointerxy()
    diffx = newx - startx
    diffy = newy - starty
    widget.move(item, diffx, diffy)
    widget.update_idletasks()
    startx = newx
    starty = newy
    
r = Tk()

c = Canvas(r, width=500, height=500)
c.pack(fill=BOTH, expand=YES)

# Draw grid lines every 50 pixels
for x in range (0,500,50):
    c.create_line(x,0,x,500,tags='grid')
for y in range(0,500,50):
    c.create_line(0,y,500,y,tags='grid')

# Draw some random rectangles on top
c.create_rectangle(124,124,182,190,fill='#FF0000',outline='#FF0000',tags='rect')
c.create_rectangle(90,50,100,300,fill='#0000FF',outline='#0000FF',tags='rect')
c.create_rectangle(320,210,415,290,fill='#00FF00',outline='#00FF00',tags='rect')
c.create_rectangle(400,50,450,100,fill='#00FFFF',outline='#00FFFF',tags='rect')

# bind rect tags to move functions
c.tag_bind('rect', '<Button-1>', startMove)
c.tag_bind('rect', '<Button1-Motion>', movingRect)

r.mainloop()

Cheers,

John McMonagle




-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.




More information about the Python-list mailing list