Reputation: 81
i try to many times to use event.x & event.y out side the function but nothing works
from tkinter import *
root = Tk()
def start_mouse_press(event):
print(f"starting mouse at {event.x, event.y}")
def stop_mouse_press(event):
print(f"stopping mouse at {event.x, event.y}")
root.bind('<ButtonPress-1>', start_mouse_press)
root.bind('<ButtonRelease-1>', stop_mouse_press)
root.mainloop()
Upvotes: 0
Views: 1079
Reputation: 7006
Whilst not specifically neat, you can use global variables to store the start position for usage later.
Consider the below example
from tkinter import *
root = Tk()
start_pos = None
def start_mouse_press(event):
global start_pos
start_pos = (event.x, event.y)
def stop_mouse_press(event):
print(f"Start Pos {start_pos}\nEnd Pos {event.x, event.y}")
root.bind("<ButtonPress-1>", start_mouse_press)
root.bind("<ButtonRelease-1>", stop_mouse_press)
The start position is recorded so that when the mouse button is released, both the start and end positions can be printed.
Upvotes: 0
Reputation: 71
You will never be able to access event outside of the function of which event is an argument.
This is a matter of scope, if you wish to use event elsewhere, pass event as an argument to the next function you wish to use it in.
Upvotes: -1