Domanda

I have been exploring some layouts on MATLAB GUI and will like to know if its possible to have a couple Panels on top of each other and then select between one or the other based on a button.

What I have done till now is created a GUI using GUIDE. On that I created Panel 1 with a static text inside it saying "Panel 1 visible" and another Panel 2 with a static text inside it saying " Panel 2 visible". I also created two Push Buttons one to display Panel 1 only and other to display Panel 2 only

Observation: 1) When I have the Panels located at different positions, the Push buttons work as expectedenter image description here 2) When I have the Panels located on top of each other, or even when they are partially overlapping only Push Button for Panel 1 work as expected. The push button for Panel 2 displays nothing (instead of displaying only Panel 2)enter image description here

Can someone please explain if there is a way to have multiple Panels on top of each other and then viewing only 1 of them at a time during run time (using GUIDE).

GUIDE .m file is as below:

function varargout = PanelTest(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @PanelTest_OpeningFcn, ...
                   'gui_OutputFcn',  @PanelTest_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end


function PanelTest_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);


function varargout = PanelTest_OutputFcn(hObject, eventdata, handles) 
varargout{1} = handles.output;


function Panel1_Callback(hObject, eventdata, handles)
set(handles.Panel1Tag,{'visible'},{'on'})
set(handles.Panel2Tag,{'visible'},{'off'})

function Panel2_Callback(hObject, eventdata, handles)
set(handles.Panel1Tag,{'visible'},{'off'})
set(handles.Panel2Tag,{'visible'},{'on'})

Thanks

È stato utile?

Soluzione

The issue is with how GUIDE is setting the Parent property of your second UIpanel. As you're dragging panel 2 onto panel 1, if panel 1 is highlighted it will be set as the parent of panel 2. The visibility of the children in a panel is controlled by their parent panel, so when you turn panel 1's visibility off it also turns off panel 2 if it's a child.

Here are two ways to fix this:

  1. When dragging in GUIDE, overlap them so that panel 1 is not highlighted and release. You can start dragging the panel 2 again and should be able to bring it further on top of panel 1. This method works but to me seems really annoying
  2. Add something along the lines of the following to your 'OpeningFcn'

    set(handles.uipanel2,'Parent',handles.figure1,'Position',get(handles.uipanel1,'Position'));
    

    As an artifact of setting the parent of panel 2 to panel 1, the Position property of panel 2 is relative to panel 1, so you have to just copy the position vector from panel 1 to panel 2.

Alternatively you can look into generating your GUIs programmatically and not having to deal with GUIDE and these weird little issues.

Altri suggerimenti

While waiting for a better GUI designer in Matlab, another approach to build "complex" interfaces is to use the GUI Layout toolbox (and its CardPanels in your case).

NB: You can still use GUIDE for other parts of your interface.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top