Lambda returning tuple question, multi-expression

Thomas Passin list1 at tompassin.net
Wed Mar 8 18:37:07 EST 2023


On 3/8/2023 4:56 PM, aapost wrote:
> b = tk.Button(master=main, text="Enable")
> b.config(
>      command=lambda: (
>          e1.config(state="normal"),
>          e2.config(state="normal"),
>          e3.config(state="normal")
>      )
> )

It's hard to understand what you are trying to do here.  I don't 
remember just now what .config() will return, but the lambda will return 
a tuple of *something*, probably (None, None, None).  Even if the tuple 
does contain three non-None values, config() requires named parameters, 
not a tuple.  In the course of executing the lambda, your three controls 
e1, e2, and e2 will get configured, but you could just write a function 
to do that:

def set_entries_enabled_state(enabled = True):
     state = 'normal' if enabled else 'disabled'
     for e in (e1, e2, e3):
         e.config(state=state)

def config_b_and_entries(enabled = True):
     state = 'normal' if enabled else 'disabled'
     b.config(state = state)
     set_entries_enabled_state(enabled)

This is easy to read and understand.  (I may not have remembered some Tk 
details correctly).




More information about the Python-list mailing list