【问题标题】:How to programmatically implement a dynamic GUI with checkboxes and scrollbars?如何以编程方式实现带有复选框和滚动条的动态 GUI?
【发布时间】:2015-12-21 21:53:54
【问题描述】:

我尝试以编程方式创建 GUI。它应显示以下内容:

右侧的复选框是静态的!左侧复选框的数量取决于用户输入!

我尝试做的是为右侧的动态复选框创建一个面板,并为这些面板创建一个滚动条。

不幸的是,我对编程 GUI 创建完全陌生。到目前为止,使用 GUIDE 一切正常。

我找到了一个很好的例子

Adding scroll bar in subplots within GUI

用轴进入它,但我没有让它适应复选框:( 这是我迄今为止的尝试。

%# create figure, panel, and slider
w = 600; h = 500;           %# width/height of figure
handles.hFig = figure('Menubar','figure', 'Resize','off', ...
    'Units','pixels', 'Position',[200 200 w h]);
handles.hPan = uipanel('Parent',handles.hFig, ...
    'Units','pixels', 'Position',[0 0 w-20 h]);
handles.hSld = uicontrol('Parent',handles.hFig, ...
    'Style','slider', 'Enable','off', ...
    'Units','pixels', 'Position',[w-20 0 20 h], ...
    'Min',0-eps, 'Max',0, 'Value',0, ...
    'Callback',{@onSlide,handles.hPan});

%# add checkbox

hcb = zeros(7,1);
clr = lines(7);

for i=1:7

    hcb(i) = addcheckbox(handles);

    pause(1)   %# slow down so that we can see the updates

end 

滑块功能没有改变。这是对我来说最有问题的功能:

function hcb = addcheckbox(handles)
    %# look for checkboxes
    cb = findobj(handles.hPan, 'type','checkbox');

    if isempty(cb)
        %# create first checkbox

        hcb = uicontrol(handles.hFig,'Style','checkbox',...
                'String','Display file extension',...
                'Value',1,'Position',[30 20 130 20]);

    else
        %# get height of figure
        p = get(handles.hFig, 'Position');
        h = p(4);

        %# increase panel height, and shift it to show new space
        p = get(handles.hPan, 'Position');
        set(handles.hPan, 'Position',[p(1) p(2)-h p(3) p(4)+h])

%         %# compute position of new axis: append on top (y-shifted)
%         p = get(ax, 'Position');
%         if iscell(p), p = cell2mat(p); end
%         p = [p(1,1) max(p(:,2))+h p(1,3) p(1,4)];
% 
%         %# create the new axis
%         hAx = axes('Parent',handles.hPan, ...
%             'Units','pixels', 'Position',p);
% 
%         %# adjust slider, and call its callback function
%         mx = get(handles.hSld, 'Max');
%         set(handles.hSld, 'Max',mx+h, 'Min',0, 'Enable','on')
%         %#set(handles.hSld, 'Value',mx+h)       %# scroll to new space
%         hgfeval(get(handles.hSld,'Callback'), handles.hSld, []);
    end

end

当我执行它时,我得到了错误:

Error using findobj
Invalid handle

Error in addcheckbox (line 3)
    cb = findobj(handles.hPan, 'type','checkbox');

这个错误是有道理的,因为我没有一个名为 checkboxes 的句柄...在示例中,'axes' 是图中包含的预定义句柄...我如何在此上下文中集成复选框?

【问题讨论】:

  • 提供的代码不会复制所描述的错误。如果您的 GUIDE GUI 可以正常运行,为什么要制作程序化 GUI?
  • 哦,你是对的!有没有可能编辑这个问题?我错过了考虑的事情... GUIDE 如何处理复选框的动态创建?必须预先定义相应的数字还是我错了?
  • 是的,没错。但是,我建议的另一种选择是使用uitable,您可以在 GUIDE 中实现它并且可以包含复选框。它还为您处理滚动。我个人更喜欢程序化 GUI,但这里没有必要重做整个 GUI。
  • 非常感谢您! uitable 似乎提供了我需要的一切!

标签: matlab user-interface checkbox scrollbar figure


【解决方案1】:

如上面的 cmets 中所述,uitable 对象在 GUIDE 中可用,并且已经包含复选框和滚动。虽然我绝对更喜欢程序化 GUI,但我也更喜欢不必重新创建已经运行的整个 GUI :)

这里有一个小的程序化示例(更容易复制/粘贴)来说明:

dummydata = {false 'Voltage Thingey 1'; ...
             false 'Voltage Thingey 2'; ...
             false 'Voltage Thingey 3' ...
             };

mytable = uitable(...
    'Data', dummydata, ...
    'ColumnEditable', [true false], ... % Allow/disallow editing of our columns
    'ColumnWidth', {'auto', 100}, ... % Width of column, in pixels
    'ColumnName', [], ... % Get rid of column headers
    'RowName', [] ...     % Get rid of row names
    );

这给了我们:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-09
    • 2013-08-10
    • 2010-11-04
    相关资源
    最近更新 更多