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

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

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

如果我在上一条评论中说的是您要尝试的 *** 作,因为tkinter不会暂停程序以等待鼠标单击事件,则您必须这样做:每次单击鼠标按钮时,它将重新绑定该程序。

from tkinter import *from tkinter.filedialog import askopenfilenamefrom PIL import Image, ImageTkimport tkinter.simpledialogroot = Tk()#setting up a tkinter canvasw = Canvas(root, width=1000, height=1000)w.pack()#adding the imageFile = askopenfilename(parent=root, initialdir="./",title='Select an image')original = Image.open(File)original = original.resize((1000,1000)) #resize imageimg = ImageTk.PhotoImage(original)w.create_image(0, 0, image=img, anchor="nw")#ask for pressure and temperature extentxmt = tkinter.simpledialog.askfloat("Temperature", "degrees in x-axis")ymp = tkinter.simpledialog.askfloat("Pressure", "bars in y-axis")#ask for real PT values at originxc = tkinter.simpledialog.askfloat("Temperature", "Temperature at origin")yc = tkinter.simpledialog.askfloat("Pressure", "Pressure at origin")#instruction on 3 point selection to define gridtkinter.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 clickingdef getorigin(eventorigin):    global x0,y0    x0 = eventorigin.x    y0 = eventorigin.y    print(x0,y0)    w.bind("<Button 1>",getextentx)#mouseclick eventw.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 spacedef 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()


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5640119.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存