How to pass an argument to event handler in tkinte

2019-01-04 12:09发布

widget.bind('<Button-1>',callback)   # binding 

def callback(self,event)
    #do something

I need to pass an argument to callback() . The argument is a dictionary object.

6条回答
该账号已被封号
2楼-- · 2019-01-04 12:27

What about

import functools
def callback(self, event, param):
    pass
arg = 123
widget.bind("", functools.partial(callback, param=arg))
查看更多
对你真心纯属浪费
3楼-- · 2019-01-04 12:34

I think that in most cases you don't need any argument to a callback because the callback can be an instance method which can access the instance members:

from Tkinter import *

class MyObj:
    def __init__(self, arg):
        self.arg = arg

    def callback(self, event):
        print self.arg

obj = MyObj('I am Obj')
root = Tk()
btn=Button(root, text="Click")
btn.bind('<Button-1>', obj.callback)
btn.pack()
root.mainloop()

But I think the functools solution proposed by Philipp is also very nice

查看更多
相关推荐>>
4楼-- · 2019-01-04 12:40

You can also supply arguments to a callback function of a widget, given only that this widget is defined as a part of a class definition ,, i.e. consider this tiny python 2.7 program (without the parts responsible of program's execution):

import Tkinter as tk #To be able to get "tk.Button" safely
from Tkinter import *

class EXAMPLE(Frame):
    def __init__(self,master=None):
        Frame.__init__(self,master)

        #make the widgets appear to a grid of size = 2 X 2
        for row in range(2):
            self.grid_rowconfigure(row,minsize=20)
        for col in range(2):
            self.grid_columnconfigure(col,minsize=20)

        #Call our METHOD OF INTEREST
        self.AnyMethod()

    #This is our method of interest
    def AnyMethod(self):
        #arguments to be supplied
        self.arg1 = 'I am 1st argument'
        self.arg2 = 'I am 2nd argument'
        self.arg3 = 'I am 3rd argument'

        #Draw the widget, & supply its callback method
        self.widgetname=tk.Button(self.master,text="My Button",command=self.method_callback)
        self.widgetname.grid(row=0,column=0)

    #create a so-called 'shell method' to swallow the REAL callback function
    def method_callback(self):
        func_callback(self.arg1,self.arg2,self.arg3)

#Define the REAL callback function in the Module's scope
def func_callback(arg1,arg2,arg3):
    print arg1
    print arg2
    print arg3

NOTE THAT the supplied arguments must be proceeded with self.

查看更多
聊天终结者
5楼-- · 2019-01-04 12:41

Pass the callback function to the instance and call it from the instance method.

from tkinter import *

class MyClass:

    def __init__(self, my_callback, message):
        self.my_callback = my_callback
        self.message = message

    def callback(self, event):
        self.my_callback(self)

def my_callback(o):
    print(o.message)


obj = MyClass(my_callback, "I am instance of MyClass")

root = Tk()

btn=Button(root, text="Click")
btn.bind('<Button-1>', obj.callback)
btn.pack()
查看更多
够拽才男人
6楼-- · 2019-01-04 12:42

You can use lambda to define an anonymous function, such as:

data={"one": 1, "two": 2}

widget.bind("<ButtonPress-1>", lambda event, arg=data: self.on_mouse_down(event, arg))

Note that the arg passed in becomes just a normal argument that you use just like all other arguments:

def on_mouse_down(self, event, arg):
    print(arg)
查看更多
聊天终结者
7楼-- · 2019-01-04 12:49

Here's the simplest and easiest-to-read solution of them all I think:

widget.bind('<Button-1>', callback2)

def callback(self, event, custom_arg=None): #change "None" to whatever you want the default value to be
    #do something

def callback2(self, event):
    callback(event, custom_arg=something_you_set) #set custom_arg to whatever you want it to be when Button-1 is pressed
查看更多
登录 后发表回答