【问题标题】:MatLab GUI: toggle buttons in toolbar (only one should be active)MatLab GUI:工具栏中的切换按钮(只有一个应该处于活动状态)
【发布时间】:2015-04-16 15:49:17
【问题描述】:

我在 MatLab 中有一个 GUI,我希望在其中有一些(切换)具有缩放功能的按钮。 我的问题是,我只想同时激活一个切换按钮: 1)我点击切换按钮A 2) 按钮 A 处于活动状态 3) 现在我点击按钮 B 4) 按钮 A 和 B 处于活动状态

但我想要的是,当我激活按钮 B 时,按钮 A 的状态变为“关闭”。就像用于绘图的内置 Matlab 工具栏的行为一样。

这是我的按钮的代码:

%%% Zoom Toolbar
figureToolBar = uitoolbar;
% pointer button/all off
    % icon
    [img,~,alpha] = imread(fullfile(matlabroot,'toolbox','matlab','icons',...
        'tool_pointer.png'));
    icon = double(img)/256/256;
    icon(~alpha) = NaN;
    % button
    uipushtool(figureToolBar,'Tooltip','Pan','CData',icon,...
        'ClickedCallback','zoom off; pan off;');
% pan button
    % icon
    [img,~,alpha] = imread(fullfile(matlabroot,'toolbox','matlab','icons',...
        'tool_hand.png'));
    icon = double(img)/256/256;
    icon(~alpha) = NaN;
    % button
    uitoggletool(figureToolBar,'Tooltip','Pan','CData',icon,...
        'OnCallback','pan on','OffCallback','pan off');
% zoom in button
    % icon
    [img,~,alpha] = imread(fullfile(matlabroot,'toolbox','matlab','icons',...
        'tool_zoom_in.png'));
    icon = double(img)/256/256;
    icon(~alpha) = NaN;
    % button
    uitoggletool(figureToolBar,'Tooltip','Zoom In','CData',icon,...
        'OnCallback','zoom on','OffCallback','zoom off');
% zoom out button
    % icon
    [img,~,alpha] = imread(fullfile(matlabroot,'toolbox','matlab','icons',...
        'tool_zoom_out.png'));
    icon = double(img)/256/256;
    icon(~alpha) = NaN;
    % button
    uipushtool(figureToolBar,'Tooltip','Zoom Out','CData',icon,...
        'ClickedCallback','zoom out');
% zoom x button
    % icon
    [img,~,alpha] = imread(fullfile(matlabroot,'toolbox','shared','sdi',...
        'web','MainView','release','SDI2','icons','toolstrip',...
        'ZoomInT_16.png'));
    icon = double(img)/256;
    icon(~alpha) = NaN;
    % button
    uitoggletool(figureToolBar,'Tooltip','Zoom X','CData',icon,...
        'OnCallback','zoom xon','OffCallback','zoom off');
% zoom y button
    % icon
    [img,~,alpha] = imread(fullfile(matlabroot,'toolbox','shared','sdi',...
        'web','MainView','release','SDI2','icons','toolstrip',...
        'ZoomInY_16.png'));
    icon = double(img)/256;
    icon(~alpha) = NaN;
    % button
    uitoggletool(figureToolBar,'Tooltip','Zoom Y','CData',icon,...
        'OnCallback','zoom yon','OffCallback','zoom off');

如有必要,我可以用 *.m 和 *.fig 文件给出一个最小的工作示例。

【问题讨论】:

    标签: matlab matlab-guide togglebutton


    【解决方案1】:

    您需要使用切换操作的回调来设置其他操作的状态,例如:

    function toggleTooolbarTest
      hTool = uitoolbar;
      % Create 2 toolbar items - setting one to have a state 1      
      tog1 = uitoggletool(hTool,'Tooltip','Toggle 1', 'CData', rand(16,16,3), 'State', 'on' );
      tog2 = uitoggletool(hTool,'Tooltip','Toggle 2', 'CData', rand(16,16,3) );
    
      % Set the callback for each toggle passing itself and the other toggle
      %  to the callback
      set(tog1,'ClickedCallback',@(obj,event)ToggleToolbar(obj,tog2));
      set(tog2,'ClickedCallback',@(obj,event)ToggleToolbar(obj,tog1));
    end
    function ToggleToolbar ( primary, secondary )
      % Switch the "other" toolbar state based on the value of the 
      %   toolbar which the user clicked on.
      switch get ( primary, 'State' )
        case 'on'
          set ( secondary, 'State', 'off' );
        case 'off'
          set ( secondary, 'State', 'on' );
      end
    end
    

    【讨论】:

    • 我的想法是这样的,但是您如何将它与这些内置缩放功能结合起来?
    • @Phab 请参阅zoom 的文档,特别是h = zoom(figure_handle) 语法,它允许您启用特定图形的缩放。
    • @Phab 我会将内置的缩放切换按钮替换为您自己的按钮,您可以在其中控制状态等......但它们仍然具有与前面评论中指出的相同的操作跨度>
    • 经过几天的尝试,我想我不得不接受没有其他办法;-)
    猜你喜欢
    • 2020-05-07
    • 2020-01-04
    • 2015-04-16
    • 1970-01-01
    • 1970-01-01
    • 2012-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多