×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    23

    appuifw.app.menu - how to avoid making dedicated functions for each item?

    Hello. I'm getting data from an external source and would like to build up a menu using that, with an unknown number of items, each which should ideally call the same function with different parameters.

    I can specify the same function as callback for all items, but it doesn't seem to me that any item index number etc. is passed as argument to the callback function.

    I've tried to specify my own arguments, but as you can see below, without succes:



    This is what happens when I choose item2, when defining argument as extra parameter. The extra parameter is ignored...

    Code:
    >>> def a(nr):
    ...     print nr
    ...
    >>> appuifw.app.menu=[(u'punkt1',a),(u'punkt2',a,21)]
    >>> TypeError: a() takes exactly 1 argument (0 given)
    And if I specify the function call with the parameter:

    Code:
    >>> appuifw.app.menu=[(u'punkt1',a),(u'punkt2',a(21))]
    21
    Traceback (most recent call last):
      File "<console>", line 1, in <module>
    TypeError: list of (unicode, callable) or (unicode, (unicode, callable)...) expected
    What should I do?

  2. #2
    Super Contributor
    Join Date
    Mar 2003
    Location
    Espoo, Finland
    Posts
    976

    Re: appuifw.app.menu - how to avoid making dedicated functions for each item?

    Code:
    appuifw.app.menu = [
        (u"View:",
            ((u"Back camera", lambda:set_camera(0)),
            (u"Front camera", lambda:set_camera(1)))),
        (u"About", menu_about),
        (u"Exit", cb_quit)]

  3. #3
    Registered User
    Join Date
    Feb 2006
    Location
    Helsinki, Finland
    Posts
    168

    Re: appuifw.app.menu - how to avoid making dedicated functions for each item?

    Hello. I'm getting data from an external source and would like to build up a menu using that, with an unknown number of items, each which should ideally call the same function with
    different parameters.
    If you're adding the items to the menu in a loop and using lambda, you might get some problems.

    Code:
    def callback(n):
        print n
    
    menu = []
    items = [1,2,3,4,5]
    for item in items:
        menu.append(  (u"Item "+str(item),lambda:callback(item))  )
    appuifw.app.menu = menu
    Using the above code will always print 5 by clicking any menuitem.
    This is because the body of the lambda is evaluated only when an item is selected and since the loop has already completed, the variable 'item' is 5.

    To get what you want you need to do this:

    Code:
    def callback(n):
        print n
    
    def make_lambda(n)
        def f():return callback(n)
        return f
    
    
    menu = []
    items = [1,2,3,4,5]
    for item in items:
        menu.append(  (u"Item "+str(item),make_lambda(item))  )
    appuifw.app.menu = menu
    I recently had a big headache over this issue


    Rich
    Nokia E66
    PyS60 1.4.4 final

Similar Threads

  1. Issue when trying to generate .sis file
    By rmulam in forum Symbian Tools & SDKs
    Replies: 9
    Last Post: 2009-08-13, 14:10

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×