python 2.7 - How can I prevent Tkinter from passing the event object in a binding? Is there any workaround to do this? -


i want bind method event using tkinter, don't need event object passed 'bind'-method. code clarity:

from tkinter import *  root = tk()  def callback(event):     print 'clicked!'  frame = frame(root, width=100, height=100) frame.bind('<button-1>', callback) frame.pack()  root.mainloop() 

here, argument event in callback unnecessary. there solution or workaround prevent bind-method passing event object?

what mean is, can call this:

def callback2():     print 'clicked!' 

in binding? like:

frame.bind('<button-2>', callback2) 

(which not working, because bin passes event, callback2 takes no arguments).

you have 3 broad options, see (in order of increasing complexity):

  1. use ignored-by-convention variable name _:

    def callback2(_):     ...   
  2. wrap callback when bind it:

    frame.bind('...', lambda event: callback2()) 
  3. write decorator ignore event parameter you:

    def no_event(func):     @functools.wraps(func)     def wrapper(event):         return func()     return wrapper 

    then apply callback methods don't need event:

    @no_event def callback2():     ... 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -