Lambda returning tuple question, multi-expression

aapost aapost at idontexist.club
Wed Mar 8 16:56:42 EST 2023


When making a UI there are a lot of binding/trace operations that need 
to occur that lead to a lot of annoying 1 use function definitions. I 
don't really see lambda use like below.

Giving 2 working lambda examples using a returned tuple to accomplish 
multiple expressions - what sort of gotchas, if any, might make the 
following bad practice if I am missing something?


Example 1:

import tkinter as tk

main = tk.Tk()
e1 = tk.Entry(master=main)
e1["state"] = "disabled"
e1.pack()
e2 = tk.Entry(master=main)
e2["state"] = "disabled"
e2.pack()
e3 = tk.Entry(master=main)
e3["state"] = "disabled"
e3.pack()

b = tk.Button(master=main, text="Enable")
b.config(
     command=lambda: (
         e1.config(state="normal"),
         e2.config(state="normal"),
         e3.config(state="normal")
     )
)
b.pack()


Example 2:

import tkinter as tk

main = tk.Tk()
l = tk.Label(master=main)
l.a = {"seconds":0}
l._updater = lambda: (
     l.a.update({"seconds": 1 + l.a["seconds"]}),
     l.config(text=l.a["seconds"]),
     l.after(ms=1000, func=l._updater)
)
l._updater()
l.pack()



More information about the Python-list mailing list