Created
October 1, 2012 10:39
-
-
Save fnielsen/3810848 to your computer and use it in GitHub Desktop.
Tkinter mouse example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from Tkinter import * | |
root = Tk() | |
canvas = Canvas(root, width=400, height=200) | |
canvas.pack() | |
canvas.create_oval(10, 10, 110, 60, fill="grey") | |
canvas.create_text(60, 35, text="Oval") | |
canvas.create_rectangle(10, 100, 110, 150, outline="blue") | |
canvas.create_text(60, 125, text="Rectangle") | |
canvas.create_line(60, 60, 60, 100, width=3) | |
class MouseMover(): | |
def __init__(self): | |
self.item = 0; self.previous = (0, 0) | |
def select(self, event): | |
widget = event.widget # Get handle to canvas | |
# Convert screen coordinates to canvas coordinates | |
xc = widget.canvasx(event.x); yc = widget.canvasx(event.y) | |
self.item = widget.find_closest(xc, yc)[0] # ID for closest | |
self.previous = (xc, yc) | |
print((xc, yc, self.item)) | |
def drag(self, event): | |
widget = event.widget | |
xc = widget.canvasx(event.x); yc = widget.canvasx(event.y) | |
canvas.move(self.item, xc-self.previous[0], yc-self.previous[1]) | |
self.previous = (xc, yc) | |
# Get an instance of the MouseMover object | |
mm = MouseMover() | |
# Bind mouse events to methods (could also be in the constructor) | |
canvas.bind("<Button-1>", mm.select) | |
canvas.bind("<B1-Motion>", mm.drag) |
I haven't tried but https://stackoverflow.com/questions/22925599/mouse-position-python-tkinter may give you an idea
Thanks for your efforts.
…On Sat 19 Sep, 2020, 5:40 AM Finn Årup Nielsen, ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
I haven't tried but
https://stackoverflow.com/questions/22925599/mouse-position-python-tkinter
may give you an idea
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://gist.github.com/3810848#gistcomment-3459517>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AQ4G72KASFQRQJSMLOVSAL3SGPZJDANCNFSM4RSVHAAQ>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can you tell how to get x and y coordinates when i am not moving the mouse pointer and when i have not pressed any button?