tkinter and events
Paul Stillwell
paul at polariscomm.com
Thu Jun 10 18:56:48 EDT 1999
Ok, I am missing some critical piece of information so maybe someone out
there can help me. I want to be able to click on an object on the canvas
and reference variables within that object, but I am not sure how to do it.
When I try it, I always get the values from the last object of that type
that I created. For example, I have the following code:
from Tkinter import *
from Canvas import *
class MyTestObject:
def __init__(self, Parent, Domain, State):
self.Parent = Parent
self.Domain = Domain
self.State = State
self.Canvas = Parent
Distance = 5
self.x = 370
self.y = 30 + (Distance * Domain)
self.Begx = self.x - 320
self.Begy = self.y - 10
self.Endx = self.x + 320
self.Endy = self.y + 10
self.Box = Rectangle(Parent, self.Begx, self.Begy, self.Endx,
self.Endy)
self.text = CanvasText(Parent, self.x, self.y, text = "Domain = %x,
State = %s" % (Domain, State))
self.Canvas.bind('<Double-1>', self.DoubleClickHandler)
def DoubleClickHandler(self, event):
print "Clicked on domain %x" % self.Domain
class Demo(Frame):
def __init__(self, parent = None):
Frame.__init__(self, parent)
self.pack(fill = BOTH, expand = YES)
self.Canvas = Canvas(self, width = "7.5i", height = "6i", background
= "white", scrollregion = (0, 0, "7.5i", "100i"))
self.Canvas.ScrollY = Scrollbar(self, orient = VERTICAL)
self.Canvas['yscrollcommand'] = self.Canvas.ScrollY.set
self.Canvas.ScrollY['command'] = self.Canvas.yview
self.Canvas.ScrollY.pack(side = RIGHT, fill = Y)
self.Canvas.pack(side = RIGHT, fill = BOTH, expand = YES)
and I do the following at the Python command prompt (tester is the module I
created that has the above code):
Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> from tester import *
>>> root = Tk()
>>> app = Demo(root)
>>> o1 = MyTestObject(app.Canvas, 0x10, 50)
>>> o2 = MyTestObject(app.Canvas, 0x20, 70)
So, when I run the code and double click on o1, I get:
>>> Clicked on domain 20
which is the last object I created even though I double clicked on the first
object. What am I doing wrong?
Please help!
Paul
paul at polariscomm.com
More information about the Python-list
mailing list