mesboomin
mesboomin

Reputation: 35

Is it possible to send a tkinter widget as a parameter to function?

Is it possible to send widgets as a parameter to any function? Such that:

l=Listbox(root,selectmode=MULTIPLE)

def SelectLB(lb):
    for i in range(lb["menu"].index("end")):
        l.select_set(i)

MyButton1=Button(root,text="MB1",command= lambda: SelectLB(l))

Thanks.

Upvotes: 0

Views: 477

Answers (1)

kabanus
kabanus

Reputation: 25895

The answer to your question is yes, you can send anything anywhere in Python (disregarding pickling and multiprocessing). It seems though you are asking if you can call the button command function with a predefined argument. This is also possible:

MyButton1=Button(root, text="MB1", command=lambda l=l: SelectLB(l))

Here you are setting the default argument ofl to the l in your current scope. This is a sort of closure, and will ensure l will be used every time the button is passed.

Upvotes: 2

Related Questions