Python Tkinter保存点坐标
我有一个pythongui,我正在创建w/Tkinter。这是我第一次与GUI和Tkinter一起工作,所以请忍受我 我试图将点位置/坐标保存到变量中。在我到目前为止的代码中,我上传了一个BMP图像,当我点击图像上的某个点时,该点被标记。我正试图找出如何将前三个点的坐标保存到变量中,在变量中我将创建一个与所有三个点相交的最佳拟合椭圆。我该怎么做呢 这是我的密码:Python Tkinter保存点坐标,python,user-interface,tkinter,Python,User Interface,Tkinter,我有一个pythongui,我正在创建w/Tkinter。这是我第一次与GUI和Tkinter一起工作,所以请忍受我 我试图将点位置/坐标保存到变量中。在我到目前为止的代码中,我上传了一个BMP图像,当我点击图像上的某个点时,该点被标记。我正试图找出如何将前三个点的坐标保存到变量中,在变量中我将创建一个与所有三个点相交的最佳拟合椭圆。我该怎么做呢 这是我的密码: from tkinter import * from PIL import Image, ImageTk class Window(
from tkinter import *
from PIL import Image, ImageTk
class Window(Frame):
# Define settings upon initialization.
def __init__(self, master=None):
# parameters that you want to send through the Frame class.
Frame.__init__(self, master)
# reference to the master widget, which is the tk window
self.master = master
# with that, we want to then run init_window, which doesn't yet
exist
self.init_window()
# Creation of init_window
def init_window(self):
# changing the title of our master widget
self.master.title("GUI")
# allowing the widget to take the full space of the root window
self.pack(fill=BOTH, expand=1)
# creating a menu instance
menu = Menu(self.master)
self.master.config(menu=menu)
# create the file object)
file = Menu(menu)
# adds a command to the menu option, calling it exit, and the
# command it runs on event is client_exit
file.add_command(label="Exit", command=self.client_exit)
# added "file" to our menu
menu.add_cascade(label="File", menu=file)
# create the file object)
analyze = Menu(menu)
# adds a command to the menu option, calling it exit, and the
# command it runs on event is client_exit
analyze.add_command(label="Region of Interest",
command=self.regionOfInterest)
# added "file" to our menu
menu.add_cascade(label="Analyze", menu=analyze)
load = Image.open("ap41.ddr.brf.sdat.bmp")
render = ImageTk.PhotoImage(load)
# labels can be text or images
img = Label(self, image=render)
img.image = render
img.place(x=0, y=0)
def regionOfInterest(self):
root.config(cursor="plus")
canvas.bind("<Button-1>", self.imgClick)
def client_exit(self):
exit()
def imgClick(self, e):
x = canvas.canvasx(e.x)
y = canvas.canvasy(e.y)
pos.append((x, y))
canvas.create_line(x - 5, y, x + 5, y, fill="red", tags="crosshair")
canvas.create_line(x, y - 5, x, y + 5, fill="red", tags="crosshair")
# root window created. Here, that would be the only window, but
# you can later have windows within windows.
root = Tk()
# loads exact size of image
imgSize = Image.open("ap41.ddr.brf.sdat.bmp")
tkimage = ImageTk.PhotoImage(imgSize)
w, h = imgSize.size
# creates canvas
canvas = Canvas(root, width=w, height=h)
canvas.create_image((w/2,h/2),image=tkimage)
canvas.pack()
# loads exact dimentsion from img size
geometry = "%dx%d" % (w,h)
root.geometry(geometry)
# creation of an instance
app = Window(root)
# mainloop
root.mainloop()
从tkinter导入*
从PIL导入图像,ImageTk
类窗口(框架):
#在初始化时定义设置。
def uuu init uuu(self,master=None):
#要通过Frame类发送的参数。
帧。\uuuu初始化(自,主)
#对主窗口小部件(tk窗口)的引用
self.master=master
#有了它,我们想运行init_窗口,但它还没有运行
存在
self.init_窗口()
#创建init_窗口
def初始窗口(自):
#更改主窗口小部件的标题
self.master.title(“GUI”)
#允许小部件占用根窗口的全部空间
self.pack(填充=两者,扩展=1)
#创建菜单实例
菜单=菜单(self.master)
self.master.config(菜单=菜单)
#创建文件对象)
文件=菜单(菜单)
#将命令添加到菜单选项中,称之为exit,然后
#它在事件上运行的命令是client\u exit
file.add\u命令(label=“Exit”,command=self.client\u Exit)
#将“文件”添加到我们的菜单中
menu.add_级联(label=“File”,menu=File)
#创建文件对象)
分析=菜单(菜单)
#将命令添加到菜单选项中,称之为exit,然后
#它在事件上运行的命令是client\u exit
analyze.add_命令(label=“Region of Interest”,
command=self.RegionFinTerest)
#将“文件”添加到我们的菜单中
menu.add_级联(label=“Analyze”,menu=Analyze)
load=Image.open(“ap41.ddr.brf.sdat.bmp”)
render=ImageTk.PhotoImage(加载)
#标签可以是文本或图像
img=标签(自我,图像=渲染)
img.image=渲染
模拟位置(x=0,y=0)
def RegionFinTerest(自身):
root.config(cursor=“plus”)
canvas.bind(“,self.imgClick)
def客户端_退出(自):
退出()
def imgClick(自我,e):
x=画布。画布x(e.x)
y=画布。画布(e.y)
位置附加((x,y))
画布。创建线(x-5,y,x+5,y,fill=“red”,tags=“crosshair”)
画布。创建线(x,y-5,x,y+5,fill=“red”,tags=“crosshair”)
#已创建根窗口。在这里,那将是唯一的窗口,但是
#以后可以在windows中使用windows。
root=Tk()
#加载图像的精确大小
imgSize=Image.open(“ap41.ddr.brf.sdat.bmp”)
tkimage=ImageTk.PhotoImage(imgSize)
w、 h=imgSize.size
#创建画布
画布=画布(根,宽度=w,高度=h)
canvas.create_image((w/2,h/2),image=tkimage)
canvas.pack()
#从img尺寸加载精确尺寸
几何体=“%dx%d”%(宽,高)
根。几何体(几何体)
#创建实例
app=窗口(根)
#主回路
root.mainloop()
要保存位置,请使用列表。我们可以将元组存储在类属性中
让我们将imgClick
函数移动到类中,以便更容易地利用类属性
然后让我们删除init_窗口
方法作为其冗余
另外,让我们添加一个计数器,当我们点击3次时,程序停止标记地图并移除按钮绑定
新类属性:
self.pos=[]
self.counter=0
然后我们修改imgClick
方法:
def imgClick(self, event):
if self.counter < 3:
x = canvas.canvasx(event.x)
y = canvas.canvasy(event.y)
self.pos.append((x, y))
print(self.pos)
canvas.create_line(x - 5, y, x + 5, y, fill="red", tags="crosshair")
canvas.create_line(x, y - 5, x, y + 5, fill="red", tags="crosshair")
self.counter += 1
else:
canvas.unbind("<Button 1>")
root.config(cursor="arrow")
self.counter = 0
您提供的代码非常有效!也感谢你一步一步的过程和你每一步的基本原理,非常有用嘿!所以我试着去做4点。我试着将它改为self.counter<4,但它仍然只允许我绘制3个点。我是不是忽略了什么?
from tkinter import *
from PIL import Image, ImageTk
class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.pos = []
self.master.title("GUI")
self.pack(fill=BOTH, expand=1)
self.counter = 0
menu = Menu(self.master)
self.master.config(menu=menu)
file = Menu(menu)
file.add_command(label="Exit", command=self.client_exit)
menu.add_cascade(label="File", menu=file)
analyze = Menu(menu)
analyze.add_command(label="Region of Interest",
command=self.regionOfInterest)
menu.add_cascade(label="Analyze", menu=analyze)
load = Image.open("ap41.ddr.brf.sdat.bmp")
render = ImageTk.PhotoImage(load)
img = Label(self, image=render)
img.image = render
img.place(x=0, y=0)
def regionOfInterest(self):
root.config(cursor="plus")
canvas.bind("<Button-1>", self.imgClick)
def client_exit(self):
exit()
def imgClick(self, event):
if self.counter < 3:
x = canvas.canvasx(event.x)
y = canvas.canvasy(event.y)
self.pos.append((x, y))
print(self.pos)
canvas.create_line(x - 5, y, x + 5, y, fill="red", tags="crosshair")
canvas.create_line(x, y - 5, x, y + 5, fill="red", tags="crosshair")
self.counter += 1
else:
canvas.unbind("<Button 1>")
root.config(cursor="arrow")
self.counter = 0
root = Tk()
imgSize = Image.open("ap41.ddr.brf.sdat.bmp")
tkimage = ImageTk.PhotoImage(imgSize)
w, h = imgSize.size
canvas = Canvas(root, width=w, height=h)
canvas.create_image((w/2,h/2),image=tkimage)
canvas.pack()
root.geometry("%dx%d"%(w,h))
app = Window(root)
root.mainloop()