栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/634249.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号