简体   繁体   English

Tkinter:仅在按下鼠标按钮时获取鼠标坐标

[英]Tkinter: Get mouse coordinates only when mouse button is pressed

I have a rather specific question in Python.我在 Python 中有一个相当具体的问题。

I know the solution for constantly recieving the mouse coordinates on a frame in Tkinter:我知道在 Tkinter 的框架上不断接收鼠标坐标的解决方案:

import Tkinter as tk
root = tk.Tk()

def motion(event):
    x, y = event.x, event.y
    print('{}, {}'.format(x, y))

root.bind('<Motion>', motion)
root.mainloop()

My question is now combining a button pressed and the motion event.我的问题现在结合按下按钮和动作事件。 In a nutshell: I want the mouse coordinates only when the button is pressed, not when it's released or not pressed.简而言之:我只在按下按钮时才想要鼠标坐标,而不是在释放或未按下时。

def ButtonPress(event):
    #This is the part, where I can't figure out, how to proceed.
    #How can I call the motion event from here.


Bt = Button(root, text='Press for coordinates!')
Bt.bind('<ButtonPress>', ButtonPress)

Regards!问候!

There are at least two very simple solutions:至少有两个非常简单的解决方案:

  • set/unset a flag on button press/release, and in your function only print the coordinates if the flag is set,在按钮按下/释放时设置/取消设置标志,如果设置了标志,则在您的函数中仅打印坐标,
  • bind/unbind the motion event on button press/release.绑定/取消绑定按钮按下/释放时的运动事件。

Setting a flag设置标志

This example uses a flag named do_capture which is set to True with a button press and set to False on a button release:此示例使用一个名为旗do_capture被设置为True按下一个按钮,并设置为False上的按钮释放:

import Tkinter as tk
root = tk.Tk()

def motion(event):
    if do_capture:
        x, y = event.x, event.y
        print('{}, {}'.format(x, y))

def capture(flag):
    global do_capture
    do_capture = flag

root.bind('<Motion>', motion)
root.bind("<ButtonPress-1>", lambda event: capture(True))
root.bind("<ButtonRelease-1>", lambda event: capture(False))

capture(False)

root.mainloop()

Bind/Unbind绑定/解绑

In this example, we bind the <Motion> event on the press of a button and unbind it on the release:在这个例子中,我们在按下按钮时绑定<Motion>事件并在释放时解除绑定:

import Tkinter as tk
root = tk.Tk()

def motion(event):
    x, y = event.x, event.y
    print('{}, {}'.format(x, y))

def capture(flag):
    if flag:
        root.bind('<Motion>', motion)
    else:
        root.unbind('<Motion>')

root.bind("<ButtonPress-1>", lambda event: capture(True))
root.bind("<ButtonRelease-1>", lambda event: capture(False))

root.mainloop()

If you don't want to store the mouse position in the motion callback and then read it again in the button's callback, you can use winfo_pointer() to get the absolute pointer position on your screen and subtract the window position winfo_root() to get the pointer position relative to the window.如果您不想在动作回调中存储鼠标位置,然后在按钮的回调中再次读取它,您可以使用winfo_pointer()获取屏幕上的绝对指针位置并减去窗口位置winfo_root()得到相对于窗口的指针位置。

Of course, you would need to catch pointer positions outside the window yourself.当然,您需要自己捕捉窗口外的指针位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取tkinter中鼠标的x和y坐标? - How to get the x and y coordinates of mouse in tkinter? 按下鼠标右键时 Python TKinter 文本小部件中的 Stardand 上下文菜单 - Stardand context menu in Python TKinter text widget when mouse right button is pressed pygame.mouse.get_pressed()报告未点击鼠标时的点击 - `pygame.mouse.get_pressed()` reports clicks when mouse is not clicked Tkinter在单击时获取鼠标坐标并将其用作变量 - Tkinter get mouse coordinates on click and use them as variables Python Tkinter:在画布上单击时获取鼠标坐标作为全局变量 - Python Tkinter: Get mouse coordinates as global variables on click on canvas Python pygame pygame.mouse.get_pressed()[0] 当我移动鼠标时的响应,而不是当鼠标在同一位置时的响应 position - Python pygame pygame.mouse.get_pressed()[0] responses when i move mouse, not when the mouse is in the same position 如何检测何时按下鼠标1按钮然后运行bash脚本? - How to detect when mouse 1 button is pressed then run a bash script? 如何仅在按下键时触发鼠标单击? 在 Python - How to trigger mouse clicks only when a key is pressed ? In Python Tkinter:获取鼠标绘制矩形的坐标 - Tkinter: getting coordinates of a mouse drawn rectangle 如何在按下tkinter中的按钮时获取鼠标位置? - how do I get mouse position while pressing a button in tkinter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM