Hide and change location of pushbutton in GUI?
4 ビュー (過去 30 日間)
古いコメントを表示
I'm making a GUI using GUIDE, and I was wondering how I can change the location of push buttons after other have been made not visible. Haven't found anything online yet.
0 件のコメント
採用された回答
Rik
2019 年 9 月 29 日
編集済み: Rik
2019 年 10 月 7 日
You can set the Position property of a uicontrol object to move it to any location you like inside the callback that sets the Visible property for the objects you're hiding.
Edit:
Here is a small example of how you could do something like this:
function example_GUI
f=figure;
for n=1:3
pos=[(n-1)/3 0 1/4 1];
buttons(n)=uicontrol('Parent',f,...
'Units','Normalized',...
'Position',pos,...
'String',sprintf('Click me! (%d)',n),...
'Tag',num2str(n),...%use tag to discern the buttons in the callback
'Callback',@buttoncallback); %#ok<SAGROW>
end
%store to guidata struct
h=struct;
h.f=f;
h.buttons=buttons;
h.HasBeenClicked=false(size(h.buttons));
guidata(h.f,h)
end
function buttoncallback(hObject,eventdata) %#ok<INUSD>
h=guidata(hObject);
buttonindex=str2double(get(hObject,'Tag'));
if h.HasBeenClicked(buttonindex)
return%ignore extra click
else
h.HasBeenClicked(buttonindex)=true;
guidata(h.f,h)
end
v=1:3;v(buttonindex)=[];
%hide one button
set(h.buttons(v(1)),'Visible','off')
%'move' the other button
pos=get(h.buttons(v(2)),'Position');
pos=[pos(1:2)+pos(3:4)/3 pos(3:4)*2/3];%semi-random new position
set(h.buttons(v(2)),'Position',pos)
end
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Migrate GUIDE Apps についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!