Problem 
Author Message
 Problem

Hi,

I have the following piece of code wherein I call appropriate function
from a list depending on the index of the active menu entry.  When I
run the program on WindowsXP the value of "i" is none. The list of
functions are maintained in a MenuList.

proc addDropdown { function FileWithPath   } {
    global MenuList
    set sourceErr ""

    if {$FileWithPath == "nofile"} {

    } else {
       catch {source $FileWithPath} sourceErr
       set errlen [string length $sourceErr]

       if {$errlen > 0} {
           puts "Error sourcing file $FileWithPath"
       }
    }

    lappend MenuList $function

    menu .menuMain$objectType -tearoff 0
    .menuMain add command -label $name \
    -command {
           global MenuList
           set i [ .menuMain$type index active]
           set function [ lindex $MenuList $i ]
           $function
    }

Quote:
}

I tried using the following binding :

bind Menu <<MenuSelect>> {
      set index [%W index active]

Quote:
}

It sets the value of "index" appropriately but i don't know how to
access this value when i click and select the option in the menu. The
menu is a drop drop menu which is invoked when i press mouse button-3.
Please help me in executing this code.

Thanks,
RG



Sat, 27 Sep 2008 14:03:36 GMT  
 Problem

Quote:

> Hi,

> I have the following piece of code wherein I call appropriate function
> from a list depending on the index of the active menu entry.  When I
> run the program on WindowsXP the value of "i" is none. The list of
> functions are maintained in a MenuList.

> proc addDropdown { function FileWithPath   } {
>     global MenuList
>     set sourceErr ""

>     if {$FileWithPath == "nofile"} {

>     } else {
>        catch {source $FileWithPath} sourceErr
>        set errlen [string length $sourceErr]

>        if {$errlen > 0} {
>            puts "Error sourcing file $FileWithPath"
>        }
>     }

>     lappend MenuList $function

>     menu .menuMain$objectType -tearoff 0
>     .menuMain add command -label $name \
>     -command {
>            global MenuList
>            set i [ .menuMain$type index active]
>            set function [ lindex $MenuList $i ]
>            $function
>     }
> }

> I tried using the following binding :

> bind Menu <<MenuSelect>> {
>       set index [%W index active]
> }

> It sets the value of "index" appropriately but i don't know how to
> access this value when i click and select the option in the menu. The
> menu is a drop drop menu which is invoked when i press mouse button-3.
> Please help me in executing this code.

It appears you don't really need the name of the active element, other
than to fetch a value out of a global array. You can eliminate the
global array and the need to fetch the active element by simply passing
the function to a procedure.

Tell us if this accomplishes the same thing:

     .menuMain add command -label $name \
       -command [list callFunction $function]
     ...
     proc callFunction {function} {
       # you can use "eval $function" if $function can contain
       # a script rather than a function call.
       $function
     }

Since you are just now learning tcl and tk, I would like to recommend
you follow the rule "always have bindings and -command arguments call a
single proc".

In your specific case you could  get away with just doing "-command
$function" but I still recommend going with a separate proc. It leaves
the door wide open to add debugging code, logging code, error catching,
all sorts of things that you may want to add later.

--
Bryan Oakley
http://www.tclscripting.com



Sat, 27 Sep 2008 20:57:20 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Problems, problems, problems

2. Eiffel Problems, Problems, Problems

3. Finn Idiom problems and Re: {rho} problem

4. Combinatorial Problem [ & a new Combinatorial Problem ]

5. Database problem/Memory problem??

6. CW 2003 - Focus problem & a select problem

7. 32bit problem - one problem solved & another found

8. GForth problem or my problem?

9. REXX DLL problems solved (was Re: Problems getting C subcommand to work)

10. HTTP-Access2 problem (was Ruby Google problem)

11. Problem with this excercise problem.

12. RSX problems and texture problems

 

 
Powered by phpBB® Forum Software