0

我对 scilab 很陌生,我创建了两个 GUI(参见下面的示例),从脚本 1 调用脚本 2。但是脚本 2 中的函数似乎不起作用。任何人都可以帮忙吗?

脚本 1

'//////////
f=figure('figure_position',[0,0],'figure_size',[1250,650]);
//////////
delmenu(f.figure_id,gettext('File'))
delmenu(f.figure_id,gettext('?'))
delmenu(f.figure_id,gettext('Tools'))
toolbar(f.figure_id,'off')

handles.dummy = 0 ;

handles.exam=uicontrol(f,'unit','normalized','BackgroundColor',        [0.5,1,1],'Enable','on','FontAngle','normal','FontName','helvetica','FontSize',[14],'FontUnits','points','FontWeight','bold','ForegroundColor',[0,0.5,0],'HorizontalAlignment','center','ListboxTop',[],'Max',[1],'Min',[0],'Position',[0.5,0.5,0.1,0.05],'Relief','flat','SliderStep',[0.01,0.1],'String','exam','Style','pushbutton','Value',[0],'VerticalAlignment','middle','Visible','on','Tag','obj102','Callback','exam_callback(handles)')

function exam_callback(handles)
close(f);
clear
exec('costs0-1.sce',-1) ; 
endfunction`

脚本 2

////////// Defining the figure (size, name etc)/////////////////////////////
f=figure('figure_position',[0,0],'figure_size',[1250,650],'auto_resize','on','background',[8]);

//////////
delmenu(f.figure_id,gettext('File'))
delmenu(f.figure_id,gettext('?'))
delmenu(f.figure_id,gettext('Tools'))
toolbar(f.figure_id,'off')



//Cabinet - TEXT
handles.obj17=uicontrol(f,'unit','normalized','BackgroundColor',[1,1,1],'Enable','on','FontAngle','normal','FontName','helvetica','FontSize',[12],'FontUnits','points','FontWeight','normal','ForegroundColor',[0,0,0],'HorizontalAlignment','center','ListboxTop',[],'Max',[1],'Min',[0],'Position',[0.15,0.93,0.1,0.05],'Relief','flat','SliderStep',[0.01,0.1],'String','Cabinet','Style','text','Value',[0],'VerticalAlignment','middle','Visible','on','Tag','obj17','Callback','')

// Cabinet - POP UP MENU
handles.service=uicontrol(f,'unit','normalized','BackgroundColor',[0.8,0.8,0.8],'Enable','on','FontAngle','normal','FontName','helvetica','FontSize',[12],'FontUnits','points','FontWeight','normal','ForegroundColor',[0,0.5,0],'HorizontalAlignment','center','ListboxTop',[],'Max',[1],'Min',[0],'Position',[0.25,0.93,0.15,0.05],'Relief','flat','SliderStep',[0.01,0.1],'String','1|2','Style','popupmenu','Value',[1],'VerticalAlignment','middle','Visible','on','Tag','service','Callback','service_callback(handles)')


// CALCULATE  PUSHBUTTON
handles.Calculate=uicontrol(f,'unit','normalized','BackgroundColor',[0,0.8,0],'Enable','on','FontAngle','normal','FontName','helvetica','FontSize',[16],'FontUnits','points','FontWeight','bold','ForegroundColor',[0,0,0],'HorizontalAlignment','center','ListboxTop',[],'Max',[1],'Min',[0],'Position',[0.22,0.02,0.15,0.08],'Relief','raised','SliderStep',[0.01,0.1],'String','CALCULATE','Style','pushbutton','Value',[0],'VerticalAlignment','middle','Visible','on','Tag','Calculate','Callback','Calculate_callback(handles)')

 // Resources- TEXT
 handles.Resourcestxt=uicontrol(f,'unit','normalized','BackgroundColor',[1,1,1],'Enable','on','FontAngle','normal','FontName','helvetica','FontSize',[14],'FontUnits','points','FontWeight','bold','ForegroundColor',[0,0.5,0],'HorizontalAlignment','center','ListboxTop',[],'Max',[1],'Min',[0],'Position',[0.75,0.95,0.20,0.05],'SliderStep',[0.01,0.1],'String','Resources in hours','Style','text','Value',[0],'VerticalAlignment','middle','Visible','on','Tag','','Callback','')

// TOTAL hours - TEXT
handles.totalhourstxt=uicontrol(f,'unit','normalized','BackgroundColor',[1,1,1],'Enable','on','FontAngle','normal','FontName','helvetica','FontSize',[14],'FontUnits','points','FontWeight','bold','ForegroundColor',[0,0.5,0],'HorizontalAlignment','center','ListboxTop',[],'Max',[1],'Min',[0],'Position',[0.75,0.75,0.12,0.05],'SliderStep',[0.01,0.1],'String','Total Hours','Style','text','Value',[0],'VerticalAlignment','middle','Visible','on','Tag','','Callback','')

// hardware hours  - text
handles.totalhours=uicontrol(f,'unit','normalized','BackgroundColor',[0.95,1,1],'Enable','on','FontAngle','normal','FontName','helvetica','FontSize',[14],'FontUnits','points','FontWeight','bold','ForegroundColor',[0,0.5,0],'HorizontalAlignment','center','ListboxTop',[],'Max',[1],'Min',[0],'Position',[0.88,0.75,0.08,0.05],'SliderStep',[0.01,0.1],'String','','Style','text','Value',[0],'VerticalAlignment','middle','Visible','on','Tag','totalhours','Callback','')

function Calculate_callback(handles)
if handles.service.value == 1 then
resource_hrs = 2
end

if handles.service.value == 2 then
resource_hrs = 10
end

set(handles.totalhours,'String',string(resource_hrs));

endfunction
4

1 回答 1

0

问题

这是一个范围界定问题。当函数exam_callback()被调用时,它会使用exec('costs0-1.sce',-1). 在该脚本中定义函数Calculate_callback(handles)。这超出了范围并在 exam_callback()完成时被删除,因此在按下按钮时无法调用。

第二个问题是句柄没有全局影响,所以离开时exam_callback()第二个 Cost Gui 的句柄没有添加到handles.

解决方案

您可以将 GUI 的生成移动到一个函数中createCostGui(),然后在 script1 的开头加载 script2 exec('costs0-1.sce',-1);

使Calculate_callback(handles)函数丢弃句柄参数并使用标签来查找句柄

function Calculate_callback()

    serviceHandle = findobj('tag','service');

    if serviceHandle.value == 1 then
        resource_hrs = 2
    end

    if serviceHandle.value == 2 then
        resource_hrs = 10
    end

    totalHoursHandle = findobj('tag','totalhours');

    set(totalHoursHandle,'String',string(resource_hrs));

endfunction

补充说明

文本元素通常是静态的,因此不需要回调参数。如果您希望参数保持其默认值,则无需指定它们。

Scilab 文档

h = uicontrol(PropertyName, PropertyValue,...) 创建一个 uicontrol 并为其分配指定的属性和值。它将默认值分配给您未指定的任何属性。默认的 uicontrol 样式是“按钮”。默认父级是当前图窗。有关这些属性和其他属性的信息,请参阅属性部分。

对你的问题的小评论

下次错误消息可以帮助您的问题更具体。

于 2013-05-18T12:21:02.980 回答