Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将光标悬停在画布上时如何显示画布坐标?_Python_Tkinter - Fatal编程技术网

Python 将光标悬停在画布上时如何显示画布坐标?

Python 将光标悬停在画布上时如何显示画布坐标?,python,tkinter,Python,Tkinter,当我将鼠标悬停在画布上时,我希望画布顶部的一些标签显示x,y坐标,如果我保持光标静止,这些坐标保持不变,但在移动光标时会发生变化。我将如何执行此操作?您可以使用回调方法并将其绑定到 导入tkinter root=tkinter.Tk() canvas=tkinter.canvas(根) canvas.pack() 已移动def(事件): canvas.itemconfigure(标记,text=“(%r,%r)”%(event.x,event.y)) canvas.bind(“,已移动) 标记=

当我将鼠标悬停在画布上时,我希望画布顶部的一些标签显示x,y坐标,如果我保持光标静止,这些坐标保持不变,但在移动光标时会发生变化。我将如何执行此操作?

您可以使用回调方法并将其绑定到

导入tkinter
root=tkinter.Tk()
canvas=tkinter.canvas(根)
canvas.pack()
已移动def(事件):
canvas.itemconfigure(标记,text=“(%r,%r)”%(event.x,event.y))
canvas.bind(“,已移动)
标记=画布。创建_文本(10,10,text=”“,anchor=“nw”)
root.mainloop()
也使用
事件。因此,当您在窗口之间切换(
+
热键)时,光标将显示正确的坐标

例如,您将光标放在画布上,
事件将跟踪它,但当您按下
+
并切换到另一个窗口时,然后稍微移动光标,然后再次将
+
放在画布上——光标的坐标将是错误的,因为
事件不会跟踪窗口之间的切换。要修复它,请使用
事件

import tkinter

root = tkinter.Tk()
canvas = tkinter.Canvas(root)
canvas.pack()

def get_coordinates(event):
    canvas.itemconfigure(tag, text='({x}, {y})'.format(x=event.x, y=event.y))

canvas.bind('<Motion>', get_coordinates)
canvas.bind('<Enter>', get_coordinates)  # handle <Alt>+<Tab> switches between windows
tag = canvas.create_text(10, 10, text='', anchor='nw')

root.mainloop()
导入tkinter
root=tkinter.Tk()
canvas=tkinter.canvas(根)
canvas.pack()
def get_坐标(事件):
canvas.itemconfigure(tag,text='({x},{y})).format(x=event.x,y=event.y))
canvas.bind(“”,获取坐标)
canvas.bind(“”,获取坐标)#handle+在窗口之间切换
tag=canvas.create_text(10,10,text='',anchor='nw')
root.mainloop()
import tkinter

root = tkinter.Tk()
canvas = tkinter.Canvas(root)
canvas.pack()

def get_coordinates(event):
    canvas.itemconfigure(tag, text='({x}, {y})'.format(x=event.x, y=event.y))

canvas.bind('<Motion>', get_coordinates)
canvas.bind('<Enter>', get_coordinates)  # handle <Alt>+<Tab> switches between windows
tag = canvas.create_text(10, 10, text='', anchor='nw')

root.mainloop()