Appdesigner window ends up in background after uigetfile
244 次查看(过去 30 天)
显示 更早的评论
I am creating a graphic program using appdesigner but when I call a function where the user can choose a file (uigetfile) the figure window is often put behind some other of my open windows after the file has been chosen. For example if I have a word document open at the same time as my program, the word document gets focus when the file has been chosen and I have to click at the bottom bar to get back to my program. I was using GUIDE before and then I didn't have that problem. I am running on Windows. Is there a way to solve this?
2 个评论
Jorg Woehl
2016-12-21
Same problem here. When I call uigetfile or uiputfile from an App Designer app, the focus is not correctly transferred to the new file selector window. Any workarounds?
Chris McRaven
2017-6-16
I am also seeing this problem. I think this is unexpected behavior. The focus should return to the uifigure which called uigetfile(). My steps to reproduce are
- Make a mlapp in App Designer that has a button that calls uigetfile()
- Run the new program from App Designer
- Click on the Matlab main window
- Click back on the new program
- Click the button which calls uigetfile()
- Select a file, and click 'open'
- Focus will return to the Matlab main window, not the new program as expected
This also works if I have another figure window open. That is, if I click away from the new program to any open figure window, then click back to the new program to run uigetfile(), the focus is returned to that open figure window.
I've tried calling figure(app.NewProgram) immediately after to no effect. I have also tried various drawnow calls as described below, but none worked. Has anybody found a workaround?
采纳的回答
Onur Ozdemir
2022-3-14
编辑:Onur Ozdemir
2022-3-14
As WTxnitz said, all these answers address getting the app window back as focus after uigetfile but what is preferred is to prevent it from minimizing in the first place. Here I made a small change to WTxnitz's answer, this way a non visible dummy figure is created instead of sending everything to background.
f = figure('Renderer', 'painters', 'Position', [-100 -100 0 0]); %create a dummy figure so that uigetfile doesn't minimize our GUI
[filename,path] = uigetfile('*.txt', 'Open text file','MultiSelect','on');
delete(f); %delete the dummy figure
This prevents uigetfile from sending appdesigner window to background while !
Best of luck.
8 个评论
Jorg Woehl
2024-5-6
编辑:Jorg Woehl
2024-5-6
@Adee, making the figure invisible does not work for me either. Also, chosing a negative location/zero width and height does not work in R2024a; a small dummy figure still shows up in the lower left corner of the display.
It is somewhat disappointing that this bug is not yet fixed by the folks at Mathworks, after existing for more than a dozen releases...
更多回答(11 个)
Friedrich
2019-9-6
编辑:Friedrich
2019-9-6
For MATLAB R2018a and newer you can use the figure command to focus your app again,
uigetfile; % Could be other dialogs here
drawnow;
figure(app.UIFigure)
For releases prior either turn the visibility off and on again or use the attached p file (change extension from .zip to .p). The usage then would be
uigetfile; % Could be other dialogs here
drawnow;
FocusUIFigure(app.UIFigure)
9 个评论
Bruce Rodenborn
2024-1-23
编辑:Bruce Rodenborn
2024-1-23
Terrible answer. Staff at MATHWORKS should provide complete information in their solution. In particular, app.UIfigure is generically part of a NEW app in MATLAB, but is NOT part of a converted GUI. Your answer should have been properly commented instead of the anemic information provided.
Bruce Rodenborn
2024-1-23
编辑:Bruce Rodenborn
2024-1-23
The solution works, but you need to find the appropriate ufigure in your ComponentBrowser. I attached an image in case you have the same problem. My code looks like:
[file_mat,dir_mat,FilterIndex]=uigetfile('*.mat','Select Data File');
drawnow; %update the figure windows
figure(app.figure) %bring the GUI back to the foreground
Chris McRaven
2017-6-16
I believe I have a (somewhat ugly) workaround. Immediately after calling uigetfile(), simply make the main uifigure window invisible and then visible. Assuming the name of your uifigure window is 'UIFigure', add
...
filename = uigetfile(...);
app.UIFigure.Visible = 'off';
app.UIFigure.Visible = 'on';
...
right after the call to uigetfile().
13 个评论
xinxin wang
2019-5-31
Great idea!!worked for me 2018b.But it's so depressing we can't direct to UIfigure
Tomas Åhlman
2018-6-12
Try using:
filename = uigetfile(...); figure(app.UIFigure);
(assuming UIFigure is the name of your main window)
3 个评论
NbrOneProcastinator
2023-1-13
2023 and still no official fix to this issue. Matlab App Designer users have been beta testers since years now!
0 个评论
WTxnitz
2020-4-20
All these answers address getting the app window back as focus after uigetfile.
The other side of the problem for me is the uigetfile dialog itself launches behind all other windows.
Here is a workaround I found. This works on 2020a and macOS Mojave
% open dummy figure
f=figure();
%bring figure to front focus
drawnow;
%the uigetfile dialog now opens properly in front focus
[xl_file,xl_path] = uigetfile('*.*');
% after user closes dialog
% delete dummy
delete(f);
%use solution elsewhere to return focus to app
app.your_app_here.Visible = 'off';
app.your_app_here.Visible = 'on';
This seems to work both from within the app embedded code and with "external" files
in the main Matlab window.
5 个评论
WTxnitz
2021-12-19
Sorry to hear it doesn't work for you :-(
Just checked. It still works for me 2021b, update 1 (9.11.0.1809720) - latest
on latest MacBook Pro M1 and MacOs Monterey 12.1
also works on intel MBP
can't help more than this. hope you find a fix
WTxnitz
2021-12-19
Not sure what didn't work
my answer is to get the uigetfile dialog itself in front focus
I did check my code and there is one difference, as to returning focus to the app
Based on other people's input instead of:
%use solution elsewhere to return focus to app
app.your_app_here.Visible = 'off';
app.your_app_here.Visible = 'on';
I use
figure(app.your_app_here)
both seem to work
NbrOneProcastinator
2023-6-27
Seven years later and still just workarounds for this problem.
Not bad, Mathworks!
0 个评论
Adam Danz
2024-5-14
Thanks for reporting the problem.
This has been fixed in the R2024a beta release and the fix will be available in the general release in a (very near) future release.
1 个评论
Gavin
2024-10-28,19:13
编辑:Gavin
2024-10-28,19:26
I'm running R2024b with update 1 some of the related issues are fixed.
e.g. hitting Enter vs mouse click left the next dialog in the background.
uigetfile() left us in the background.
However in Windows, after uigetdir(); or uiconfirm();
figure(app.MyApp); is still required to bring the app back to the front. After directory selection the whole app disappears behind Chrome, Notepad, whatever else is open. At least now figure() works. In earlier versions even that didn't do it reliably.
And BTW, when it wasn't working it was NOT consistant. Sometimes it would work properly (randomly as far as I could see.)
Syed Hussain
2019-1-25
Hi
Its is really strange behaviour
My Solution was just to use
...
filename = uigetfile(...);
app.UIFigure.Visible = 'on';
...
It removed the glichy behaviour.
Thanks
1 个评论
Jorg Woehl
2024-5-6
This did not bring the app window back in focus for me (R2024a), and it also doesn't resolve the issue of the file selector window ending up behind the app window when calling uigetfile.
Riyadh Abbas
2017-5-26
Hi there, I found a solution to this issue provided by http://undocumentedmatlab.com/blog/solving-a-matlab-hang-problem, which suggested adding two lines drawnow; pause(0.05);
I tried it and it did work for me, hope can solve your problem.
4 个评论
James Ryan
2017-12-6
Perhaps you misread the question. This is not a hang (as in your link). The window simply end up behind others.
Jorg Woehl
2024-5-14
编辑:Jorg Woehl
2024-5-14
I have just published a simple workaround for these focus issues, which -- as The MathWorks acknowledges -- still have "currently no official workaround". Check out my File Exchange contribution https://www.mathworks.com/matlabcentral/fileexchange/165961-fixfocus, which was in part inspired by this thread.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Develop uifigure-Based Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!