如何使按钮在 GUI 中不可见?

How I make a pushbutton invisible in a GUI?

我想用我的按钮在打击乐背景图像区域播放我的 wav,所以我需要我的按钮在我的数字上不可见 window。

我的脚本:

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[s,fs]=wavread('filename.wav');
sound(s,fs);

谢谢..

要使您的按钮在您单击时不可见,请在回调函数

中将 visible 设置为 关闭
set(hObject, 'Visible', 'off')

要使其在您的 GUI 中对其他 parts/functions 不可见,只需将 hObject 替换为您的按钮的 手柄


更新:

您可以制作一个可点击的图像,并针对不同的点击位置播放不同的声音。使用回调 'ButtonDownFcn' 触发图片中的点击事件。您可以使用轴 属性 'CurrentPoint' 检索点击的位置。此 return 为具有 x-y-z 投影坐标的 2x3 矩阵。但是当您使用 2D 图时,您可以简单地选择前 2 个值,阅读更多 here.

然后使用 x/y 坐标找出用户点击的图像中的内容并播放相应的声音。

一个简单的例子:

% Draw an image
figure()
imHandle = image(imread(figPath));
% Set callback function (target function could have any name)
set(imHandle,'ButtonDownFcn', @ImgClickCB); 

和回调函数(显示 x 和 y 坐标。)

function ImgClickCB(hObject, ~)
clickPoint = get( get(hObject,'Parent'), 'CurrentPoint');
fprintf('Clicked at x: %0.f y: %0.f \n', clickPoint(1,1), clickPoint(1,2));

以下示例隐藏和显示一个按钮。

我创建了一个示例,但没有使用 guide
您可以将代码复制并粘贴到 Matlab m 文件中执行。
在没有 guide 工具的情况下创建 GUI,更适合 Stack Overflow 站点,因为不需要附加 fig 文件。
您最好使用 guide 工具,因为没有它创建 GUI 很复杂。

以下代码示例隐藏(和显示)按钮:

%TestNoGuideHideButton.m
%Create GUI with two buttons, without using GUIDE.
function TestNoGuideHideButton()
    %Create figure.    
    h.fig = figure('position', [800 400 260 80]);

    %Add button, with callback function Button1
    h.buttonOne = uicontrol('style', 'pushbutton',...
        'position',[10 20 100 40], ...
        'string' , 'Button1', ...
        'callback', {@Button1});

    %Add button, with callback function hideButton
    h.buttonTwo = uicontrol('style', 'pushbutton', ...
        'position',[150 20 100 40], ...
        'string' , 'Hide Button1', ...
        'callback', {@hideButton});


    function Button1(hObject, eventdata)
        %Modify color of Button1 to random color.
        set(h.buttonOne, 'BackgroundColor', rand(1, 3));
    end


    function hideButton(hObject, eventdata)
        is_visible = isequal(get(h.buttonOne, 'Visible'), 'on');

        if (is_visible)
            %Hide buttonOne if Visible.
            set(h.buttonOne, 'Visible', 'off');
            set(h.buttonTwo, 'string', 'Show Button1'); %Replace string.
        else
            %Restore buttonOne if hidden.
            set(h.buttonOne, 'Visible', 'on');
            set(h.buttonTwo, 'string', 'Hide Button1'); %Replace string.
        end
    end
end

对于你上面描述的问题,你显然不能添加一个按钮来显示和隐藏另一个按钮。

您可以在播放结束时恢复按钮。

您还可以为背景图添加回调函数(在guide中寻找WindowButtonDownFcn)。
按下图中任意位置,触发回调,是否可以恢复隐藏按钮。


您可能想看看 this blog entry,我在其中讨论了如何操作 uicontrols 的 CData 属性。

我在下面添加了一些代码来展示一个简单的例子:

f = figure(); % create a figure with an axes on it
pb = uicontrol('Style','checkbox', 'Units','pixels', 'Position',[10 10 300 200], ...
               'Callback',@(a,b)msgbox('play clown!'));
% read some data
data = load ( 'clown' );
% extract out the image
img = data.X;
% convert image to RGB for displaying on checkbox
img = ind2rgb(img,colormap(f)); 
% Set the cdata property of the checkbox to be the image of interest
set(pb, 'CData', img )

上面的代码创建了一个带有小丑图像的图形,您可以单击它(这可能是您的鼓)。 "button" 一直保持在那里,您不需要使其不可见

注意:我使用复选框而不是按钮 -> 因为有时按钮在聚焦时可能有 "border",这会影响图像,而复选框则不会。

我已经复制了下面生成的图像(在我点击按钮之后):