Python Tkinter保存点坐标

时间:2017-07-25 19:07:06

标签: python user-interface tkinter

我有一个Python GUI我正在创建w / Tkinter。这是我第一次使用GUI和Tkinter工作,所以请耐心等待我!

我正在尝试将点位置/坐标保存到变量中。在我到目前为止的代码中,我上传了一个BMP图像,当我点击图像上的某个点时,标记了该点。我试图弄清楚如何将前三个点的坐标保存到变量中,我将用它来创建一个与这三个点相交的最佳拟合椭圆。我该怎么做呢?

这是我的代码:

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

1 个答案:

答案 0 :(得分:1)

要保存位置,请使用列表。我们可以将元组存储在类属性中。

让我们将$houses = Houses::query() ->with('pictures') ->orderBy('sort', 'ASC') ->take('6') ->get(); 函数移到类中,这样我们就可以更轻松地利用类属性。

然后我们在这里删除imgClick方法作为冗余。

还可以添加一个计数器,这样当我们达到3次点击时,程序会停止标记地图并删除按钮绑定。

新类属性:

init_window 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 值。

看看这段代码,让我知道你的想法:

self.pos