Hide and change location of pushbutton in GUI?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Peter Smith
el 28 de Sept. de 2019
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 comentarios
Respuesta aceptada
Rik
el 29 de Sept. de 2019
Editada: Rik
el 7 de Oct. de 2019
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
Más respuestas (0)
Ver también
Categorías
Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!