Python (压力) def getextenty(事件扩展): 全球ye ye=eventextenty.y 印刷品(ye) tkinter.messagebox.showinfo(“网格”,“网格已设置。您可以开始拾取坐标了。”) w、 绑定(“,printcoo
(压力) def getextenty(事件扩展): 全球ye ye=eventextenty.y 印刷品(ye) tkinter.messagebox.showinfo(“网格”,“网格已设置。您可以开始拾取坐标了。”) w、 绑定(“,printcoords) #压力-温度空间的坐标变换 def printcoords(事件): xmpx=xe-x0 xm=xmt/xmpx ympx=ye-y0 ym=-ymp/ympx #坐标变换 newx=(event.x-x0)*(xm)+xc newy=(事件y-y0)*(ym)+yc #将x和y坐标输出到控制台 打印(newx,newy) root.mainloop()Python (压力) def getextenty(事件扩展): 全球ye ye=eventextenty.y 印刷品(ye) tkinter.messagebox.showinfo(“网格”,“网格已设置。您可以开始拾取坐标了。”) w、 绑定(“,printcoo,python,tkinter,click,coordinates,mouse,Python,Tkinter,Click,Coordinates,Mouse,(压力) def getextenty(事件扩展): 全球ye ye=eventextenty.y 印刷品(ye) tkinter.messagebox.showinfo(“网格”,“网格已设置。您可以开始拾取坐标了。”) w、 绑定(“,printcoords) #压力-温度空间的坐标变换 def printcoords(事件): xmpx=xe-x0 xm=xmt/xmpx ympx=ye-y0 ym=-ymp/ympx #坐标变换 newx=(event.x-x0)*(xm)+xc new
我修改了代码。这段代码本身似乎可以工作,但在完整代码的上下文中却不行。如果我在最后尝试打印(x,y),则会显示错误消息
namererror:name'x'未定义
。我是否需要某种if
语句或循环,以便能够一个接一个地保存3个坐标?是全文的改编版本吗code@Lele你介意将你的修改代码复制粘贴到“编辑”下的问题上吗。我正在打电话,所以无法查看。pyfiles@Lele您只能使用w.bind绑定“”一次。重复这样做只是替换以前的绑定您是否试图让程序等待用户按下某个键?然后继续等待用户再次按下其他按钮?哇,这似乎是我想要的(我可能必须调整我的转换,但程序在其他方面很好!)我现在正在做一些其他事情,但稍后将进行测试和评论。谢谢一切正常。谢谢(只需将ympx=ye-y0
更改为ympx=y0-ye
即可翻转坐标系(但这与问题无关,哈哈)。
# 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 = ...
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()
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)
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()