Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tkinter get mouse coordinates on click and use them as variables

I am completely new to Python. Therefore don't get too mad at me, because I am sure that there are basic things that I am missing. Here is my problem:

I am trying to extract mouse-click coordinates from an image and use those coordinates as variables.

The code allows to import and image, from which I want to extract the coordinates. Some prompts ask the user about size and extent of the diagram, after which I would like to set up a coordinate grid by clicking the origin, and the end point on the x- and y-axes respectively. The idea is to use these 3 sets of coordinates and transform them into Pressure and Temperature coordinates through some transformation functions (see code).

# Determine the origin by clicking
# Probably with classes??
class Origin:
    def getorigin(eventorigin):
          eventorigin.x0 = eventorigin.x
          eventorigin.y0 = eventorigin.y
    #mouseclick event
    w.bind("<Button 1>",getorigin)
# What do I do here??
x0 = ...
y0 = ...

I don't really know how to assign the coordinates that I get through clicking, to a new variable that I can use later in the code.

I can print the coordinates, but since they are a function, they are local and are not usable outside of the function (as far as I could understand). So, and approach using classes might be better, but I have no idea how to do that.

Any help is appreciated.

FULL CODE (ADAPTED):

from tkinter import *
from tkinter.filedialog import askopenfilename
from PIL import Image, ImageTk
import tkinter.simpledialog

root = Tk()

#setting up a tkinter canvas
w = Canvas(root, width=1000, height=1000)
w.pack()

#adding the image
File = askopenfilename(parent=root, initialdir="./",title='Select an image')
original = Image.open(File)
original = original.resize((1000,1000)) #resize image
img = ImageTk.PhotoImage(original)
w.create_image(0, 0, image=img, anchor="nw")

#ask for pressure and temperature extent
xmt = tkinter.simpledialog.askfloat("Temperature", "degrees in x-axis")
ymp = tkinter.simpledialog.askfloat("Pressure", "bars in y-axis")

#ask for real PT values at origin
xc = tkinter.simpledialog.askfloat("Temperature", "Temperature at origin")
yc = tkinter.simpledialog.askfloat("Pressure", "Pressure at origin")

#instruction on 3 point selection to define grid
tkinter.messagebox.showinfo("Instructions", "Click: \n" 
                                            "1) Origin \n"
                                            "2) Temperature end \n"
                                            "3) Pressure end")

# From here on I have no idea how to get it to work...

# Determine the origin by clicking
def getorigin(eventorigin):
    global x0,y0
    x0 = eventorigin.x
    y0 = eventorigin.y
    print(x0,y0)
#mouseclick event
w.bind("<Button 1>",getorigin)

# Determine the extent of the figure in the x direction (Temperature)
def getextentx(eventextentx):
    global xe
    xe = eventextentx.x
    print(xe)
#mouseclick event
w.bind("<Button 1>",getextentx)

# Determine the extent of the figure in the y direction (Pressure)
def getextenty(eventextenty):
    global ye
    ye = eventextenty.y
    print(ye)
#mouseclick event
w.bind("<Button 1>",getextenty)

#message to confirm that the grid is set up
tkinter.messagebox.showinfo("Grid", "Grid is set. You can start picking coordinates.")

#Coordinate transformation into Pressure-Temperature space
def printcoords(event):
    xmpx = xe-x0
    xm = xmt/xmpx
    ympx = ye-y0
    ym = -ymp/ympx

    #coordinate transformation
    newx = (event.x-x0)*(xm)+xc
    newy = (event.y-y0)*(ym)+yc

    #outputting x and y coords to console
    print (newx,newy)
#mouseclick event
w.bind("<Button 1>",printcoords)

root.mainloop()
like image 542
rattorosa Avatar asked Feb 27 '17 00:02

rattorosa


People also ask

How do I get the XY coordinates of a mouse Python?

To determine the mouse's current position, we use the statement, pyautogui. position(). This function returns a tuple of the position of the mouse's cursor. The first value is the x-coordinate of where the mouse cursor is.

How do I find my mouse click coordinates?

The position of x-coordinate of the mouse click is found by subtracting the event's x position with the bounding rectangle's x position. The x position of the event is found using the 'clientX' property. The x position of the canvas element, i.e. the left side of the rectangle can be found using the 'left' property.

What is tkinter canvas?

A tkinter canvas can be used to draw in a window. Use this widget to draw graphs or plots. You can even use it to create graphical editors. You can draw several widgets in the canvas: arc bitmap, images, lines, rectangles, text, pieslices, ovals, polygons, ovals, polygons, and rectangles.

How do I find the coordinates of a canvas in Python?

In order to return the coordinates of an item on the Canvas, we can use the coords(item) method. It returns a list with the coordinates of the shapes in the canvas widget.


1 Answers

The easiest way is to set x, y to global, class or not doesn't matter. I didn't see your full code because I can't open zip files on my phone. So here's what I can help with your example

import tkinter as tk
def getorigin(eventorigin):
      global x,y
      x = eventorigin.x
      y = eventorigin.y
      print(x,y)

root = tk.Tk()
root.bind("<Button 1>",getorigin)
like image 165
Taku Avatar answered Oct 12 '22 02:10

Taku