首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tkinter在单击时获取鼠标坐标,并将其用作变量

Tkinter在单击时获取鼠标坐标,并将其用作变量
EN

Stack Overflow用户
提问于 2017-02-27 08:33:52
回答 2查看 21.9K关注 0票数 10

我对Python完全陌生。因此,不要对我太生气,因为我确信我遗漏了一些基本的东西。这就是我的问题:

我正在尝试从图像中提取鼠标单击坐标,并使用这些坐标作为变量。

代码允许导入和图像,我想从其中提取坐标。一些提示询问用户图表的大小和范围,然后我想通过分别单击x轴和y轴上的原点和终点来设置坐标网格。其思想是使用这3组坐标,并通过一些转换函数将它们转换为压力和温度坐标(参见代码)。

代码语言:javascript
复制
# 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 = ...

我真的不知道如何将通过单击获得的坐标分配给一个新的变量,以便稍后在代码中使用。

我可以打印坐标,但因为它们是一个函数,所以它们是局部的,不能在函数外部使用(据我所知)。因此,使用类的方法可能会更好,但我不知道如何做到这一点。

任何帮助都是非常感谢的。

完整代码(改编):

代码语言:javascript
复制
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()
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-02-28 03:22:19

如果我在前面的评论中说的就是你想要做的,因为tkinter不会暂停程序来等待鼠标点击事件,所以你必须这样做:每次点击鼠标按键时,它都会重新绑定它

代码语言:javascript
复制
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()
票数 8
EN

Stack Overflow用户

发布于 2017-02-27 08:49:08

最简单的方法是将x,y设置为全局,类或无关紧要。我没有看到你的完整代码,因为我无法在手机上打开zip文件。下面是我可以对您的示例提供的帮助

代码语言:javascript
复制
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)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42476040

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档