diff --git a/reticule.py b/reticule.py index 9c963c0..668c619 100644 --- a/reticule.py +++ b/reticule.py @@ -22,19 +22,16 @@ class Reticule: - def __init__(self, canvas, radius_x=35, radius_y=20, color='green'): + def __init__(self, canvas, width, height, radius_x=35, radius_y=20, color='green'): self.canvas = canvas + self.width = width + self.height = height self.radius_x = radius_x self.radius_y = radius_y self.color = color self.item_ids = [] self.init_reticule() self.xy_cache = 0, 0 - self.canvas.bind( - '', - self.handle_canvas_resize, - add='+', - ) def get_reticule_coords(self, x, y): ''' @@ -42,8 +39,8 @@ class Reticule: rectangle yield coords for four lines and an oval suitable for passing to canvas.coords() method. ''' - width = int(self.canvas['width']) - height = int(self.canvas['height']) + width = self.width + height = self.height top = y - self.radius_y left = x - self.radius_x @@ -58,7 +55,7 @@ class Reticule: def init_reticule(self): append = self.item_ids.append - w, h = int(self.canvas['width']), int(self.canvas['height']) + w, h = self.width, self.height coords = self.get_reticule_coords(w >> 1, h >> 1) # Center. append(canvas.create_line(*next(coords), fill=self.color)) @@ -76,36 +73,15 @@ class Reticule: def handle_set_event(self, event): self.set_reticule(event.x, event.y) - def handle_canvas_resize(self, _event): - # We're going to assume that some other callback has updated the - # canvas values. Otherwise we could get the new width and height - # from the Configure event. - self.set_reticule(*self.xy_cache) - if __name__ == '__main__': from tkinter import * - def handle_canvas_resize(event): - # I don't know why the Tk system doesn't update these values. - canvas['width'] = event.width - canvas['height'] = event.height - # update reticule here/now? - # If the canvas window has enlarged then some of the lines will have - # to be extended. - canvas = Canvas(bg='black') - canvas.bind('', handle_canvas_resize) canvas.pack(expand=True, fill=BOTH) - canvas.update() - # Let the configure event propagate and trigger the - # handle_canvas_resize() callback to set the width - # and height values of the canvas correctly so that - # the reticule lines get drawn correctly. - - reticule = Reticule(canvas) + reticule = Reticule(canvas, 1024, 768) canvas.bind('', reticule.handle_set_event) canvas.bind('', reticule.handle_set_event)