| 206 | # The rest is here for testing and demonstration purposes only! |
| 207 | |
| 208 | class Icon: |
| 209 | |
| 210 | def __init__(self, name): |
| 211 | self.name = name |
| 212 | self.canvas = self.label = self.id = None |
| 213 | |
| 214 | def attach(self, canvas, x=10, y=10): |
| 215 | if canvas is self.canvas: |
| 216 | self.canvas.coords(self.id, x, y) |
| 217 | return |
| 218 | if self.canvas is not None: |
| 219 | self.detach() |
| 220 | if canvas is None: |
| 221 | return |
| 222 | label = tkinter.Label(canvas, text=self.name, |
| 223 | borderwidth=2, relief="raised") |
| 224 | id = canvas.create_window(x, y, window=label, anchor="nw") |
| 225 | self.canvas = canvas |
| 226 | self.label = label |
| 227 | self.id = id |
| 228 | label.bind("<ButtonPress>", self.press) |
| 229 | |
| 230 | def detach(self): |
| 231 | canvas = self.canvas |
| 232 | if canvas is None: |
| 233 | return |
| 234 | id = self.id |
| 235 | label = self.label |
| 236 | self.canvas = self.label = self.id = None |
| 237 | canvas.delete(id) |
| 238 | label.destroy() |
| 239 | |
| 240 | def press(self, event): |
| 241 | if dnd_start(self, event): |
| 242 | # where the pointer is relative to the label widget: |
| 243 | self.x_off = event.x |
| 244 | self.y_off = event.y |
| 245 | # where the widget is relative to the canvas: |
| 246 | self.x_orig, self.y_orig = self.canvas.coords(self.id) |
| 247 | |
| 248 | def move(self, event): |
| 249 | x, y = self.where(self.canvas, event) |
| 250 | self.canvas.coords(self.id, x, y) |
| 251 | |
| 252 | def putback(self): |
| 253 | self.canvas.coords(self.id, self.x_orig, self.y_orig) |
| 254 | |
| 255 | def where(self, canvas, event): |
| 256 | # where the corner of the canvas is relative to the screen: |
| 257 | x_org = canvas.winfo_rootx() |
| 258 | y_org = canvas.winfo_rooty() |
| 259 | # where the pointer is relative to the canvas widget: |
| 260 | x = event.x_root - x_org |
| 261 | y = event.y_root - y_org |
| 262 | # compensate for initial pointer offset |
| 263 | return x - self.x_off, y - self.y_off |
| 264 | |
| 265 | def dnd_end(self, target, event): |
no outgoing calls
no test coverage detected
searching dependent graphs…