有没有一种方法可以直观地显示我单击的位置,使用 Python。类似于在 Windows 上按 CTRL 并看到圆圈
Is there a way I can visually show where I clicked, using Python. Similar to pressing CTRL on Windows and seeing circles
目前我的程序将打开一个图像,您可以在该图像上的任意位置单击它,它会 return 您单击位置的像素坐标。从这些坐标中,您可以输入一些文本,它会向您显示编辑后的图像。然而,我想知道是否有一种方法可以在视觉上帮助用户向他们显示他们点击的位置。这是我当前用于打开图像并单击它以 return 坐标的代码。我正在使用 tkinter,我知道我应该从 tkinter import * 下来,但现在已经太晚了:/.
### Function to open files ###
img_window = tkinter.PanedWindow(top, orient='vertical')
def file_opener():
global actual_img
global original_img
top.filename = filedialog.askopenfilename(initialdir="*/Images/", title ="Select an Image", filetypes=((".png", "*.png"),(".jpg", "*.jpg")))
original_img = Image.open(top.filename)
#################### CLICK LOCATION OF WHERE YOU WANT TO ADD TEXT ####################
event2canvas = lambda e, c: (c.canvasx(e.x), c.canvasy(e.y))
imgApp = tkinter.Toplevel()
# creating an img var of my original image
img = ImageTk.PhotoImage(original_img)
#setting up a tkinter canvas with scrollbars
frame = tkinter.Frame(imgApp, bd=2, relief=tkinter.SUNKEN)
frame.grid_rowconfigure(0, weight=1)
frame.grid_columnconfigure(0, weight=1)
xscroll = tkinter.Scrollbar(frame, orient=tkinter.HORIZONTAL)
xscroll.grid(row=1, column=0, sticky=tkinter.E+tkinter.W)
yscroll = tkinter.Scrollbar(frame)
yscroll.grid(row=0, column=1, sticky=tkinter.N+tkinter.S)
canvas = tkinter.Canvas(frame, bd=0, xscrollcommand=xscroll.set, yscrollcommand=yscroll.set, width = img.width(), height = img.height())
canvas.grid(row=0, column=0, sticky=tkinter.N+tkinter.S+tkinter.E+tkinter.W)
xscroll.config(command=canvas.xview)
yscroll.config(command=canvas.yview)
frame.pack(fill=tkinter.BOTH, expand = 1)
# adding the image to canvas
canvas.create_image(0,0,image=img,anchor="nw")
canvas.config(scrollregion=canvas.bbox(tkinter.ALL))
#function to be called when mouse is clicked
def returncoords(event):
global cx, cy
#outputting x and y coords to console
cx, cy = event2canvas(event, canvas)
return cx, cy
#mouseclick event
canvas.bind("<ButtonPress-1>", returncoords)
# canvas.bind("<ButtonRelease-1>", returncoords)
imgApp.title("Please select an area on Image")
imgApp.config(bg="red")
imgApp.mainloop()
这是我的解决方案:当图像被打开时,它将显示 tcross 光标,当用户单击时它会更改光标,直到用户释放。
canvas.create_image(0,0,image=img,anchor="nw")
canvas.config(cursor = "tcross")
canvas.config(scrollregion=canvas.bbox(tkinter.ALL))
# function to be called when mouse is clicked
def returnCoords(event):
global cx, cy
# change the cursor when button is held / click so user knows they selected a coordinate
canvas.config(cursor = "dotbox")
cx, cy = event2canvas(event, canvas)
return cx, cy
def returnCursor(event):
canvas.config(cursor = "arrow")
time.sleep(.7)
imgApp.destroy()
# mouseclick event / release
canvas.bind("<ButtonPress-1>", returnCoords)
canvas.bind("<ButtonRelease-1>", returnCursor)
目前我的程序将打开一个图像,您可以在该图像上的任意位置单击它,它会 return 您单击位置的像素坐标。从这些坐标中,您可以输入一些文本,它会向您显示编辑后的图像。然而,我想知道是否有一种方法可以在视觉上帮助用户向他们显示他们点击的位置。这是我当前用于打开图像并单击它以 return 坐标的代码。我正在使用 tkinter,我知道我应该从 tkinter import * 下来,但现在已经太晚了:/.
### Function to open files ###
img_window = tkinter.PanedWindow(top, orient='vertical')
def file_opener():
global actual_img
global original_img
top.filename = filedialog.askopenfilename(initialdir="*/Images/", title ="Select an Image", filetypes=((".png", "*.png"),(".jpg", "*.jpg")))
original_img = Image.open(top.filename)
#################### CLICK LOCATION OF WHERE YOU WANT TO ADD TEXT ####################
event2canvas = lambda e, c: (c.canvasx(e.x), c.canvasy(e.y))
imgApp = tkinter.Toplevel()
# creating an img var of my original image
img = ImageTk.PhotoImage(original_img)
#setting up a tkinter canvas with scrollbars
frame = tkinter.Frame(imgApp, bd=2, relief=tkinter.SUNKEN)
frame.grid_rowconfigure(0, weight=1)
frame.grid_columnconfigure(0, weight=1)
xscroll = tkinter.Scrollbar(frame, orient=tkinter.HORIZONTAL)
xscroll.grid(row=1, column=0, sticky=tkinter.E+tkinter.W)
yscroll = tkinter.Scrollbar(frame)
yscroll.grid(row=0, column=1, sticky=tkinter.N+tkinter.S)
canvas = tkinter.Canvas(frame, bd=0, xscrollcommand=xscroll.set, yscrollcommand=yscroll.set, width = img.width(), height = img.height())
canvas.grid(row=0, column=0, sticky=tkinter.N+tkinter.S+tkinter.E+tkinter.W)
xscroll.config(command=canvas.xview)
yscroll.config(command=canvas.yview)
frame.pack(fill=tkinter.BOTH, expand = 1)
# adding the image to canvas
canvas.create_image(0,0,image=img,anchor="nw")
canvas.config(scrollregion=canvas.bbox(tkinter.ALL))
#function to be called when mouse is clicked
def returncoords(event):
global cx, cy
#outputting x and y coords to console
cx, cy = event2canvas(event, canvas)
return cx, cy
#mouseclick event
canvas.bind("<ButtonPress-1>", returncoords)
# canvas.bind("<ButtonRelease-1>", returncoords)
imgApp.title("Please select an area on Image")
imgApp.config(bg="red")
imgApp.mainloop()
这是我的解决方案:当图像被打开时,它将显示 tcross 光标,当用户单击时它会更改光标,直到用户释放。
canvas.create_image(0,0,image=img,anchor="nw")
canvas.config(cursor = "tcross")
canvas.config(scrollregion=canvas.bbox(tkinter.ALL))
# function to be called when mouse is clicked
def returnCoords(event):
global cx, cy
# change the cursor when button is held / click so user knows they selected a coordinate
canvas.config(cursor = "dotbox")
cx, cy = event2canvas(event, canvas)
return cx, cy
def returnCursor(event):
canvas.config(cursor = "arrow")
time.sleep(.7)
imgApp.destroy()
# mouseclick event / release
canvas.bind("<ButtonPress-1>", returnCoords)
canvas.bind("<ButtonRelease-1>", returnCursor)