-2

I need to find a way to code the following

1.there's a geometry object that contains an array of points that are then drawn on a canvas widget (got this covered)

2.when you left click on the canvas it checks if you clicked withing a certain margin of an existing point and if that's true a point in the array is selected (got this covered in terms of searching for the point and selecting it)

3.Once selected the point will follow the mouse until the mouse button is released.

Using the Motion event on it's own doesn't seem to work as it seems the function is called over and over while the button is pressed. So I'd need to trigger the search function when the button is pressed them the move function when the button is held.

I'd be grateful for pointers.

  • The way you put it, it seems as if you already figured it out. Which part of your described solution ("trigger the search function when the button is pressed them the move function when the button is held") are you having trouble with? – Dan Getz Nov 10 '19 at 13:58
  • So here's the problem - I bind the search to button-3 let's say - I only get x,y for that event. How do I get the xy for the motion of the mouse for the move function. Basically I want it to be sort of a drag and drop behaviour. So after the point is clicked succesfully there would be something like 'while(rightMouseButton is held) pointxy = mouse xy' – Andrew Zmurowski Nov 10 '19 at 14:03
  • Oh, are you aware of/have you tried the `''` event? https://effbot.org/tkinterbook/tkinter-events-and-bindings.htm That seems to cover your situation, though don't forget it might get called by someone clicking and dragging over blank areas of the canvas. – Dan Getz Nov 10 '19 at 14:07
  • I did - so what happens is - the function is called in a loop while the button is pressed which is cool for the movement function itself. However this move function has to be called after the point is actually selected and I don't know how to call it from within the search function so that it get's the event coords. – Andrew Zmurowski Nov 10 '19 at 14:18
  • These are callbacks. You don't call them, they get called. So it's not going to be called literally within your search function. You need to keep track of the current state somewhere so that when the motion callback gets called, you know if & what you're dragging. – Dan Getz Nov 10 '19 at 14:26
  • Ah I see. So I can bind the search function to the mouse button pressed, keep the index of the selected point, then bind move function to the and bind clear the selected point to Button release? – Andrew Zmurowski Nov 10 '19 at 14:29
  • 1
    "greatful for pointers" is not an appropriate question for stackoverflow. You need to be more specific. It would help if you could provide a [mcve]. Youi've used a lot of words, but it sounds like all you're really asking is how to drag an item on the canvas. Is that correct? Have you done any research? For example, have you seen this: https://stackoverflow.com/a/6789351/7432? – Bryan Oakley Nov 10 '19 at 15:22
  • Hi Brian, thanks for the link. I'll try to phrase my questions better next time. – Andrew Zmurowski Nov 10 '19 at 19:03

1 Answers1

-1

Thanks to Dan Getz I did the following: -bind the point selection to select point and store index in self.selectedPoint -bind move function to using the self.selectedPoint to indicate the selected point in the array, then passing the events x,y coords to the array as new coords for the selected point -bind the clearSelected function to to set the self.selectedPoint to -1 thus clearing the selection

The problem I'm still having is that when moving the point I update the screen while the mouse is held which produces quite a bit of flickering. I'm wondering if there's anything I can do to prevent that.