473,698 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tkinter Puzzler

I am trying to initialize a menu in the following manner:

for entry in [("Up", KeyUpDir), ("Back", KeyBackDir), ("Home", KeyHomeDir), ("Startdir", KeyStartDir), ("Root",
KeyRootDir)]:

func = entry[1]
UI.ShortBtn.men u.add_command(l abel=entry[0], command=lambda: func(None))

However, at runtime, each of the menu options binds to the *last* function
named in the list (KeyStartDir).

Explicitly loading each entry on its own line works fine:

UI........comma nd=lambda:KeyWH ATEVERDir(None)

Any ideas why the first form does not fly?
TIA,
----------------------------------------------------------------------------
Tim Daneliuk tu****@tundrawa re.com
PGP Key: http://www.tundraware.com/PGP/
Jul 18 '05 #1
4 1275
Tim Daneliuk wrote:
I am trying to initialize a menu in the following manner:

for entry in [("Up", KeyUpDir), ("Back", KeyBackDir), ("Home",
KeyHomeDir), ("Startdir", KeyStartDir), ("Root", KeyRootDir)]:

func = entry[1]
UI.ShortBtn.men u.add_command(l abel=entry[0], command=lambda:
func(None))

However, at runtime, each of the menu options binds to the *last* function
named in the list (KeyStartDir).

Explicitly loading each entry on its own line works fine:

UI........comma nd=lambda:KeyWH ATEVERDir(None)

Any ideas why the first form does not fly?


This has nothing to do with Tkinter, but only with the way nested scopes work:
to put it roughly, your "lambda: func(None)" only knows about the *name* func,
which is not actually evaluated until the button is pressed. And when the button
is pressed, the name func is bound to the last command in the loop.

To do what you want, change your code to:

for entry in (...):
UI.ShortBtn.men u.add_command(
label=entry[0],
command=lambda func=entry[1]: func(None)
)

This way, the value for the func parameter is evaluated when the function is
defined and not when it is called.

HTH
--
- Eric Brunel <eric (underscore) brunel (at) despammed (dot) com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com
Jul 18 '05 #2
Le 07 Jan 2005 05:28:31 EST, Tim Daneliuk a écrit :
I am trying to initialize a menu in the following manner:

for entry in [("Up", KeyUpDir), ("Back", KeyBackDir), ("Home", KeyHomeDir), ("Startdir", KeyStartDir), ("Root",
KeyRootDir)]:

func = entry[1]
UI.ShortBtn.men u.add_command(l abel=entry[0], command=lambda: func(None)) The problem is that you *call* the callback : the command parameter is
bound to the result of func(None)
However, at runtime, each of the menu options binds to the *last* function
named in the list (KeyStartDir).

Explicitly loading each entry on its own line works fine:

UI........comma nd=lambda:KeyWH ATEVERDir(None)

Any ideas why the first form does not fly? I would simplify the code like ;

add_cmd = UI.ShortBtn.men u.add_command
for label, func in (("Up", KeyUpDir), .... ):
add_cmd(label=l abel, command=func)

And have
def KeyUpDir(arg=No ne):
# whatever


TIA,
----------------------------------------------------------------------------
Tim Daneliuk tu****@tundrawa re.com
PGP Key: http://www.tundraware.com/PGP/

Jul 18 '05 #3
Tim Daneliuk <tu****@tundraw are.com> wrote:
I am trying to initialize a menu in the following manner:

for entry in [("Up", KeyUpDir), ("Back", KeyBackDir), ("Home",
KeyHomeDir), ("Startdir", KeyStartDir), ("Root", KeyRootDir)]:

func = entry[1]
UI.ShortBtn.men u.add_command(l abel=entry[0], command=lambda: func(None))

However, at runtime, each of the menu options binds to the *last* function
named in the list (KeyStartDir).

Explicitly loading each entry on its own line works fine:

UI........comma nd=lambda:KeyWH ATEVERDir(None)

Any ideas why the first form does not fly?


One word: late binding. Well, two, pedantically speaking;-).

The lambda you're passing as the value for 'command' is a closure: it
knows it will have to look up name 'func' in the environment in which
it's embedded -- but also that it's meant to do that lookup as late as
possible, each time it's called.

If you wanted to do the lookup just once, at the time lambda executes
and created an anonymous function rather than each time said anonymous
function is called, you could have expressed that...:
command=lambda func=func: func(None)
Here, func is a local variable (argument) of the anonymous function, and
its "default value" is set ONCE, when the anon function is created.

Back to your code, when your anon function is called, it looks up name
'func' in the surrounding environment... and there it finds it bound to
whatever it was RE-bound to the LAST time...
Point to remember: a closure looks up free-variable names in its
surrounding environment *as late as possible*, i.e., when the function
object is called; while default argument values are evaluated *at
function creation time* (when lambda or def executes, not when the
resulting function object gets called).
Alex
Jul 18 '05 #4
Tim Daneliuk wrote:
I am trying to initialize a menu in the following manner:

for entry in [("Up", KeyUpDir), ("Back", KeyBackDir), ("Home",
KeyHomeDir), ("Startdir", KeyStartDir), ("Root", KeyRootDir)]:

func = entry[1]
UI.ShortBtn.men u.add_command(l abel=entry[0], command=lambda:
func(None))

However, at runtime, each of the menu options binds to the *last* function
named in the list (KeyStartDir).

Explicitly loading each entry on its own line works fine:

UI........comma nd=lambda:KeyWH ATEVERDir(None)

Any ideas why the first form does not fly?
TIA,
----------------------------------------------------------------------------

Tim Daneliuk tu****@tundrawa re.com
PGP Key: http://www.tundraware.com/PGP/


Thanks All - great responses!

--
----------------------------------------------------------------------------
Tim Daneliuk tu****@tundrawa re.com
PGP Key: http://www.tundraware.com/PGP/
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5980
by: Josh | last post by:
Caution, newbie approaching... I'm trying to come up with a very simple Tkinter test application that consists of a window with a drop-down menu bar at the top and a grid of colored rectangles filling the remainder of the window. Mind you, this is a contrived test application to help me understand Tkinter and Python, not an actual application yet. I've trivially subclassed Tkinter.Canvas into ColorCanvas, added a bunch of ColorCanvases...
3
7030
by: srijit | last post by:
Hello, Any idea - why the following code crashes on my Win 98 machine with Python 2.3? Everytime I run this code, I have to reboot my machine. I also have Win32all-157 installed. from Tkinter import * class App:
5
1649
by: Paul Rubin | last post by:
I have a gui with a bunch of buttons, labels, the usual stuff. It uses the grid manager: gui = Frame() gui.grid() gui.Label(....).grid() # put some widgets into the gui ... # more widgets Now at the the very bottom of the gui, I want to add two more buttons, let's say "stop" and "go". I want "stop" to appear in the gui's lower
1
2972
by: syed_saqib_ali | last post by:
Please take a look at and run the code snippet shown below. It creates a canvas with vertical & Horizontal scroll-bars. If you shrink the window to smaller than the area of the canvas, the scroll-bars work as advertised. That's great. However, if you click the Left Mouse button, it calls code which expands the width of the canvas by 100 pixels. The area being viewed expands correspondingly..... BUT I DON'T WANT IT TO!!
0
3581
by: syed_saqib_ali | last post by:
Below is a simple code snippet showing a Tkinter Window bearing a canvas and 2 connected scrollbars (Vertical & Horizontal). Works fine. When you shrink/resize the window the scrollbars adjust accordingly. However, what I really want to happen is that the area of the canvas that the scrollbars show (the Scrollregion) should expand as the window grows. It doesn't currently do this. although, if the window shrinks smaller than the...
1
3596
by: Michael Yanowitz | last post by:
Hello: Below I have included a stripped down version of the GUI I am working on. It contains 2 dialog boxes - one main and one settings. It has the following problems, probably all related, that I am hoping someone knows what I am doing wrong: 1) Pressing the Settings.. Button multiple times, brings up many instances of the Settings Panel. I just want it to bring up one. Is there an easy way to do that?
3
3606
by: dwelch91 | last post by:
I'm trying unsuccessfully to do something in Tk that I though would be easy. After Googling this all day, I think I need some help. I am admittedly very novice with Tk (I started with it yesterday), so I am sure I am overlooking something simple. The basic idea is that my application will consist of a series of modal dialogs, that are chained together in "wizard" fashion. There will be some processing in between dialogs, but for the most...
8
3285
by: karthikbalaguru | last post by:
Hi, One of my python program needs tkinter to be installed to run successfully. I am using Redhat 9.0 and hence tried installing by copying the tkinter-2.2.2-36.i386.rpm alone from the CD 3 to my pc. But, it is not getting installed and is failing by throwing the below errors. Should i need to configure / install any specific files for resolving this issue ?
3
2981
by: joshdw4 | last post by:
I hate to do this, but I've thoroughly exhausted google search. Yes, it's that pesky root window and I have tried withdraw to no avail. I'm assuming this is because of the methods I'm using. I guess my question is two-fold. 1) How do I get rid of that window? 2) Any comments in general? I am just learning python (and coding with classes), so I'm sure there are things I should pound into my head before I learn bad habits. Here's the...
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7739
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.