Menus with Tkinter ??

Carles Sadurní Anguita isard at localhost.localdomain
Tue Sep 12 22:41:25 CEST 2000


El Mon, 11 Sep 2000 23:02:44 GMT, John Grayson va escriure:
>In article <slrn8rqs9u.4j4.thor at localhost.localdomain>,
>  irmina at ctv.es wrote:
>> On Mon,  Carles Sadurní Anguita <isard at localhost.localdomain> wrote:
[...]
>> >--------------------- This code doesn't work -------
>> >
>> >#!/usr/bin/python
>> >from Tkinter import *
>> >
>> >def ah(n):
>> >	print n
>> >
>> >root = Tk()
>> >menubar = Menu(root)
>> >menuarxiu = Menu(menubar, tearoff=0)
>> >menuarxiu.add_command(label="Ah!", command=ah(0))
>> >menuarxiu.add_command(label="Oh!", command=ah(1))
>> >menubar.add_cascade(label="arxiu", menu=menuarxiu)
>> >root.config(menu = menubar)
>> >mainloop()
>> >____________________________________________________


>> Well,
>> menuarxiu.add_command(label="Ah!", command=<reference to function>)
>>
>> "ah" is a reference to function.
>> ah(0) is a call to a function.

>Cardinal Fang! Bring me the lambda!
>
>Change:
>
>     menuarxiu.add_command(label="Ah!", command=ah(0))
>     menuarxiu.add_command(label="Oh!", command=ah(1))
>
>to:
>
>     menuarxiu.add_command(label="Ah!", command=lambda x=0: ah(x))
>     menuarxiu.add_command(label="Oh!", command=lambda x=1: ah(x))


Thank you everybody. Now I understand how to pass parametres to a function.

-- 
------------------------------------------------------------------------
Carles Sadurní Anguita    |   no había que cumplir promesas,
                          |   el cansancio era de placer,
Quita _mira_la_firma para |   las lágrimas eran de risas...
responder.                |   y parece que aún fue ayer.
                          |           Los Suaves
------------------------------------------------------------------------



More information about the Python-list mailing list