Python Forum
[Tkinter] Programmatically creating buttons that remember their positions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Programmatically creating buttons that remember their positions
#7
When you use a variable in a lambda expression, the expression creates a closure, an expression, and the relevant context required to evaluate the expression. In this example, the lambda expression "p" creates a closure that contain the variables x and y.
Output:
Python 3.10.7 (tags/v3.10.7:6cc6b13, Sep 5 2022, 14:08:36) [MSC v.1933 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> x = 0 >>> y = 0 >>> p = lambda: print(x, y) >>> p() 0 0 >>> x = 2 >>> p() 2 0 >>> y = 3 >>> p() 2 3
A partial function does something different. When you build a partial function, any variables passed to the constructor are evaluated, and the value is passed to the constructor (just like any Python function call).
Output:
Python 3.10.7 (tags/v3.10.7:6cc6b13, Sep 5 2022, 14:08:36) [MSC v.1933 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from functools import partial >>> x = 0 >>> y = 1 >>> p = partial(print, x, y) >>> p() 0 1 >>> x = 10 >>> y = 42 >>> p() 0 1 >>>
The partial never knew about x or y. The partial() constructor was passed 0 and 1, not x and y.
Clunk_Head likes this post
Reply


Messages In This Thread
RE: Programmatically creating buttons that remember their positions - by deanhystad - Jun-22-2023, 02:51 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] How to add conversions and fix button positions. javesike1262 7 3,022 Jan-31-2021, 04:39 PM
Last Post: deanhystad
  Creating a frame with 4 command buttons Heyjoe 5 2,570 Aug-21-2020, 03:16 PM
Last Post: deanhystad
  GUI Tkinter Widget Positions punksnotdead 3 3,039 Jun-12-2019, 06:06 PM
Last Post: Yoriz
  A little idea to remember wxPython classes Sebastian_Adil 0 2,353 Mar-26-2018, 10:23 PM
Last Post: Sebastian_Adil
  Fill out form on webpage and post request programmatically ian 2 3,673 Jul-18-2017, 03:12 PM
Last Post: ian

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020