user interface - Python Tkinter Saving Point Coordinates -
i have python gui creating w/ tkinter. first time working w/ gui's , tkinter please bear w/ me!
i trying save point locations/coordinates variables. in code have far, upload bmp image, , when click on point on image, point marked. trying figure out how save first 3 points' coordinates variables in use create best-fit ellipse intersects 3 of points. how go doing this?
here code:
from tkinter import * pil import image, imagetk class window(frame): # define settings upon initialization. def __init__(self, master=none): # parameters want send through frame class. frame.__init__(self, master) # reference master widget, tk window self.master = master # that, want run init_window, doesn't yet exist self.init_window() # creation of init_window def init_window(self): # changing title of our master widget self.master.title("gui") # allowing widget take full space of root window self.pack(fill=both, expand=1) # creating menu instance menu = menu(self.master) self.master.config(menu=menu) # create file object) file = menu(menu) # adds command menu option, calling exit, , # command runs on event client_exit file.add_command(label="exit", command=self.client_exit) # added "file" our menu menu.add_cascade(label="file", menu=file) # create file object) analyze = menu(menu) # adds command menu option, calling exit, , # command runs on event client_exit analyze.add_command(label="region of interest", command=self.regionofinterest) # added "file" our menu menu.add_cascade(label="analyze", menu=analyze) load = image.open("ap41.ddr.brf.sdat.bmp") render = imagetk.photoimage(load) # labels can 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, window, # 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 img size geometry = "%dx%d" % (w,h) root.geometry(geometry) # creation of instance app = window(root) # mainloop root.mainloop()
to save positions lets use list. can store tuples in class attribute.
lets move imgclick
function class can take advantage of class attributes easier.
then lets remove init_window
method redundant here.
also lets add counter when reach 3 clicks program stops marking map , removes button bind.
new class attributes:
self.pos = []
self.counter = 0
then modify imgclick
method:
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
you notice there print statement print out saved values of self.pos
on each click during analyze clicks.
take @ code , let me know think:
from tkinter import * 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()
Comments
Post a Comment