import tkinter as tk
from PIL import ImageTk
import PIL
import math
class EPoint:
def __init__(self,x,y):
self.x=x
self.y=y
def dist(self,a):
return math.sqrt((self.x-a.x)**2+(self.y-a.y)**2)
class Application(tk.Frame):
def __init__(self, master=None):
self.corners=[]
tk.Frame.__init__(self, master)
self.pack()
self.createWidgets()
def createWidgets(self):
self.hi_there = tk.Button(self)
self.hi_there["text"] = "Hello World\n(click me)"
self.hi_there["command"] = self.say_hi
self.hi_there.pack(side="top")
self.canvas=tk.Canvas(self)
self.canvas.configure(width=480,height=360,background="#ff4444")
imgOrig=PIL.Image.open("images/1.jpg")
self.img=ImageTk.PhotoImage(imgOrig.resize((int(self.canvas['width']),int(self.canvas['height'])),resample=PIL.Image.BILINEAR))
self.canvas.bind('<1>',lambda e: self.addCorner(e.x,e.y))
self.canvas.create_image(2,2,anchor="nw",image=self.img)
self.canvas.create_line(30,30,40,40,fill="#00ff00")
self.canvas.create_line(30,40,40,30,fill="#00ff00")
self.canvas.pack()
self.QUIT = tk.Button(self, text="QUIT", fg="red", command=root.destroy)
self.QUIT.pack(side="bottom")
def say_hi(self):
print("hi there, everyone!")
def addCorner(self,x,y):
a=EPoint(x,y)
for i,c in enumerate(self.corners): # move an improperly placed point
if a.dist(c)<20:
self.corners[i]=a
return
if len(self.corners)<4: # add a new corner
self.corners.append(a)
return
index,minDist=0,float('inf') # replace the corner closest to the clicked point
for i,c in enumerate(self.corners):
if a.dist(c)<minDist:
index,minDist=i,a.dist(c)
self.corners[index]=a
def redrawGrid(self):
pass
root = tk.Tk()
app = Application(master=root)
app.mainloop()