Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Tkinter.Menu() Question

Reply
Thread Tools

Tkinter.Menu() Question

 
 
Adonis
Guest
Posts: n/a
 
      01-04-2004
I wish to create a popup menu from a list, when it is created they all show
the same label from the list:

Months = [0, 'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November',
'December']

def _Menu_Click(self, month):
print month

menu = Menu(fraMain, tearoff=0)

for Month in Months[1:]:
menu.add_command(label=Month, command=lambda : _Menu_Click(Month))

When the menu calls _Menu_Click(...) it always prints 'December'

Now my understanding is the when an object is created and it is replicated,
whichever of the modified replications affects all of the objects, being
that they are just mere references to the original (my wording might be off,
correct me if I am wrong). How do I avoid this for what I am trying to
achieve?

Any help is greatly appreciated.

Adonis


 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      01-04-2004
Adonis wrote:

> I wish to create a popup menu from a list, when it is created they all
> show the same label from the list:
>
> Months = [0, 'January', 'February', 'March', 'April', 'May', 'June',
> 'July', 'August', 'September', 'October', 'November',
> 'December']
>
> def _Menu_Click(self, month):
> print month
>
> menu = Menu(fraMain, tearoff=0)
>
> for Month in Months[1:]:
> menu.add_command(label=Month, command=lambda : _Menu_Click(Month))
>
> When the menu calls _Menu_Click(...) it always prints 'December'
>
> Now my understanding is the when an object is created and it is
> replicated, whichever of the modified replications affects all of the
> objects, being that they are just mere references to the original (my
> wording might be off, correct me if I am wrong). How do I avoid this for
> what I am trying to achieve?


(I made some bogus changes so I could ensure it works)

from Tkinter import *

Months = [0, 'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November',
'December']

root = Tk()

class DontCare:
def _Menu_Click(self, month):
print month
dc = DontCare()

mb = Menubutton(root, text="so what")
mb.pack()
menu = mb["menu"] = Menu(mb, tearoff=0)


for Month in Months[1:]:
menu.add_command(label=Month, command=lambda m=Month: dc._Menu_Click(m))

root.mainloop()

Note how the instance (dc) is provided with the method (_Menu_Click) in the
lambda (in your real code you will most likely have to replace dc with
self). The real trick is to provide the loop variable Month as the default
value for m in the lambda. m will be bound to the same string as Month when
the lambda is *created*. If you don't do this, the value of the variable
Month will be looked up when the lambda is *called*, which will be always
"December" after the for loop is terminated.

Peter
 
Reply With Quote
 
 
 
 
Adonis
Guest
Posts: n/a
 
      01-04-2004
"Peter Otten" <(E-Mail Removed)> wrote in message
news:bt9v6d$s3n$07$(E-Mail Removed)-online.com...
> Adonis wrote:
>
> > I wish to create a popup menu from a list, when it is created they all
> > show the same label from the list:
> >
> > Months = [0, 'January', 'February', 'March', 'April', 'May', 'June',
> > 'July', 'August', 'September', 'October', 'November',
> > 'December']
> >
> > def _Menu_Click(self, month):
> > print month
> >
> > menu = Menu(fraMain, tearoff=0)
> >
> > for Month in Months[1:]:
> > menu.add_command(label=Month, command=lambda : _Menu_Click(Month))
> >
> > When the menu calls _Menu_Click(...) it always prints 'December'
> >
> > Now my understanding is the when an object is created and it is
> > replicated, whichever of the modified replications affects all of the
> > objects, being that they are just mere references to the original (my
> > wording might be off, correct me if I am wrong). How do I avoid this for
> > what I am trying to achieve?

>
> (I made some bogus changes so I could ensure it works)
>
> from Tkinter import *
>
> Months = [0, 'January', 'February', 'March', 'April', 'May', 'June',
> 'July', 'August', 'September', 'October', 'November',
> 'December']
>
> root = Tk()
>
> class DontCare:
> def _Menu_Click(self, month):
> print month
> dc = DontCare()
>
> mb = Menubutton(root, text="so what")
> mb.pack()
> menu = mb["menu"] = Menu(mb, tearoff=0)
>
>
> for Month in Months[1:]:
> menu.add_command(label=Month, command=lambda m=Month:

dc._Menu_Click(m))
>
> root.mainloop()
>
> Note how the instance (dc) is provided with the method (_Menu_Click) in

the
> lambda (in your real code you will most likely have to replace dc with
> self). The real trick is to provide the loop variable Month as the default
> value for m in the lambda. m will be bound to the same string as Month

when
> the lambda is *created*. If you don't do this, the value of the variable
> Month will be looked up when the lambda is *called*, which will be always
> "December" after the for loop is terminated.
>
> Peter


Thanks a million, works like a charm.

Adonis


 
Reply With Quote
 
John Grayson
Guest
Posts: n/a
 
      01-07-2004
Try:

for Month in Months[1:]:
menu.add_command(label=Month, command=lambda m=Month : \
_Menu_Click(m))



Adonis wrote:

> I wish to create a popup menu from a list, when it is created they all show
> the same label from the list:
>
> Months = [0, 'January', 'February', 'March', 'April', 'May', 'June',
> 'July', 'August', 'September', 'October', 'November',
> 'December']
>
> def _Menu_Click(self, month):
> print month
>
> menu = Menu(fraMain, tearoff=0)
>
> for Month in Months[1:]:
> menu.add_command(label=Month, command=lambda : _Menu_Click(Month))
>
> When the menu calls _Menu_Click(...) it always prints 'December'
>
> Now my understanding is the when an object is created and it is replicated,
> whichever of the modified replications affects all of the objects, being
> that they are just mere references to the original (my wording might be off,
> correct me if I am wrong). How do I avoid this for what I am trying to
> achieve?
>
> Any help is greatly appreciated.
>
> Adonis
>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments