Create square uicontrol components when using normalized units
3 次查看(过去 30 天)
显示 更早的评论
For my figure and ui elements I prefer using normalized units, but now I'm trying to create square pushbuttons and I can't get it right with normalized units.
Apart from not getting the desired result, I'm also getting different results when I add a breakpoint in the for loop. (with breakpoint I get the desired result, removing the breakpoint gives a strange mess). Using R2019b. --> diving into the breakpoint issue is interesting, but I'm mainly interested in fixing my code :)
The goal of the script is to create 3 pushbuttons that have an image on them. I want these pushbuttons to appear as squares (equal pixels height/width). I create them as normalized (everything in my GUI is normalized), and then I play around with getpixelposition and setpixelposition.
The image is use is scanner.png and can be found here https://imgur.com/a/d6gth68 (supposed to be 257x257 pixels). The second picture is the desired result (achieved when I add a breakpoint), image 3 and 4 are variations that i get when I remove the breakpoint.
Bonus question: how do I evenly space these buttons? I would like the center button to be perfectly centered in my GUI, but currently i'm just eyeballing it.
%Create figure
h.gui.fig = figure('units','normalized' , 'menubar','none');
h.gui.fig.WindowState = 'Maximized';
h.gui.fig.Name = 'GUI';
h.gui.fig.NumberTitle = 'off';
back_clr = [0 44/256 91/256];
%Create tabs
h.gui.tg = uitabgroup(h.gui.fig,'units','normalized');
tab_names = {'Main' 'Signals' 'Tab3' 'Scanner'};
for ii=1:length(tab_names)
h.gui.tab(ii) = uitab(h.gui.tg,'title',tab_names{ii});
h.gui.tab(ii).BackgroundColor = back_clr';
end
%Create buttons with picture
I_scan=imread('scanner.png');
I_size = size(I_scan);
h.set.asa_types = {'Type1' 'Type2' 'Type3'};
tab = h.gui.tab(4);
for ii = 1:length(h.set.asa_types)
uicontrol(tab,'Style','text','ForegroundColor','w','FontSize', 18,'String',[h.set.asa_types{ii},' scan'],...
'BackgroundColor',back_clr,'HorizontalAlignment','center','units','normalized', 'Position', [(ii/5)-0.05 0.65 0.2 0.05]);
h.gui.scanner_button(ii)=uicontrol(tab,'Style','pushbutton','units','normalized','BackgroundColor','w',...
'HorizontalAlignment','center', 'Position', [ii/5 1/3 0.2 0.2],'cdata',I_scan,...
'Callback',['disp(''Pressed button ' h.set.asa_types{ii} ''');']);
%pause(0.1)
drawnow
set(h.gui.scanner_button(ii),'units','pixels') %%Adding breakpoint here changes execution
pos = getpixelposition(h.gui.scanner_button(ii))
pos(3) = I_size(1)+10; pos(4) = I_size(2)+10; %my attempt to change the buttons size to correspond with picture size (+10 for border)
setpixelposition(h.gui.scanner_button(ii),pos)
end
0 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!