[Tkinter-discuss] more info on find_closest problem

Jeff Cagle jrcagle at juno.com
Thu Jan 18 20:24:24 CET 2007


Well, I take one thing back: There was a text object at the point 
(30,10) -- a text object that was updated with mouse x,y coords every 
time a <Button-1> event fired.  When without that text object, 
find_closest() works well.  With it, find_closest gets confused again. 

It should be possible, I hope, to work around this issue by means of 
tags for the text objects.  But I'm curious: why is this happening?

Here's the code, with updated output:

[code="seems like it oughta work"]
def display(self):
        self.mainw = Tk()
        self.mainw.f = Frame(self.mainw)
        self.mainw.f.c = Canvas(self.mainw.f,width=400,height=400)
        # remove next line for success with find_closest()
        self.mainw.f.c.mouse_text = 
self.mainw.f.c.create_text(30,10,text='') 

...
        self.print_canvas()
        print "Center coords: (%.1f,%.1f) and (%.1f, %.1f)" % (x0,y0,x1,y1)
        f = mathutils.line_func((x0,y0),(x1,y1))
        g = mathutils.inv_line_func((x0,y0),(x1,y1))

        print "Objects near (%.1f,%.1f)" % (x0,y0), \
              self.mainw.f.c.find_closest(x0,y0)
        start_id = self.mainw.f.c.find_closest(x0,y0)[0]
        print "start_id: ", start_id
        start_x0,start_y0,start_x1,start_y1 = self.mainw.f.c.bbox(start_id)
        print "Bounding box near (%.1f,%.1f): (%.1f,%.1f) x (%.1f,%.1f)" % \
              (x0,y0,start_x0,start_y0,start_x1,start_y1)

        print "Objects near (%.1f,%.1f)" % (x1,y1), \
              self.mainw.f.c.find_closest(x1,y1)
        end_id = self.mainw.f.c.find_closest(x1,y1)[0]
        print "end_id: ", end_id
        end_x0,end_y0,end_x1,end_y1 = self.mainw.f.c.bbox(end_id)
        print "Bounding box near (%.1f,%.1f): (%.1f,%.1f) x (%.1f,%.1f)" % \
              (x1,y1,end_x0,end_y0,end_x1,end_y1)

        raw_input("Press <Enter> to continue...")
...
[/code]
[output="without offending text object"]
 >>> p.display()
The canvas contains
------------------
Item_ID      Type    coords
1            text    [200.0, 20.0]
2            text    [278.09907304116047, 37.825603777564567]
3            text    [340.72966684424534, 87.771835665427943]
4            text    [375.48702419272826, 159.94623188786341]
5            text    [375.48702419272826, 240.05376811213657]
6            text    [340.72966684424534, 312.22816433457206]
7            text    [278.09907304116047, 362.17439622243546]
8            text    [200.00000000000003, 380.0]
9            text    [121.90092695883955, 362.17439622243546]
10           text    [59.270333155754656, 312.22816433457206]
11           text    [24.512975807271744, 240.05376811213659]
12           text    [24.512975807271744, 159.94623188786343]
13           text    [59.270333155754628, 87.771835665428]
14           text    [121.90092695883949, 37.825603777564567]
Center coords: (278.1,37.8) and (375.5, 159.9)
Objects near (278.1,37.8) (2,)
start_id:  2
Bounding box near (278.1,37.8): (269.0,32.0) x (288.0,45.0)
Objects near (375.5,159.9) (4,)
end_id:  4
Bounding box near (375.5,159.9): (362.0,154.0) x (388.0,167.0)
Press <Enter> to continue...
[/output]

[output="with offending text object"]
 >>> p.display()
The canvas contains
------------------
Item_ID        Type    coords
1              text    [30.0, 10.0]
2              text    [200.0, 20.0]
3              text    [278.09907304116047, 37.825603777564567]
4              text    [340.72966684424534, 87.771835665427943]
5              text    [375.48702419272826, 159.94623188786341]
6              text    [375.48702419272826, 240.05376811213657]
7              text    [340.72966684424534, 312.22816433457206]
8              text    [278.09907304116047, 362.17439622243546]
9              text    [200.00000000000003, 380.0]
10             text    [121.90092695883955, 362.17439622243546]
11             text    [59.270333155754656, 312.22816433457206]
12             text    [24.512975807271744, 240.05376811213659]
13             text    [24.512975807271744, 159.94623188786343]
14             text    [59.270333155754628, 87.771835665428]
15             text    [121.90092695883949, 37.825603777564567]
Center coords: (278.1,37.8) and (375.5, 159.9)
Objects near (278.1,37.8) (1,)
start_id:  1
Bounding box near (278.1,37.8): (29.0,4.0) x (31.0,17.0)
Objects near (375.5,159.9) (1,)
end_id:  1
Bounding box near (375.5,159.9): (29.0,4.0) x (31.0,17.0)
Press <Enter> to continue...
[/output]

Thanks for any insights,
Jeff Cagle






More information about the Tkinter-discuss mailing list