简体   繁体   English

如何在按下tkinter中的按钮时获取鼠标位置?

[英]how do I get mouse position while pressing a button in tkinter?

I'm trying to get the mouse position while pressing a button, but instead of getting the position of the mouse on Tkinter root, it gives me the position of the mouse on the button. 我试图在按下按钮的同时获得鼠标位置,但不是在Tkinter根上获得鼠标的位置,而是在按钮上给出了鼠标的位置。 For example, if the button is placed on 200, 200, and I press the top left of the button, it prints 0, 0 instead of 200, 200. 例如,如果按钮放在200,200上,按下按钮的左上角,它会打印0,0而不是200,200。

import Tkinter as tk

def leftclick(event):
    print("left")
    x, y = event.x, event.y
    print('{}, {}'.format(x, y))

root = tk.Tk()
add_user = tk.Button(root, height=63, width=195 ,text="sign up a user")
add_user.place(x= 20, y = 30)
root.bind("<Button-1>", leftclick)
root.mainloop()

You can call the winfo_pointerx and winfo_pointery methods of a widget to get each individual coordinate, or you can call winfo_pointerxy to get them both: 您可以调用窗口小部件的winfo_pointerxwinfo_pointery方法来获取每个单独的坐标,或者您可以调用winfo_pointerxy来获取它们:

def leftclick(event):
    x, y = event.widget.winfo_pointerxy()
    print('{}, {}'.format(x, y))

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

相关问题 如何通过在Tkinter中按下按钮来将Var显示为标签 - How do I get to display a Var into a label by pressing a button in Tkinter 如何使用tkinter和turtle获得鼠标位置? - How do I get mouse position with tkinter and turtle? 如何在tkinter中获得相对于父窗口小部件的鼠标位置? - How do I get mouse position relative to the parent widget in tkinter? 在 Python Tkinter 中按下按钮后如何清除窗口? - How do i clear a window after pressing a button in Python Tkinter? 如何将鼠标单击位置添加到tkinter中的列表? - How do I add a mouse click position to a list in tkinter? 如何在 tkinter 中获取小部件 position? - How do I get a widget position in tkinter? BeautifulSoup - 如何获取按下按钮传递的文档? - BeautifulSoup - How do I get the document that pressing a button delivers? 如何通过按下按钮从 tkinter 输入字段返回变量以在另一个 function 中使用? - How do I return a variable from a tkinter Entry field to use in another function by pressing a button? 如何在Tkinter中获取文本结束位置的行和列? - How do I get the line and column of the end position of text in Tkinter? 0如何将按钮分配给鼠标位置,然后再次按下按钮将鼠标移回该位置(用于VR / python / freepie) - 0 How do I assign a button to a mouse position and bring the mouse back to that position with another button press (for VR purposes/python/freepie)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM