0

I am trying to make a definition, what includes event (you know, to see the place of the click), and other four args. I want to call it, but I don't know how. I tried to give it a default value, but no success, I don't know what to write in the place of 'event'

My code is:

def example(event,a,b,c,d):
Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
  • Does your function use the `event` parameter? When you say "I want to call it" do you mean that you literally want to call it from your code, or do you want it to be called in response to an event? – Bryan Oakley Dec 14 '14 at 16:37
  • I would like to write a code, what check if the coordinates of a mouse click is between the given pixels, or not. And this the part of it, which compare the coordinates of the click, with the given points. a,b,c,d are the given points. I want to compare them with event.x and event.y, but i don't know how. – kriszgyurki Dec 14 '14 at 17:54

1 Answers1

0

Your bound function will already be called like callback(event) from within tkinter's system, so your def header takes one positional argument by default and is usually written def callback(event): and bound with some_widget.bind(sequence, callback), just passing the function object to bind and letting event get passed along internally.

That having been said, there are two ways to use other variables from outside within event callbacks and still use the event object too.

  1. Use lambda as a wrapper to pass along arbitrary args:
a, b, c, d = some_bbox
def on_click(event, a, b, c, d):
  print(event.x, event.y, a, b, c, d)
  # do the rest of your processing
some_widget.bind("<Button-1>", lambda event, a=a, b=b, c=c, d=d: on_click(event, a, b, c, d)
  1. Use either the global or nonlocal keyword to specify variables to take from the outer scope:
a, b, c, d = some_bbox
def on_click(event):
  # use global if a, b, c, and d exist at the module level
  global a, b, c, d
  # use nonlocal if a, b, c, and d exist within the scope of another function
  # nonlocal a, b, c, d
  print(event.x, event.y, a, b, c, d)
  # do the rest of your processing
some_widget.bind("<Button-1>", on_click)

python 3.8 tkinter 8.6