[英]Tkinter Binding a function to left mouse button not working
for some reason binding a function that finds the x and y coordinates to a mouse click event just isn't working, when I left click absolutely nothing happens, but if I call out the function in the actual code it runs fine. 由于某种原因,将找到x和y坐标的函数绑定到鼠标单击事件上是行不通的,当我左键单击时,绝对没有任何反应,但是如果我在实际代码中调用该函数,它将运行良好。 I tried other solutions posted on this site but they did not seem to help. 我尝试了此网站上发布的其他解决方案,但它们似乎无济于事。 using python 3.4 Here's my exact code: 使用python 3.4这是我的确切代码:
#Import TKINTER toolset:
from tkinter import *
#Starting variables:
#Defining mouse x and y coordinates
global mouse_x
global mouse_y
mouse_x = 0
mouse_y = 0
#Main window:
window = Tk()
#Workspace and Canvas:
wrkspace = Frame(window, bg="red",width=640,height=480)
canvas = Canvas(wrkspace,bg="white",width=640,height=480)
#Keyframe editor: (DO LATER)
#Displays mouse x and y on workspace:
def find_mouse_xy():
mouse_x = wrkspace.winfo_pointerx()
mouse_y = wrkspace.winfo_pointery()
print ("x: " + str(mouse_x))
print ("y: " + str(mouse_y))
wrkspace.bind("<Button-1>",find_mouse_xy)
wrkspace.pack()
canvas.pack()
#Runs window:
window.mainloop()
Your Canvas
is catching the mouse event, but you have it bound to your Frame
. 您的Canvas
正在捕获鼠标事件,但是您已将其绑定到Frame
。
Try binding it to the canvas instead. 尝试将其绑定到画布。
canvas.bind("<Button-1>",find_mouse_xy)
Also, you will need an argument for your find_mouse_xy()
function for the event that will be passed to it. 此外,对于将传递给它的事件,您需要为find_mouse_xy()
函数使用一个参数。
def find_mouse_xy(event):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.