I have an image saved as image.png. The workflow of my task is this:
- Load image in Tkinter along with a "select two points" button below the image
- The user left-clicks with the mouse twice on two points of the image
- When he selects the first point, that specific point gets highlighted (say in red, or whichever color); then he selects the second point and also this second point gets highlighted
- The (x,y) coordinates of the two points are stored in global variables and will be later used
- Once the user has selected two points, a second "Done!" button appears. When this button is clicked the GUI closes. N.B I would like the two points to remain highlighted until the user clicks the closing button so that he has an idea of where he/she clicked
I managed to solve all steps, except step 3. The most similar thing that I found is to create a rectangle with canvas.create_rectangle(x,y,x+1,y+1,fill="red"), but first I would prefer a circle and second I don't manage to link the canvas to my Label
Any help would be appreciated :D
here is my code so far:
root = Tk() # create a window
frame = Frame(root) # define upper frame
middleframe = Frame(root) # define middle frame
exitFrame = Frame(root) # define exit frame
frame.pack() # pack the frame
middleframe.pack() # pack the subframe
exitFrame.pack(side = 'bottom') # pack the exit frame
# function that closes the GUI
def close_window():
root.destroy()
# load the image
img = PhotoImage(file="image.png") # save the image
panel = Label(frame, image=img) # display the image as a label
panel.grid(row=0, column=0) # pack the image
# make the user select some points
global x_Coordinates # initialize empty list for storing x-axis coordinates
global y_Coordinates # initialize empty list for storing y-axis coordinates
x_Coordinates = []
y_Coordinates = []
clicks = 0
def countClicks():
global clicks # this will use the variable to count
clicks = clicks + 1 # increment "clicks"
if clicks == 2: # if the user has selected 2 points, add a button that closes the window
exit_button = Button(exitFrame, state = "normal", text = "Done!", command = close_window) # link the closing function to the button
exit_button.grid(row=2, column=0, pady=5) # set button position with "grid"
pass
def selectPoints(): # function called when user clicks the button "select two points"
panel.bind("<Button 1>", saveCoordinates) # link the function to the left-mouse-click event
exit_button = Button (exitFrame, state = "disabled", text = "Done!", command = close_window) # link closing function to the button
exit_button.grid(row=2, column=0, pady=5) # set button position with "grid"
button_select_points.config(state = "disabled") # switch button state to "disabled"
def saveCoordinates(event): # function called when left-mouse-button is clicked
x_coordinate = event.x # save x and y coordinates selected by the user
y_coordinate = event.y
x_Coordinates.append(x_coordinate) # append to external list
y_Coordinates.append(y_coordinate) # append to external list
countClicks() # invoke function "countClicks"
button_select_points = Button(middleframe, text = "select two points", command = selectPoints) # insert button and link it to "selectPoints"
button_select_points.grid(row=1, column=0, pady=5) # set button position with "grid"
root.mainloop() # keep the GUI open