Why doesn't the canvas start at (0,0) ?

It does. You just need to set the borderwidth and highlightthickness options to zero.

canvas = Canvas(parent, bd=0, highlightthickness=0)

How do I manipulate individual pixels on the canvas ?

It's not that you can't; more like you shouldn't. Use an image instead (Photoimage or PIL). The things you put onto a Canvas become 'objects', not just pixels set to a particular color. Canvas items can be moved, highlighted, disabled etc, which means that the canvas needs to keep track of them all. As a result, pixel manipulation on the canvas is not very efficient.

Why shouldn't I use numbers as tags ?

There are two different ways to reference items on a canvas.

Now the problem. Integers are interpreted as item handles, not tags, even if you pass them to Tkinter as strings. This can lead to strange behaviour in your program.

I am messing around with a tkinter canvas and a lot of items in it. For each item I would like to detect clicks and so on. Should I do a bind for each item, or would it be better letting the canvas detect the click, and find what item was clicked on ?

Except for Enter/Leave events, using a single binding is no harder than using item bindings. The trick is to use the CURRENT tag, as shown in the attached example.

An alternative to CURRENT is to use something like find_overlapping(event.x-m, event.y-m, event.x+m, event.y+m) (where m is 2-3 pixels) to find items near the mouse pointer. /F

   1 #
   2 # tracking mouse actions for many canvas objects
   3 #
   4 # fredrik lundh, april 1998
   5 #
   6 # fredrik@pythonware.com
   7 # http://www.pythonware.com
   8 #
   9 
  10 from Tkinter import *
  11 from random import randint
  12 
  13 root = Tk()
  14 
  15 canvas = Canvas(root, width=200, height=200)
  16 canvas.pack()
  17 
  18 def click(event):
  19     if canvas.find_withtag(CURRENT):
  20         canvas.itemconfig(CURRENT, fill="blue")
  21         canvas.update_idletasks()
  22         canvas.after(200)
  23         canvas.itemconfig(CURRENT, fill="red")
  24 
  25 for i in range(100):
  26     x, y = randint(0, 200-1), randint(0, 200-1)
  27     canvas.create_oval(x-5, y-5, x+5, y+5, fill="red")
  28 
  29 canvas.bind("<Button-1>", click)
  30 
  31 root.mainloop()

tkinter: CanvasConundrums (last edited 2011-07-05 22:43:20 by AnthonyMuss)