MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   是否将字段动态添加到MATLAB GUI? (https://www.labfans.com/bbs/showthread.php?t=22722)

poster 2019-12-10 16:49

是否将字段动态添加到MATLAB GUI?
 
我正在使用GUIDE生成MATLAB GUI,但是我想在用户单击按钮时创建字段。有没有办法在回调中动态添加新的GUI对象?


回答:
实现此目的的一种方法是在开始时创建GUI对象,但是将其“ Visibility”属性设置为“ off”。然后,当用户单击按钮时,将“可见性”属性设置回“开”。这样,您将不会在GUI运行时创建新的GUI对象,而只需更改它的哪些部分可见或不可见。

[B]编辑:[/B]如果在运行时之前不知道需要多少个新的GUI对象,这就是将新的GUI对象添加到handles结构中的方式(其中[B]hFigure[/B]是GUI图形的句柄):

p = uicontrol(hFigure,'Style','pushbutton','String','test',... 'Callback',@p_Callback); % Including callback, if needed handles.test = p; % Add p to the "test" field of the handles structure guidata(hFigure,handles); % Add the new handles structure to the figure 然后,您当然必须为新的GUI对象编写回调函数(如果需要),它可能看起来像这样:

function p_Callback(hObject,eventdata) handles = guidata(gcbf); % This gets the handles structure from the figure ... (make whatever computations/changes to GUI are needed) ... guidata(gcbf,handles); % This is needed if the handles structure is modified 我在上面的代码中使用的感兴趣的函数是: [URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/guidata.html"]GUIDATA[/URL] (用于存储/检索GUI的数据)和[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/gcbf.html"]GCBF[/URL] (获取当前正在执行回调的对象的父图形的句柄)。



[url=https://stackoverflow.com/questions/1093426]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 22:46

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.