Tkinter bind of class -- help

johngrayson at home.com johngrayson at home.com
Mon Apr 24 11:02:59 EDT 2000


In article <39027BE6.8DF89328 at flash.net>,
  Cindy Huyser <chuyser at flash.net> wrote:
> I've created a class that contains a number of Canvas widgets inside
> frames.  When I import this class and instantiate it in my main
program
> I put it inside a frame.
>
> What I would like to do is to get x,y coordinate information on events
> (either <Button-1> or <Motion>) that occur in the area occupied by the
> class instance so that I can display detail information in another
area
> of the main window.  At first, I tried to use bind() on the class
> instance, but received an error.  I have tried callbacks from the
frame
> in which the object resides, but find I only get event information for
> that area unoccupied by the object; I have tried looking at
information
> stored in a member variable of the class, but have found the response
> unacceptably slow.
>
> Any suggestions for solving this problem would be greatly appreciated
by
> this newbie Tkinter programmer.
>

Without seeing your code, it is a little difficult to
see what you're trying to achieve. However, here is
some sample code that does something along the lines
of your explanation:

from Tkinter import *

root = Tk()
root.title('Canvas Info')

class MakeWindows:
    def __init__(self, parent):

	f1 = Frame(root)
	Label(f1, text='x,y: ').pack(side=LEFT, padx=10)
	self.coord = StringVar()
	Entry(f1, textvariable=self.coord).pack(side=LEFT, anchor=W)
	f1.pack(padx=10, pady=10)

	self.subWin = Toplevel(root, width=500, height=500)
	self.grid = Frame(self.subWin)

	for row in range(5):
	    for col in range(5):
		f = Frame(self.grid, borderwidth=2, relief=GROOVE)
		item = Canvas(f, width=90, height=90)
		item.bind('<Motion>', lambda e, s=self, i=item:
                           s.info(e, i))
		item.pack(fill=BOTH, expand=YES)
		f.grid(row=row, col=col, sticky=NSEW)
        self.grid.pack()

    def info(self, event, item):
	print 'Canvas ID =', item
	self.coord.set('%d:%d' % (event.x, event.y))

c=MakeWindows(root)

root.mainloop()


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list