473,399 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Tkinter.Menu() Question

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
Jul 18 '05 #1
3 1350
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
Jul 18 '05 #2
"Peter Otten" <__*******@web.de> wrote in message
news:bt*************@news.t-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
Jul 18 '05 #3
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


Jul 18 '05 #4

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

Similar topics

1
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...
1
by: midtoad | last post by:
I'm trying to display a GIF image in a label as the central area to a Tkinter GUI. The image does not appear, though a space is made for it. Why is this so? I notice that I can display a GIF...
25
by: BJörn Lindqvist | last post by:
See: http://www.wxpython.org/quotes.php. especially: "wxPython is the best and most mature cross-platform GUI toolkit, given a number of constraints. The only reason wxPython isn't the standard...
0
by: Stewart Midwinter | last post by:
I have a Tkinter app running on cygwin. It includes a Test menu item that does nothing more than fetch a directory listing and display it in a Toplevel window (I'd use a tkMessageBox showinfo...
2
by: python programming newb | last post by:
Hi all, first post. I'm new to python and tkinter. I'm trying to write a program that opens the root window with a button that then opens a toplevel window that then has it's own widgets. I...
2
by: Dustan | last post by:
I don't know if this is because of Tkinter (ie Tk) itself or the Windows default way of handling things, but when I create a very long menu (my test is shown below), the way it displays is rather...
4
by: Gigs_ | last post by:
class MenuDemo(Frame): def __init__(self, parent=None): Frame.__init__(self, parent) self.pack(expand=YES, fill=BOTH) self.createWidgets() def createWidgets(self): self.makeMenuBar()...
5
by: Kevin Walzer | last post by:
I'm having difficulty structuring a Tkinter menu entry. Here is the command in question: self.finkmenu.add_command(label='Update List of Packages',...
2
by: Doran, Harold | last post by:
I am currently reading An Intro to Tkinter (1999) by F. Lundh. This doc was published in 1999 and I wonder if there is a more recent version. I've googled a bit and this version is the one I keep...
3
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...

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.