GUI position issue: I can't make buttons of an upper panel visible.
6 次查看(过去 30 天)
显示 更早的评论
Dear all,
I am making my first GUI and struggling in few points. Here is one of them. I took the example doc_uipanels1 from Matlab as a basis. What I need is to have a central panel, a right panel, a bottom panel and an upper panel. I don't know why but I can't manage to have the buttons of the upper panel visible. I have tried to let them visible all the time for debugging but it doesn't work. I need the GUI to work on any size of screen so I thought using the screen parameter would be a good idea. Here is my piece of code:
% Figure resize function
function figResize(src,evt)
fpos = get(S.f,'Position');
set(S.botPanel,'Position',...
[1/20 1/20 fpos(3)-.1 fpos(4)*4/35])
set(S.rightPanel,'Position',...
[fpos(3)*105/120 fpos(4)*4/35 fpos(3)*15/120 fpos(4)*31/35])
set(S.centerPanel,'Position',...
[1/20 fpos(4)*4/35 fpos(3)*105/120 fpos(4)*31/35]);
set(S.topPanel,'Position',...
[1/20 fpos(4)*31/35 fpos(3)*105/120 fpos(4)*4/35]);
movegui(S.f,'center');
end
% Bottom panel resize function
function botPanelResize(src, evt)
bpos = get(S.botPanel,'Position');
set(plotButton,'Position',...
[bpos(3)*107/120 bpos(4)*2/8 bpos(3)*10/120 2])
set(S.FigInt,'Position',...
[bpos(3)*37/120 bpos(4)*2/8 bpos(3)*14/120 2])
set(S.ExFct,'Position',...
[bpos(3)*57/120 bpos(4)*2/8 bpos(3)*20/120 2])
end
% Right panel resize function
function rightPanelResize(src,evt)
rpos = get(S.rightPanel,'Position');
set(listBox,'Position',...
[rpos(3)*2/32 rpos(4)*2/27 rpos(3)*24/32 rpos(4)*20/27]);
set(listBoxLabel,'Position',...
[rpos(3)*4/32 rpos(4)*24/27 rpos(3)*24/32 rpos(4)*2/27]);
end
% Top panel resize function
function topPanelResize(src, evt)
tpos = get(S.topPanel,'Position');
set(S.CircCh,'Position',...
[tpos(3)*17/120 tpos(2)+tpos(4)*2/8 tpos(3)*24/120 4])
for kc = 1:length(couleurs)
set(S.ChooseCirc(kc),'Position',...
[tpos(3)*(37+10*kc)/120 tpos(2)+tpos(4)*2/8 tpos(3)*24/120 4])
end
set(S.ChosenCirc,'Position',...
[tpos(3)*(37+10*kc+1)/120 tpos(2)+tpos(4)*2/8 tpos(3)*24/120 4])
end
%%Callback for plot button
function plotButtonCallback(src,evt)
% Get workspace variables and plot some image
set([S.FigInt,S.ExFct],'Visible','On');
end % plotButtonCallback
%%Callback for Execute routine
function ExFctCallback(src,evt)
% Execute some external routines
set([S.CircCh,S.ChooseCirc(1:lkp), S.ChosenCirc],'Visible','on');
% Execute some external routine
set([S.FigInt,S.ExFct],'Visible','Off');
set([S.CircCh,S.ChooseCirc, S.ChosenCirc],'Visible','Off');
%%------------ GUI layout ---------------
SCR = get(0,'Screensize'); % Get screensize.
% List of jpg files in the dir
D = dir('*.JPG'); % or jpeg or whatever.
NAM = {D(:).name}; % Store the name of all items returned in D.
%%Set up the figure and defaults
S.f = figure('Units','characters',...
'Position',[SCR(3)/2-20 ,SCR(4)/2-20 , 20*10*(SCR(3)/1024), 2.5*20*(SCR(4)/768)],... %[30 30 80 35],...
'Color',panelColor,...
'HandleVisibility','callback',...
'IntegerHandle','off',...
'Renderer','painters',...
'menubar','none',...
'NumberTitle','off',...
'Name','Workspace Plotter',...
'ResizeFcn',@figResize);
%%Create the bottom uipanel
S.botPanel = uipanel('BorderType','etchedin',...
'BackgroundColor',panelColor,...
'Units','characters',...
'Position',[1/20 1/20 119.9 8],...
'Parent',S.f,...
'ResizeFcn',@botPanelResize);
%%Create the right side panel
S.rightPanel = uipanel('bordertype','etchedin',...
'BackgroundColor',panelColor,...
'Units','characters',...
'Position',[88 8 32 20],...
'Parent',S.f,...
'ResizeFcn',@rightPanelResize);
%%Create the center panel
S.centerPanel = uipanel('bordertype','etchedin',...
'Units','characters',...
'Position', [1/20 8 88 27],...
'Parent',S.f);
%%Create the upper uipanel
S.topPanel = uipanel('BorderType','etchedin',...
'BackgroundColor',panelColor,...
'Units','characters',...
'Position',[1/20 17/20 88 6],...
'Parent',S.f,...
'ResizeFcn',@topPanelResize);
%%Add an axes to the center panel
a = axes('parent',S.centerPanel);
axis(a,'off')
%%Add listbox and label
TipStr=sprintf('First nb = dist from target 1 to camera,\n 2nd nb = dist from 2nd target to camera');
listBoxLabel = uicontrol(S.f,'Style','text','Units','characters',...
'Position',[4 24 24 2],...
'String','Select a file',...
'TooltipString',TipStr,...
'BackgroundColor',panelColor,...
'Parent',S.rightPanel);
listBox = uicontrol(S.f,'Style','listbox','Units','characters',...
'Position',[4 2 24 20],...
'BackgroundColor','white',...
'Parent',S.rightPanel,...
'String',NAM);
%%Add bottom buttons
plotButton = uicontrol(S.f,'Style','pushbutton','Units','characters',...
'Position',[10 2 24 2],...
'String','Create Plot',...
'Parent',S.botPanel,...
'Callback',@plotButtonCallback);
% Execute code and choose if intermediate results should be visible
S.FigInt = uicontrol(S.f,'Style','check','Units','characters',...
'Position',[150 8 28 4],...
'min',0,'max',1,...
'String','Intermediate results',...
'TooltipString','Do you want to visualize intermediate results?',...
'Value',1,...
'Parent',S.botPanel,...
'Visible','Off');
S.ExFct = uicontrol(S.f,'Style','pushbutton','Units','characters',...
'Position',[200 8 52 2],...
'String','Execute the calculation on this plot',...
'Parent',S.botPanel,...
'Visible','Off',...
'Callback',@ExFctCallback);
%%Add top buttons
% Choice of the colours of the circle
inleft = 150; sepleft = 25;
couleurs = {'red';'green';'cyan';'magenta';'yellow';'blue'};
Tipstr = 'Select the 2 colours corresponding to the correct diameter of the circles';
S.CircCh = uicontrol(S.f,'Style','text','Units','characters',...
'Position',[170 28 38 4],...
'String','Choose the 2 correct circles',...
'TooltipString',Tipstr,...
'Parent',S.topPanel,...
'Visible','Off',...
'FontSize',12);
for k = 1:length(couleurs)
S.ChooseCirc(k) = uicontrol(S.f,'Style','rad','Units','characters',...
'Position',[inleft+sepleft*(k-1) 27 18 2],...
'String',couleurs{k},...
'BackgroundColor',couleurs{k},...
'Parent',S.topPanel,...
'FontSize',12,...
'Visible','Off');
end
S.ChosenCirc = uicontrol(S.f,'Style','check','Units','characters',...
'Position',[190 24 8 4],...
'String','Done',...
'min',0,'max',1,...
'Parent',S.topPanel,...
'Value',0,...
'Visible','Off');
set([S.ExFct,S.ChooseCirc],'Callback',{@ExFctCallback});
% At the end of the procedure, the execution buttons disappear and
% only the one to load a new fig stays visible.
set([S.FigInt,S.ExFct],'Visible','Off');
set([S.CircCh,S.ChooseCirc, S.ChosenCirc],'Visible','Off');
end % ExFctCallback
Any hint would be warmly welcolme as I am quite desesperate at the moment! Cheers.
0 个评论
回答(2 个)
Adam
2014-9-26
Check your 'Position' settings for the buttons.
I did a bit of quick testing and when I created a new control as:
uicontrol(S.f,'Style','text','Units','normalized',...
'Position',[0.5 0.5 0.5 0.5],...
'String','Choose the 2 correct circles',...
'TooltipString',Tipstr,...
'Parent',S.topPanel,...
'Visible','on',...
'FontSize',12);
I could see it, but not when I added yours as:
S.CircCh = uicontrol(S.f,'Style','text','Units','characters',...
'Position',[170 28 38 4],...
'String','Choose the 2 correct circles',...
'TooltipString',Tipstr,...
'Parent',S.topPanel,...
'Visible','Off',...
'FontSize',12);
You can still use characters as units (I prefer normalized units for resizing purposes, but characters is fine if you prefer it), I just used 'normalized' because I could be sure it would fit within the panel and didn't have time to mess about working out what size and position should be in chars to ensure it is actually on the panel rather than floating around off-screen somewhere.
Carine
2014-9-26
1 个评论
Adam
2014-9-26
编辑:Adam
2014-9-26
I have cleared my workspace now so unfortunately I am not 100% certain, but it is possible I set the units of the panel to normalized also, but that should not matter. The units of the panel affect its position relative to its parent .
The units of the uicontrol are relative to its parent (i.e. the panel) which is why I just used [0.5 0.5 0.5 0.5] which place it in the top-right quadrant of the panel.
I've never programmed my own resize functions as I've never found the need (using normalized units generally allows me to get away without needing to although some controls do get excessively small if the figure is resized too much). I didn't resize the figure though so I wouldn't think those resize callbacks would trigger in that case. Even where they do trigger though you may also have your 'Position' incorrect there too.
I have, on a couple of occasions, got my 'Position' badly wrong for an entire figure and created it entirely off-screen which confused me for a while. I strongly suspect your uicontrols for that top panel are simply positioned somewhere odd either off-screen or under another panel. I don't know what visibility precedence is if you have a button parented to a panel, but positioned outside of its bounds - I suspect it simply won't show up anywhere.
另请参阅
类别
在 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!