Locking All Other GUI Windows While Another is Up
11 次查看(过去 30 天)
显示 更早的评论
I have a GUI which controls hardware. The GUI creates multiple new GUI's from it which change aspects of how the original GUI interacts with this hardware. While a user is changing settings of how the original GUI functions, I do not want the original GUI to be able to function. Ensuring the button to configure the GUI is unusable is easy as I simply make the button to do so invisible while the user is waiting on results. However, I have not been able to find a simple way of making the original GUI unusable.
I've tried uiwait with a simple example, but it has not worked as the "fail test" still displays text on the original GUI (under handles1.GUI) if I change my focus back to the original GUI after creating the second (handles2.GUI).
handles1=struct();
handles1.GUI=figure();
handles1.UI1=uicontrol('style','text',...
'parent',handles1.GUI,...
'units','normalized',...
'position',[0.2 0.2 0.1 0.1]);
handles1.UI2=uicontrol('style','pushbutton',...
'Callback',@Lock_Me,...
'parent',handles1.GUI,...
'String','Lock Test',...
'Units','Normalized',...
'position',[0.4 0.4 0.1 0.1]);
handles1.UI3=uicontrol('style','pushbutton',...
'Callback',@Fail_Me,...
'parent',handles1.GUI,...
'String','Fail Test',...
'Units','Normalized',...
'position',[0.6 0.6 0.1 0.1]);
function Lock_Me(hObject,~,~)
handles2=struct();
handles2.GUI=figure();
handles2.UI1=uicontrol('style','pushbutton',...
'Parent',handles2.GUI,...
'Callback',@Unlock_Me);
setappdata(handles2.GUI,'lockedfig',hObject.Parent)
uiwait(hObject.Parent);
end
function Unlock_Me(hObject,~,~)
uiresume(getappdata(handles2.GUI,'lockedfig'))
close(hObject.Parent)
end
function Fail_Me(hObject,~,~)
GUI1children=hObject.Parent.Children;
for i=1:length(GUI1children)
if strcmp(GUI1children(i).Style,'text')
GUI1children(i).String='Failure';
end
end
end
I'm not super familiar with uiwait and uiresume while using multiple figures, so I may be incorrect in using appdata to transfer the handle of the original figure which needs to be resumed.
This is not the exact structure of my GUI, it is much more complex with a few pushbuttons, and popupmenus I would like to disable. The GUI window which is spawned to configure the original GUI also has multiple GUI's it can create to change advanced settings of the original GUI. However, if the above code works the way it should, then I should be okay.
I'm using MATLAB 2017b and am not using GUIDE, but I'm sure if you submit something with how GUIDE does something I can figure out how to do it manually.
6 个评论
OCDER
2018-8-3
If you put the handles.GUI.Visible 'on', in the catch statement, then your original GUI will reappear ONLY WHEN there's an error from the New GUI - as in, normal-working New GUI will just forever hide the original GUI.
The New GUI should have uiwait and uiresume in it, so the the original GUI is waiting for the New GUI to complete and return an output. Without the uiwait and uiresume in NewGUI, you're right - it'll just open New GUI + reshow Orig GUI.
采纳的回答
Adam Danz
2018-8-3
To summarize my and OCDER's suggestions:
Toggle the visibility of the GUI with
handles.GUI.Visible = 'off'; % or 'on'
In the case of a malfunction when the GUI is inivisible one option is to set a try/catch as OCDER suggested in the comments above.
handles.GUI.Visible = 'off';
try
Out = NewGUI(handles.GUI.Position, ...); %Open your new GUI
%Any other stuff that might trigger an error goes here....
catch E
disp(E);
handles.GUI.Visible = 'on'; %Restore your GUI Visibility
end
handles.GUI.Visible = 'on'; %Restore your GUI Visibility
The second option is more reactionary and not automatic. You could write a simple function that searches for your invisible GUI and turns it back on. You could call this function from the command window or from a button in the visible GUIs whenever an error occurs. Of course you won't have the GUI handle any more so you have to search for it.
This function finds all figures, determines which ones are invisible, and turns those ones on.
function h = findMyInvisibleGUI()
% Searches for any figures that are invisible and makes them visible,
% and returns the handles.
allFigHandles = findall(0, 'Type', 'figure');
visibleStatus = get(allFigHandles, 'Visible');
isOff = strcmpi(visibleStatus, 'off');
h = allFigHandles(isOff);
set(h, 'Visible', 'on')
1 个评论
OCDER
2018-8-3
Also, uiwait, uiresume, and waitfor could be used to control which GUI must complete first before another GUI can resume. Here are the websites links for these.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!