[Tutor] 1 or 2 Tkinter Canvas questions

Gregor Lingl glingl at aon.at
Tue Mar 9 08:57:47 EST 2004


Hi!

The very interesting reply to the last
posting of Marilyn lets me hope, that
there are people reading and writing
here, who can answer the following questions:

Recently I wrote a simple Tkinter program
example which should draw a maximal rectangle
on a (resizable) Canvas. It goes essentially
like this:

from Tkinter import *

class RedRect(Tk):
   def __init__(self):
       Tk.__init__(self)
       self.title("RedRect")
       CvRedRect(self).pack(expand=1, fill="both")

class CvRedRect(Canvas):
   def __init__(self, root):
       Canvas.__init__(self, root, bg="white")
       self.bind("<Configure>", self.paint)
       self.r = self.create_rectangle(0,0,0,0, outline="red")
       self.txt1 = self.create_text(30,30,text="")
       self.txt2 = self.create_text(30,60,text="")
   def paint(self, event):
       w,h=event.width, event.height
       self.itemconfig(self.txt1, text=str(w))
       self.itemconfig(self.txt2, text=str(h))
       self.coords(self.r,2,2,w-3,h-3)

if __name__ == "__main__":
   RedRect().mainloop()   # Marilyn's idea, nice!

Now my questions:

(1) Why do I have to use in
       self.coords(self.r,2,2,w-3,h-3)
   the constant(s)  2 and 3? One would expect, that
   something like
       self.coords(self.r,0,0,w-1,h-1)
   or
       self.coords(self.r,0,0,w,h)
   would work. But no, these rectangles are outside
   the visible prtion of the canvas. Moreover: is
   there some property ("option") of canvas which
   represents this sort of "inset"?

   Shortly: why doesn't appear the whole Canvas on the
   screen?

(2) Why does apparently
       self.geometry("400x300+50+50")
   determine the size of the canvas packed into the
   Tk() - window and not the sizue of the Tk-window
   it*self*, which is 408*327 pixels?

Minor questions, maybe. But I find it annoying that I need
*experiment* with canvas and rectangle in order to get it as
wanted. I'd prefer to compute it over trial an error.

Thanks in advance

Gregor



More information about the Tutor mailing list