首页 > 解决方案 > 如何从代码创建的按钮调用另一个按钮的回调

问题描述

假设我在 MATLAB GUI 上有两个按钮。当我按下 pushMain 时,我想调用 pushChild 按钮的回调。

我可能已经简单地将 pushChild_Callback(hObjects, eventdata, handles) 放在 pushMain 的回调中,但我的问题是我通过代码创建了 pushChild 对象,因此代码上没有单独的回调函数。

handles.pushChild = uicontrol('Style','pushbutton');
handles.pushChild.Callback = {@printA, 1}
 % this means when I press on pushChild button, it will print "A = 1"

function printA(src, event, j)
handles = guidata(src);
fprintf('A = %d\n',j);

现在,当我按下 pushMain 时,我想在屏幕上看到“A = 1”。

我试过了:

function pushMain(hObject, eventdata, handles)
fprintf('1\n');
fprintf('2\n');
fprintf('3\n');
handles.pushChild.Callback(); % option 1 -> no error but nothing happens.
handles.pushChild.Callback(hObject, eventdata, handles) % option 2 -> nothing happens

问候。

标签: matlabuser-interface

解决方案


推荐阅读