在MATLAB中启用/禁用uitabs

yizd12fk  于 6个月前  发布在  Matlab
关注(0)|答案(5)|浏览(85)

我在我的GUI中使用了matlab中的uitab组。然而,UItabgroup的一个限制是没有启用/禁用功能。我试图通过使用matlab communnity findjObject中的函数来使用其他替代方法
我使用下面的方法来使用上面的函数。

jtabgroup=findjobj(tabgroup);
jtabgroup.setEnableAt(false); % also I tried turning enable off for 
% for individual tabs using setEnabledAt(1,0)

字符串
我得到以下错误

Undefined function 'setEnabled' for input arguments of type 'handle.handle'.


有人可以帮助我解决这个问题或建议我一些启用/禁用选项卡的替代方法。

enyaitl3

enyaitl31#

你可以,也应该,使用一个很棒的GUI Layout Toolbox来代替uitab。它允许你选择性地禁用开箱即用的标签,更不用说大量其他有用的功能了。FEX上有两个提交,一个用于HG 1(uiextras包),另一个用于HG 2(uix包,向后兼容uiextras接口)。
这里是HG 2的例子,你需要将TabEnables属性设置为一个'on'/'off'值的数组,每个选项卡一个(不是最用户友好的API,但嘿,这比其他任何东西都好)。

f = figure();
p = uix.TabPanel('Parent', f,'Padding', 5);
uicontrol('Style', 'frame', 'Parent', p, 'Background', 'r');
uicontrol('Style', 'frame', 'Parent', p, 'Background', [.8 .8 .8]);
uicontrol('Style', 'frame', 'Parent', p, 'Background', 'g');
p.TabTitles = {'Red', 'Gray', 'Green'};
p.Selection = 2;
p.TabEnables = {'on','on','off'};

字符串
x1c 0d1x的数据
另一个建议是求助于纯Java解决方案。这显然是假设你只能将Java组件放置在选项卡中,但是几乎所有的Matlab UI组件,除了轴,都可以很容易地被行为更好、外观更好的Java组件替换。

uxhixvfz

uxhixvfz2#

不幸的是,正如评论中所报道的,uitab没有enable属性。
下面是iutab对象的列表:

BackgroundColor
    BeingDeleted
      BusyAction
   ButtonDownFcn
        Children
       CreateFcn
       DeleteFcn
 ForegroundColor
HandleVisibility
   Interruptible
          Parent
        Position
  SizeChangedFcn
             Tag
           Title
   TooltipString
            Type
   UIContextMenu
           Units
        UserData

字符串
一个可能的解决方法是启用/禁用属于uitabuicontrols
您可以获取uitabuicontrols列表,因为它们的句柄存储在children属性中。
一旦你有了句柄,你就可以将它们的enable属性设置为onoff
在下面的代码中:

  • 创建一个uitabgroup,其中包含两个iutab
  • 在每个iutab中,添加了一些uicontrols,初始enable状态设置为“off”
  • 在图中,添加了两个checkbox,以启用/禁用两个uitab中的uicontrols

获取句柄和启用/禁用uicontrols函数已直接写为checkboxcallbak

% Create a Figure
f = figure;
% Add a uitabgroup
tabgp = uitabgroup(f,'Position',[.05 .05 .3 .8]);
% Add two uitab
tab1 = uitab(tabgp,'Title','Tab #1');
tab2 = uitab(tabgp,'Title','Tab #2');
% Add some uicontrols to the first uitab (initial status = disabled)
% Add a Pushbutton
uicontrol('parent',tab1,'style','pushbutton','unit','normalized', ...
   'position',[.1 .1 .3 .1],'string','OK','enable','off')
% Add a checkbox
uicontrol('parent',tab1,'style','checkbox','unit','normalized', ...
   'position',[.1 .3 .6 .1],'string','Checkbox #1','enable','off')
% Add a radiobutton
uicontrol('parent',tab1,'style','radio','unit','normalized', ...
   'position',[.1 .6 .5 .1],'string','Radio #1','enable','off')
% Add another radiobutton
uicontrol('parent',tab1,'style','radio','unit','normalized', ...
   'position',[.1 .5 .5 .1],'string','Radio #2','enable','off')

% Add some uicontrols to the first uitab
% Add a Pushbutton
uicontrol('parent',tab2,'style','pushbutton','unit','normalized', ...
   'position',[.1 .1 .3 .1],'string','OK','enable','off')
% Add a checkbox
uicontrol('parent',tab2,'style','checkbox','unit','normalized', ...
   'position',[.1 .3 .6 .1],'string','Checkbox #1','enable','off')
% Add a radiobutton
uicontrol('parent',tab2,'style','radio','unit','normalized', ...
   'position',[.1 .6 .5 .1],'string','Radio #1','enable','off')
% Add another radiobutton
uicontrol('parent',tab2,'style','radio','unit','normalized', ...
   'position',[.1 .5 .5 .1],'string','Radio #2','enable','off')
% Add two checkbox to the Figure to enable / disable the uicontrols in the
% uitab
uicontrol('parent',f,'style','checkbox','unit','normalized', ...
   'position',[.4 .3 .6 .1],'string','Enable Tab 1 uicontrols', ...
   'callback','tab1_c=get(tab1,''children'');e_d=''off'';if(get(gcbo,''value'') == 1) e_d=''on''; end;set(tab1_c(:),''Enable'',e_d)')
uicontrol('parent',f,'style','checkbox','unit','normalized', ...
   'position',[.4 .4 .6 .1],'string','Enable Tab 2 uicontrols', ...
   'callback','tab2_c=get(tab2,''children'');e_d=''off'';if(get(gcbo,''value'') == 1) e_d=''on''; end;set(tab2_c(:),''Enable'',e_d)')


x1c 0d1x的数据
希望这对你有帮助。
卡普拉

sqougxex

sqougxex3#

如果你使用的是App Designer或者是基于Web的MATLAB GUI,那么JavaFrame UI的东西就没有效果了,所以基于findjobj的选项卡禁用解决方案就不起作用了。
有一个类似于findjobj的脚本,用于基于web的GUI,名为mlapptools。概括地说,您可以使用此脚本查找具有tab类并包含带有mwTabHeader类的span的<div>,然后使用CSS pointer-events属性禁止在选项卡上输入。这将锁定用户,使其无法单击选项卡,因此,切换它并激活任何回调函数。我在使用mlapptools导航HTML树时遇到了麻烦,所以我最终使用文本分析工具箱来做这件事。

% Get the HTML code that makes the figure and parse it to look for <div>s
page = mlapptools.getHTML(hUIFigure);
page = strrep(page, '\n', newline); % I had issues with HTML parsing without
page = strrep(page, '\"', '"'); % these additional substitutions
tree = htmlTree(page);
subtrees = findElement(tree, 'div');

% Get all of the current tabs titles
tabs = {tabgroup.Children.Title};
tabIDs = cell(size(tabs));

% For each DIV, match it up to a tab and extract the widgetid
for ii=1:length(subtrees)
    if contains(getAttribute(subtrees(ii), 'class'), 'tab') && ...
            contains(sprintf('%s', subtrees(ii)), 'mwTabHeader')
        for jj=1:length(tabs)
            if strcmp(strtrim(extractHTMLText(subtrees(ii))), tabs{jj})
                tabIDs{jj} = char(getAttribute(subtrees(ii), 'widgetid'));
                break
            end
        end
    end
end

fprintf('Locking tabs...\n');
win = mlapptools.getWebWindow(hUIFigure);
currentTab = find(tabgroup.SelectedTab == tabgroup.Children);
for ii=1:length(tabIDs)
    if ii ~= currentTab
        % execute dojo queries for each tab to set the style
        % note the DOM format CSS
        win.executeJS(sprintf('dojo.style(dojo.query("[%s = ''%s'']")[0], "%s", "%s")', ...
            'widgetid', tabIDs{ii}, ...
            'pointerEvents', 'none'));
        win.executeJS(sprintf('dojo.style(dojo.query("[%s = ''%s'']")[0], "%s", "%s")', ...
            'widgetid', tabIDs{ii}, ...
            'color', 'grey'));
    end
end

fprintf('Unlocking tabs...\n');
for ii=1:length(tabIDs)
    win.executeJS(sprintf('dojo.style(dojo.query("[%s = ''%s'']")[0], "%s", "%s")', ...
        'widgetid', tabIDs{ii}, ...
        'pointerEvents', 'auto'));
    win.executeJS(sprintf('dojo.style(dojo.query("[%s = ''%s'']")[0], "%s", "%s")', ...
        'widgetid', tabIDs{ii}, ...
        'color', 'black'));
end

字符串

eanckbw9

eanckbw94#

一个更简单的解决方案,对我来说-我有一个方法,递归地寻找一个选项卡中的组件,并设置启用(如果可以启用)。注意:这不会启用整个选项卡,而是内容。为了最好的图形外观,我忽略了某些元素
function setEnableWithChildren(app,component,value)%递归设置所有项目的启用状态

if isa(component, 'matlab.ui.control.Label') || isa(component, 'matlab.ui.control.Lamp')
    return % Don't enable labels, etc.
end

if isprop(component, 'Enable')
    if isa(component, 'matlab.ui.control.Table')
        if value
            component.Enable = 'on'; % Table uses on/off
        else
            component.Enable = 'off';
        end
    else
        component.Enable = value;
    end
end

if isprop(component, 'Children')
    children = allchild(component);
else
    children = [];
end
for child = reshape(children, 1, [])
    app.setEnableWithChildren(child, value);
end

字符串

ctehm74n

ctehm74n5#

一个简单但有效的解决方案,只使用MATLAB组件,就是使用一个无文本,无色的uilabel作为掩码,放置在标签上。如果标签是Visible,那么标签将不可选择,因为你不能点击标签。如果标签Visible属性是“off”,你可以选择标签。这避免了更改标签,运行回调,然后再更改回来的问题。

相关问题