Tkinter在单击时获取鼠标坐标并将其用作变量 [英] Tkinter get mouse coordinates on click and use them as variables
问题描述
我对Python完全陌生.因此,不要对我太生气,因为我确信我缺少一些基本的东西.这是我的问题:
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.
该代码允许导入和图像,我想从中提取坐标.一些提示会询问用户有关图的大小和范围的信息,然后我想通过分别单击x轴和y轴上的原点以及终点来建立坐标网格.想法是使用这三组坐标,并通过一些转换函数将它们转换为压力和温度坐标(请参见代码).
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.
感谢您的帮助.
完整代码(已自适应):
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()
推荐答案
如果我在上一条评论中说的是您要尝试的操作,因为tkinter不会暂停程序以等待鼠标单击事件,将必须执行此操作:每次单击鼠标按钮时,它将重新绑定
If what I said in my previous comment is what you're trying to do, since tkinter doesn't pause the program to wait for a mouse click event you will have to do this: it rebinds it every time the mouse button get clicked
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)
w.bind("<Button 1>",getextentx)
#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)
w.bind("<Button 1>",getextenty)
# Determine the extent of the figure in the y direction (Pressure)
def getextenty(eventextenty):
global ye
ye = eventextenty.y
print(ye)
tkinter.messagebox.showinfo("Grid", "Grid is set. You can start picking coordinates.")
w.bind("<Button 1>",printcoords)
#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)
root.mainloop()
这篇关于Tkinter在单击时获取鼠标坐标并将其用作变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!