On 08Mar2023 16:56, aapost <aap...@idontexist.club> wrote:
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?
There're no real gotchas: tuples are evaluated left to right, so you
should have things happen in the order you've expressed them (if the
order matters). What you lose with a lambda is control constructs like
loops and if-statements (well, there's the `x if foo else y` but that
gets cumbersome quickly). Once things get complicated you may want to
define a more complication function using `def`:
def callback1():
... do complicated stuff ...
b.config(command=callback1)
The only other issue, which applies across the board with GUIs and is
nothing specific to lambdas is that the GUI only renders and operates
while the main loop is running. When your callbacks do trivial stuff
you're fine. If they block (eg waiting for user input or network calls
etc) the GUI is also blocked. You need threads or other concurrent
approaches if the GUI is to stay responsive.
The flip side of that is that a concurrent context like a Thread should
not interact with the GUI directly. In things like Qt I've actually had
that mistake crash the app because the Qt framework is (was?) not thread
safe. You need to arrange that GUI actions occur in the main programme
thread. I think the same applies with tk, and is anyway probably good
practice for any GUI. It's not as hard as it sounds - typically when
something happens asynchronously you arrange to issue an "event", and
the GUI mainloop will process that as it happens - the event callback
will be fired (called) by the main loop itself and thus the callback
gets to do its thing in the main loop.
Cheers,
Cameron Simpson <c...@cskk.id.au>
--
https://mail.python.org/mailman/listinfo/python-list