[Scilab-users] Executing scripts with functions from GUI

Samuel Gougeon sgougeon at free.fr
Mon Apr 18 21:05:17 CEST 2016


Le 18/04/2016 12:14, JLan a écrit :
> I wonder if someone can give me a hint here.
>
> I am trying to make a menu GUI that can start different scripts.
>
> This does not work properly when the scripts contain functions.
>
> A simple example. I have a script called
> aaa.sce :
> //-------------------
> disp('Script aaa')
> function afun(aa)
>      if aa=='-a'  then
>          disp('AFUN')
>      end
> endfunction
> //-------------------
> After executing this from the console:
> *-->exec('aaa.sce', -1)*
> I can obviously do:
> *
> 1
>
> Script aaa
>   
> -->afun -a
>   
> AFUN
> *
> I then clear afun and try to start this from my GUI
> maingui.sce:
> //----------------------------------------------------------
> clear afun
> function sel(al)
>      disp(al);
>      if al==1 then
>        
>      exec('aaa.sce',-1)
>      end
>      if al==2 then
>           exec('bbb.sce',-1)
>      end
>      if al==3 then
>           disp('D_Spare')
>      end
>      set(hl, 'value', []);
> endfunction
>
> ctrw=figure(...
>     'figure_position',[-1,-1],...
>     'figure_size', [620,400],...
>     'figure_name', '',...
>     'menubar_visible','off',...
>     'toolbar_visible','off');
> ht1=uicontrol(ctrw,...
>     'style','text',...
>     'position', [10 310 100 30],...
>     'string',' MAIN MENU');
> hl=uicontrol(ctrw,'style','listbox', ...
>     'position', [10 10 550 280],...
>     'callback','sel(get(hl,''value''))');
>
> set(hl, 'string',"...
>      Run aaa|...
>      Run bbb|...
>      spare");
> //-----------------------------------------
>
> When running
> *-->exec('maingui.sce', -1)*
>   then clicking aaa and trying to run afun I get:
> *
>    
>      1.
>   
>   Script aaa
>
> -->afun -a
>    !--error 4
> Undefined variable: afun*
>
> I have tried to define afun as global, it has an effect but I still don't
> get the same response.
afun() is defined in sel(), and so is local to sel(). It is cleared when 
leaving sel(). To prevent this, you may return it as "afun" in the 
environment calling sel() with:
afun = return(afun)
at the end of sel(), before leaving it.

https://help.scilab.org/docs/5.5.2/en_US/return.html

HTH
Samuel




More information about the users mailing list