Calculating disctance between 2 coordinates using click events





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I can display an image on my panel, what I need is to click on 2 spots in the picture and calculate the distance between them. I am having trouble with the event handler and how to use it similarly to a scanner in Java. For example, if I run the program and click once somewhere in the image, it runs all 3 methods at once which leads to give an error.



root = Tk()

img = ImageTk.PhotoImage(Image.open("target.PNG"))
#img = cv2.imread("target.PNG")
panel = Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")

def leftClick(event):
global x0,y0
x0 = event.x
y0 = event.y
return x0, y0

panel.bind("<Button-1>", leftClick)

def rightClick(event):
global x1,y1
x1 = event.x
y1 = event.y
return x1, y1

panel.bind("<Button-1>", rightClick)

def getDistance(event):
distance = math.sqrt( ((x0-x1)**2)+((y0-y1)**2) )
print(distance)

panel.bind("<Button-1>", getDistance)
root.mainloop()


What I'm looking for is to execute each step once at a time. The final step to calculate the distance can be done outside a method it doesn't really matter. I just need to get the coordinates to work first. Please let me know how I could proceed to solve this.










share|improve this question

























  • The Pythagoras theorem is mandatory to have two components. You can calculate the measurement value in each second click. You must collect the clicked points in a list, and then scroll the list to the left each click.

    – dsgdfg
    Jan 3 at 5:20


















1















I can display an image on my panel, what I need is to click on 2 spots in the picture and calculate the distance between them. I am having trouble with the event handler and how to use it similarly to a scanner in Java. For example, if I run the program and click once somewhere in the image, it runs all 3 methods at once which leads to give an error.



root = Tk()

img = ImageTk.PhotoImage(Image.open("target.PNG"))
#img = cv2.imread("target.PNG")
panel = Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")

def leftClick(event):
global x0,y0
x0 = event.x
y0 = event.y
return x0, y0

panel.bind("<Button-1>", leftClick)

def rightClick(event):
global x1,y1
x1 = event.x
y1 = event.y
return x1, y1

panel.bind("<Button-1>", rightClick)

def getDistance(event):
distance = math.sqrt( ((x0-x1)**2)+((y0-y1)**2) )
print(distance)

panel.bind("<Button-1>", getDistance)
root.mainloop()


What I'm looking for is to execute each step once at a time. The final step to calculate the distance can be done outside a method it doesn't really matter. I just need to get the coordinates to work first. Please let me know how I could proceed to solve this.










share|improve this question

























  • The Pythagoras theorem is mandatory to have two components. You can calculate the measurement value in each second click. You must collect the clicked points in a list, and then scroll the list to the left each click.

    – dsgdfg
    Jan 3 at 5:20














1












1








1








I can display an image on my panel, what I need is to click on 2 spots in the picture and calculate the distance between them. I am having trouble with the event handler and how to use it similarly to a scanner in Java. For example, if I run the program and click once somewhere in the image, it runs all 3 methods at once which leads to give an error.



root = Tk()

img = ImageTk.PhotoImage(Image.open("target.PNG"))
#img = cv2.imread("target.PNG")
panel = Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")

def leftClick(event):
global x0,y0
x0 = event.x
y0 = event.y
return x0, y0

panel.bind("<Button-1>", leftClick)

def rightClick(event):
global x1,y1
x1 = event.x
y1 = event.y
return x1, y1

panel.bind("<Button-1>", rightClick)

def getDistance(event):
distance = math.sqrt( ((x0-x1)**2)+((y0-y1)**2) )
print(distance)

panel.bind("<Button-1>", getDistance)
root.mainloop()


What I'm looking for is to execute each step once at a time. The final step to calculate the distance can be done outside a method it doesn't really matter. I just need to get the coordinates to work first. Please let me know how I could proceed to solve this.










share|improve this question
















I can display an image on my panel, what I need is to click on 2 spots in the picture and calculate the distance between them. I am having trouble with the event handler and how to use it similarly to a scanner in Java. For example, if I run the program and click once somewhere in the image, it runs all 3 methods at once which leads to give an error.



root = Tk()

img = ImageTk.PhotoImage(Image.open("target.PNG"))
#img = cv2.imread("target.PNG")
panel = Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")

def leftClick(event):
global x0,y0
x0 = event.x
y0 = event.y
return x0, y0

panel.bind("<Button-1>", leftClick)

def rightClick(event):
global x1,y1
x1 = event.x
y1 = event.y
return x1, y1

panel.bind("<Button-1>", rightClick)

def getDistance(event):
distance = math.sqrt( ((x0-x1)**2)+((y0-y1)**2) )
print(distance)

panel.bind("<Button-1>", getDistance)
root.mainloop()


What I'm looking for is to execute each step once at a time. The final step to calculate the distance can be done outside a method it doesn't really matter. I just need to get the coordinates to work first. Please let me know how I could proceed to solve this.







python tkinter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 5:00









eyllanesc

86.5k103564




86.5k103564










asked Jan 3 at 4:43









Alfred RothlesbergAlfred Rothlesberg

133




133













  • The Pythagoras theorem is mandatory to have two components. You can calculate the measurement value in each second click. You must collect the clicked points in a list, and then scroll the list to the left each click.

    – dsgdfg
    Jan 3 at 5:20



















  • The Pythagoras theorem is mandatory to have two components. You can calculate the measurement value in each second click. You must collect the clicked points in a list, and then scroll the list to the left each click.

    – dsgdfg
    Jan 3 at 5:20

















The Pythagoras theorem is mandatory to have two components. You can calculate the measurement value in each second click. You must collect the clicked points in a list, and then scroll the list to the left each click.

– dsgdfg
Jan 3 at 5:20





The Pythagoras theorem is mandatory to have two components. You can calculate the measurement value in each second click. You must collect the clicked points in a list, and then scroll the list to the left each click.

– dsgdfg
Jan 3 at 5:20












2 Answers
2






active

oldest

votes


















0














You Can Try This Two:



Process 1(Uses mouse left click, right click, middle(scroll) click):



The following code takes



(x0, y0) from mouse-left-click



(x1, y1) from mouse-right-click



and then prints distance between them on mouse-middle(scroll)-click



from tkinter import *
from PIL import ImageTk, Image
import math

root = Tk()

img = ImageTk.PhotoImage(Image.open("Logo.png"))
panel = Label(root, image=img)
panel.pack(side="bottom", fill="both", expand="yes")
x0 = 0
y0 = 0
x1 = 0
y1 = 0


def leftClick(event):
global x0, y0
x0 = event.x
y0 = event.y
# return [x0, y0]


panel.bind("<Button-1>", leftClick)


def rightClick(event):
global x1, y1
x1 = event.x
y1 = event.y
# return x1, y1


panel.bind("<Button-3>", rightClick)


def getDistance(event):
global x0, y0, x1, y1
distance = math.sqrt(((x0 - x1)**2)+((y0 - y1)**2))
print(distance)


panel.bind("<Button-2>", getDistance)
root.mainloop()


Process 2(Uses only mouse left click):



The following code takes



(x0, y0) from first mouse-left-click



(x1, y1) from second mouse-left-click



and then prints distance between them on third mouse-left-click



from tkinter import *
from PIL import ImageTk, Image
import math

root = Tk()

img = ImageTk.PhotoImage(Image.open("Logo.png"))
panel = Label(root, image=img)
panel.pack(side="bottom", fill="both", expand="yes")
counter = 0
x0 = 0
x1 = 0
y0 = 0
y1 = 0


def getDistance(event):
global counter, x0, y0, x1, y1
if counter == 0:
x0 = event.x
y0 = event.y
counter += 1
elif counter == 1:
x1 = event.x
y1 = event.y
counter += 1
elif counter == 2:
distance = math.sqrt(((x0 - x1)**2)+((y0 - y1)**2))
print(distance)
counter = 0


panel.bind("<Button-1>", getDistance)
root.mainloop()





share|improve this answer

































    0














    Below is a demo for count distance from a start point to a end point, which takes a DRAG operation with mouse left button.



    import tkinter as tk
    from PIL import ImageTk, Image
    import math
    start_point_x, start_point_y, end_point_x, end_point_y = 0, 0, 0, 0

    def mouse_left_down_detection(event):
    global start_point_x, start_point_y
    start_point_x = event.x
    start_point_y = event.y

    def mouse_left_release_detection(event):
    global end_point_x, end_point_y
    end_point_x = event.x
    end_point_y = event.y
    print(start_point_x, start_point_y, end_point_x, end_point_y)
    print(get_instance(start_point_x, start_point_y, end_point_x, end_point_y))

    def get_instance(x1, y1, x2, y2):
    return math.sqrt((pow(abs(x2-x1), abs(x2-x1))+pow(abs(y2-y1), abs(y2-y1))))

    image_path = "andy.jpg"
    root = tk.Tk()
    img = ImageTk.PhotoImage(Image.open(image_path))
    panel = tk.Label(root, image=img)
    # Bind event mouse left down
    panel.bind("<Button-1>", mouse_left_down_detection)
    # Bind event mouse left release and calculate distance
    panel.bind("<ButtonRelease-1>", mouse_left_release_detection)
    panel.pack(side="bottom", fill="both", expand="yes")
    root.mainloop()





    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54016457%2fcalculating-disctance-between-2-coordinates-using-click-events%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      You Can Try This Two:



      Process 1(Uses mouse left click, right click, middle(scroll) click):



      The following code takes



      (x0, y0) from mouse-left-click



      (x1, y1) from mouse-right-click



      and then prints distance between them on mouse-middle(scroll)-click



      from tkinter import *
      from PIL import ImageTk, Image
      import math

      root = Tk()

      img = ImageTk.PhotoImage(Image.open("Logo.png"))
      panel = Label(root, image=img)
      panel.pack(side="bottom", fill="both", expand="yes")
      x0 = 0
      y0 = 0
      x1 = 0
      y1 = 0


      def leftClick(event):
      global x0, y0
      x0 = event.x
      y0 = event.y
      # return [x0, y0]


      panel.bind("<Button-1>", leftClick)


      def rightClick(event):
      global x1, y1
      x1 = event.x
      y1 = event.y
      # return x1, y1


      panel.bind("<Button-3>", rightClick)


      def getDistance(event):
      global x0, y0, x1, y1
      distance = math.sqrt(((x0 - x1)**2)+((y0 - y1)**2))
      print(distance)


      panel.bind("<Button-2>", getDistance)
      root.mainloop()


      Process 2(Uses only mouse left click):



      The following code takes



      (x0, y0) from first mouse-left-click



      (x1, y1) from second mouse-left-click



      and then prints distance between them on third mouse-left-click



      from tkinter import *
      from PIL import ImageTk, Image
      import math

      root = Tk()

      img = ImageTk.PhotoImage(Image.open("Logo.png"))
      panel = Label(root, image=img)
      panel.pack(side="bottom", fill="both", expand="yes")
      counter = 0
      x0 = 0
      x1 = 0
      y0 = 0
      y1 = 0


      def getDistance(event):
      global counter, x0, y0, x1, y1
      if counter == 0:
      x0 = event.x
      y0 = event.y
      counter += 1
      elif counter == 1:
      x1 = event.x
      y1 = event.y
      counter += 1
      elif counter == 2:
      distance = math.sqrt(((x0 - x1)**2)+((y0 - y1)**2))
      print(distance)
      counter = 0


      panel.bind("<Button-1>", getDistance)
      root.mainloop()





      share|improve this answer






























        0














        You Can Try This Two:



        Process 1(Uses mouse left click, right click, middle(scroll) click):



        The following code takes



        (x0, y0) from mouse-left-click



        (x1, y1) from mouse-right-click



        and then prints distance between them on mouse-middle(scroll)-click



        from tkinter import *
        from PIL import ImageTk, Image
        import math

        root = Tk()

        img = ImageTk.PhotoImage(Image.open("Logo.png"))
        panel = Label(root, image=img)
        panel.pack(side="bottom", fill="both", expand="yes")
        x0 = 0
        y0 = 0
        x1 = 0
        y1 = 0


        def leftClick(event):
        global x0, y0
        x0 = event.x
        y0 = event.y
        # return [x0, y0]


        panel.bind("<Button-1>", leftClick)


        def rightClick(event):
        global x1, y1
        x1 = event.x
        y1 = event.y
        # return x1, y1


        panel.bind("<Button-3>", rightClick)


        def getDistance(event):
        global x0, y0, x1, y1
        distance = math.sqrt(((x0 - x1)**2)+((y0 - y1)**2))
        print(distance)


        panel.bind("<Button-2>", getDistance)
        root.mainloop()


        Process 2(Uses only mouse left click):



        The following code takes



        (x0, y0) from first mouse-left-click



        (x1, y1) from second mouse-left-click



        and then prints distance between them on third mouse-left-click



        from tkinter import *
        from PIL import ImageTk, Image
        import math

        root = Tk()

        img = ImageTk.PhotoImage(Image.open("Logo.png"))
        panel = Label(root, image=img)
        panel.pack(side="bottom", fill="both", expand="yes")
        counter = 0
        x0 = 0
        x1 = 0
        y0 = 0
        y1 = 0


        def getDistance(event):
        global counter, x0, y0, x1, y1
        if counter == 0:
        x0 = event.x
        y0 = event.y
        counter += 1
        elif counter == 1:
        x1 = event.x
        y1 = event.y
        counter += 1
        elif counter == 2:
        distance = math.sqrt(((x0 - x1)**2)+((y0 - y1)**2))
        print(distance)
        counter = 0


        panel.bind("<Button-1>", getDistance)
        root.mainloop()





        share|improve this answer




























          0












          0








          0







          You Can Try This Two:



          Process 1(Uses mouse left click, right click, middle(scroll) click):



          The following code takes



          (x0, y0) from mouse-left-click



          (x1, y1) from mouse-right-click



          and then prints distance between them on mouse-middle(scroll)-click



          from tkinter import *
          from PIL import ImageTk, Image
          import math

          root = Tk()

          img = ImageTk.PhotoImage(Image.open("Logo.png"))
          panel = Label(root, image=img)
          panel.pack(side="bottom", fill="both", expand="yes")
          x0 = 0
          y0 = 0
          x1 = 0
          y1 = 0


          def leftClick(event):
          global x0, y0
          x0 = event.x
          y0 = event.y
          # return [x0, y0]


          panel.bind("<Button-1>", leftClick)


          def rightClick(event):
          global x1, y1
          x1 = event.x
          y1 = event.y
          # return x1, y1


          panel.bind("<Button-3>", rightClick)


          def getDistance(event):
          global x0, y0, x1, y1
          distance = math.sqrt(((x0 - x1)**2)+((y0 - y1)**2))
          print(distance)


          panel.bind("<Button-2>", getDistance)
          root.mainloop()


          Process 2(Uses only mouse left click):



          The following code takes



          (x0, y0) from first mouse-left-click



          (x1, y1) from second mouse-left-click



          and then prints distance between them on third mouse-left-click



          from tkinter import *
          from PIL import ImageTk, Image
          import math

          root = Tk()

          img = ImageTk.PhotoImage(Image.open("Logo.png"))
          panel = Label(root, image=img)
          panel.pack(side="bottom", fill="both", expand="yes")
          counter = 0
          x0 = 0
          x1 = 0
          y0 = 0
          y1 = 0


          def getDistance(event):
          global counter, x0, y0, x1, y1
          if counter == 0:
          x0 = event.x
          y0 = event.y
          counter += 1
          elif counter == 1:
          x1 = event.x
          y1 = event.y
          counter += 1
          elif counter == 2:
          distance = math.sqrt(((x0 - x1)**2)+((y0 - y1)**2))
          print(distance)
          counter = 0


          panel.bind("<Button-1>", getDistance)
          root.mainloop()





          share|improve this answer















          You Can Try This Two:



          Process 1(Uses mouse left click, right click, middle(scroll) click):



          The following code takes



          (x0, y0) from mouse-left-click



          (x1, y1) from mouse-right-click



          and then prints distance between them on mouse-middle(scroll)-click



          from tkinter import *
          from PIL import ImageTk, Image
          import math

          root = Tk()

          img = ImageTk.PhotoImage(Image.open("Logo.png"))
          panel = Label(root, image=img)
          panel.pack(side="bottom", fill="both", expand="yes")
          x0 = 0
          y0 = 0
          x1 = 0
          y1 = 0


          def leftClick(event):
          global x0, y0
          x0 = event.x
          y0 = event.y
          # return [x0, y0]


          panel.bind("<Button-1>", leftClick)


          def rightClick(event):
          global x1, y1
          x1 = event.x
          y1 = event.y
          # return x1, y1


          panel.bind("<Button-3>", rightClick)


          def getDistance(event):
          global x0, y0, x1, y1
          distance = math.sqrt(((x0 - x1)**2)+((y0 - y1)**2))
          print(distance)


          panel.bind("<Button-2>", getDistance)
          root.mainloop()


          Process 2(Uses only mouse left click):



          The following code takes



          (x0, y0) from first mouse-left-click



          (x1, y1) from second mouse-left-click



          and then prints distance between them on third mouse-left-click



          from tkinter import *
          from PIL import ImageTk, Image
          import math

          root = Tk()

          img = ImageTk.PhotoImage(Image.open("Logo.png"))
          panel = Label(root, image=img)
          panel.pack(side="bottom", fill="both", expand="yes")
          counter = 0
          x0 = 0
          x1 = 0
          y0 = 0
          y1 = 0


          def getDistance(event):
          global counter, x0, y0, x1, y1
          if counter == 0:
          x0 = event.x
          y0 = event.y
          counter += 1
          elif counter == 1:
          x1 = event.x
          y1 = event.y
          counter += 1
          elif counter == 2:
          distance = math.sqrt(((x0 - x1)**2)+((y0 - y1)**2))
          print(distance)
          counter = 0


          panel.bind("<Button-1>", getDistance)
          root.mainloop()






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 3 at 6:00

























          answered Jan 3 at 5:45









          Partho63Partho63

          2,01211123




          2,01211123

























              0














              Below is a demo for count distance from a start point to a end point, which takes a DRAG operation with mouse left button.



              import tkinter as tk
              from PIL import ImageTk, Image
              import math
              start_point_x, start_point_y, end_point_x, end_point_y = 0, 0, 0, 0

              def mouse_left_down_detection(event):
              global start_point_x, start_point_y
              start_point_x = event.x
              start_point_y = event.y

              def mouse_left_release_detection(event):
              global end_point_x, end_point_y
              end_point_x = event.x
              end_point_y = event.y
              print(start_point_x, start_point_y, end_point_x, end_point_y)
              print(get_instance(start_point_x, start_point_y, end_point_x, end_point_y))

              def get_instance(x1, y1, x2, y2):
              return math.sqrt((pow(abs(x2-x1), abs(x2-x1))+pow(abs(y2-y1), abs(y2-y1))))

              image_path = "andy.jpg"
              root = tk.Tk()
              img = ImageTk.PhotoImage(Image.open(image_path))
              panel = tk.Label(root, image=img)
              # Bind event mouse left down
              panel.bind("<Button-1>", mouse_left_down_detection)
              # Bind event mouse left release and calculate distance
              panel.bind("<ButtonRelease-1>", mouse_left_release_detection)
              panel.pack(side="bottom", fill="both", expand="yes")
              root.mainloop()





              share|improve this answer




























                0














                Below is a demo for count distance from a start point to a end point, which takes a DRAG operation with mouse left button.



                import tkinter as tk
                from PIL import ImageTk, Image
                import math
                start_point_x, start_point_y, end_point_x, end_point_y = 0, 0, 0, 0

                def mouse_left_down_detection(event):
                global start_point_x, start_point_y
                start_point_x = event.x
                start_point_y = event.y

                def mouse_left_release_detection(event):
                global end_point_x, end_point_y
                end_point_x = event.x
                end_point_y = event.y
                print(start_point_x, start_point_y, end_point_x, end_point_y)
                print(get_instance(start_point_x, start_point_y, end_point_x, end_point_y))

                def get_instance(x1, y1, x2, y2):
                return math.sqrt((pow(abs(x2-x1), abs(x2-x1))+pow(abs(y2-y1), abs(y2-y1))))

                image_path = "andy.jpg"
                root = tk.Tk()
                img = ImageTk.PhotoImage(Image.open(image_path))
                panel = tk.Label(root, image=img)
                # Bind event mouse left down
                panel.bind("<Button-1>", mouse_left_down_detection)
                # Bind event mouse left release and calculate distance
                panel.bind("<ButtonRelease-1>", mouse_left_release_detection)
                panel.pack(side="bottom", fill="both", expand="yes")
                root.mainloop()





                share|improve this answer


























                  0












                  0








                  0







                  Below is a demo for count distance from a start point to a end point, which takes a DRAG operation with mouse left button.



                  import tkinter as tk
                  from PIL import ImageTk, Image
                  import math
                  start_point_x, start_point_y, end_point_x, end_point_y = 0, 0, 0, 0

                  def mouse_left_down_detection(event):
                  global start_point_x, start_point_y
                  start_point_x = event.x
                  start_point_y = event.y

                  def mouse_left_release_detection(event):
                  global end_point_x, end_point_y
                  end_point_x = event.x
                  end_point_y = event.y
                  print(start_point_x, start_point_y, end_point_x, end_point_y)
                  print(get_instance(start_point_x, start_point_y, end_point_x, end_point_y))

                  def get_instance(x1, y1, x2, y2):
                  return math.sqrt((pow(abs(x2-x1), abs(x2-x1))+pow(abs(y2-y1), abs(y2-y1))))

                  image_path = "andy.jpg"
                  root = tk.Tk()
                  img = ImageTk.PhotoImage(Image.open(image_path))
                  panel = tk.Label(root, image=img)
                  # Bind event mouse left down
                  panel.bind("<Button-1>", mouse_left_down_detection)
                  # Bind event mouse left release and calculate distance
                  panel.bind("<ButtonRelease-1>", mouse_left_release_detection)
                  panel.pack(side="bottom", fill="both", expand="yes")
                  root.mainloop()





                  share|improve this answer













                  Below is a demo for count distance from a start point to a end point, which takes a DRAG operation with mouse left button.



                  import tkinter as tk
                  from PIL import ImageTk, Image
                  import math
                  start_point_x, start_point_y, end_point_x, end_point_y = 0, 0, 0, 0

                  def mouse_left_down_detection(event):
                  global start_point_x, start_point_y
                  start_point_x = event.x
                  start_point_y = event.y

                  def mouse_left_release_detection(event):
                  global end_point_x, end_point_y
                  end_point_x = event.x
                  end_point_y = event.y
                  print(start_point_x, start_point_y, end_point_x, end_point_y)
                  print(get_instance(start_point_x, start_point_y, end_point_x, end_point_y))

                  def get_instance(x1, y1, x2, y2):
                  return math.sqrt((pow(abs(x2-x1), abs(x2-x1))+pow(abs(y2-y1), abs(y2-y1))))

                  image_path = "andy.jpg"
                  root = tk.Tk()
                  img = ImageTk.PhotoImage(Image.open(image_path))
                  panel = tk.Label(root, image=img)
                  # Bind event mouse left down
                  panel.bind("<Button-1>", mouse_left_down_detection)
                  # Bind event mouse left release and calculate distance
                  panel.bind("<ButtonRelease-1>", mouse_left_release_detection)
                  panel.pack(side="bottom", fill="both", expand="yes")
                  root.mainloop()






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 3 at 6:04









                  Lau RealLau Real

                  1378




                  1378






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54016457%2fcalculating-disctance-between-2-coordinates-using-click-events%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      How to fix TextFormField cause rebuild widget in Flutter

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith