i am trying to make a cropping feature in GUI. currently i can allow the user to draw a rectangle and perform a crop of the image on the canvas when the user unreleases the mouse. Once the rectangle is make i am trying to get the feature where the user can select one of the 4 lines of the rectangle and move it across the canvas thus changing the size of the crop box and cropping accordingly. this is what i have so far:
#these three functions draw the rectangle with end crop finding the start and end coordinates to perform the crop on
def startCrop(event):#gets coordinates when mouse clicked to begin crop
global cropX
cropX =event.x
global cropY
cropY = event.y
def drawRect(event):#draws rectangle of crop box
panelB.delete("crop_rect")
panelB.create_rectangle(cropX, cropY, event.x, event.y, outline='red', tag="crop_rect")
def endCrop(event): #gets dimensions and coordinates of rectangle crop box to performs crop on, performs crop and renders crop in panel C
global cropX, cropY, endX, endY, croppedImg, thres, newHeight, croppedArray,ratio, newHeight,height,topLine,bottomLine,leftLine,rightLine
cropXtemp = min(cropX,event.x)
endX = max(cropX, event.x)
cropYtemp = min(cropY, event.y)
endY = max(cropY, event.y)
cropX = cropXtemp
cropY = cropYtemp
panelB.delete("crop_rect")
topLine = panelB.create_line(cropX,cropY,endX,cropY,fill='blue',width=2)
rightLine = panelB.create_line(endX,cropY,endX,endY,fill='blue',width=2)
bottomLine=panelB.create_line(cropX,endY,endX,endY,fill='blue',width=2)
leftLine = panelB.create_line(cropX,cropY,cropX,endY,fill='blue',width=2)
croppedImg = performCrop(thres)
croppedArray = croppedImg
setPanelC(croppedImg)
#these functions are my attempt of trying to selecting the lines and then moving them and adjust the other lines
def select_line(event):
# Set the selected line's color to red
panelB.itemconfig(event.widget, fill="red")
# Store the starting coordinates of the selected line
event.widget.start_coords = (event.x, event.y)
def move_line(event):
# Calculate the change in x and y coordinates
dx = event.x - event.widget.start_coords[0]
dy = event.y - event.widget.start_coords[1]
# Move the selected line by the change in x and y
panelB.move(event.widget, dx, dy)
# Update the other lines' coordinates
panelB.coords(topLine, *panelB.coords(topLine)[0:2], panelB.coords(topLine)[2] + dx, panelB.coords(topLine)[3] + dy)
panelB.coords(bottomLine, *panelB.coords(bottomLine)[0:2], panelB.coords(bottomLine)[2] + dx, panelB.coords(bottomLine)[3] + dy)
panelB.coords(leftLine, panelB.coords(leftLine)[0] + dx, *panelB.coords(leftLine)[1:3], panelB.coords(leftLine)[3] + dy)
panelB.coords(rightLine, panelB.coords(rightLine)[0] + dx, *panelB.coords(rightLine)[1:3], panelB.coords(rightLine)[3] + dy)
# Update the starting coordinates of the selected line
event.widget.start_coords = (event.x, event.y)
#this takes care of drawing the rectangle
panelB.bind("<Button-1>", startCrop)
panelB.bind("<B1-Motion>", drawRect)
panelB.bind("<ButtonRelease-1>", endCrop)
#this takes care of moving the 4 lines
panelB.tag_bind(topLine, "<Button-3>", select_line)
panelB.tag_bind(rightLine, "<Button-3>", select_line)
panelB.tag_bind(bottomLine, "<Button-3>", select_line)
panelB.tag_bind(leftLine, "<Button-3>", select_line)
panelB.tag_bind(topLine, "<B3-Motion>", move_line)
panelB.tag_bind(rightLine, "<B3-Motion>", move_line)
panelB.tag_bind(bottomLine, "<B3-Motion>", move_line)
panelB.tag_bind(leftLine, "<B3-Motion>", move_line)
this draws the rectangle and then makes the 4 blue lines but when i click on them nothing happens. i expect it to change too red to indicate that it has been selected, but nothing happens. i got most of this from chatgpt, and have been looking in youtube but no luck yet. If you have experience with moving/dragging different objects/lines across the canvas, i would appreciate the help.