【问题标题】:How to add items to a listbox via GUI如何通过 GUI 将项目添加到列表框
【发布时间】:2015-12-11 19:38:28
【问题描述】:

我正在拼命尝试为我的 GUI 创建一个日志框。 我想做的是一开始,在按下按钮后在列表中写入文本。 PUSH Button的回调函数为:

function run__Callback(hObject, eventdata, handles)

   initial_name=cellstr(get(handles.logbox1,'String'))
    handles.names={'test','haus', 'top', 'down', 'optimistic'}
    handles.names{end,end}=[]                                   %Add Element for new text 
    handles.names{end,end}='neuuu'                              %Add Element
    num_element=length(handles.names)                           %Count Elements
    set(handles.logbox1,'String',handles.names)                 %Aktualisiere Liste

    set(handles.logbox1,'Top',num_element)                      %Set Listbox to the last Element

并且列表框在同一个 GUI 中。尽管如此,还是有错误:

使用 hg.uicontrol/set 时出错

The name 'Top' is not an accessible property for an instance of class 'uicontrol'.

谁能帮帮我,我不明白怎么了?

最好的问候,约翰

【问题讨论】:

  • 我不明白。您希望每次按下按钮时在列表框中添加新元素?
  • logbox1 是列表框的标签,很抱歉造成这种混乱。它之所以这样命名,是因为它会记录在 GUI 中完成的所有步骤。每次我在 GUI 或其 SUBGUI 中按下按钮时,我都希望在日志框中看到该操作.....所以将带有文本字符串的项目添加到日志框中,并将日志的焦点放在最后一个元素上!但是当我尝试正确设置焦点并且我不知道如何从其他回调访问 logbox 并设置它的值时,我得到了这个错误......很高兴为您提供帮助!最好的问候,约翰

标签: matlab user-interface listbox uicontrol


【解决方案1】:

您遇到了错误,因为Top 不是listbox uicontrolproperty;此外,Top 不是任何uicontrol 的属性。

你可以找到here the list of uicontrol properties.

最接近“Top”的列表框属性是ListboxTop

我创建了两个简单的 GUI,可以帮助您管理对列表框的访问。

主 GUI“add_to_listbox”包含:

  • listboxtag listbox1
  • a pushbutton with tag pushbutton1, String "Add Action": 每次用户按下它时,都会在列表框顶部添加一个字符串,例如 "Main GUI: Inserted string #x" ("x " 是反对)
  • pushbuttontag "open_subgui", String "Open SubGUI" 打开第二个 GUI

SubGUI ("add_to_listbox_subgui") 包含

  • a pushbuttontag pushbutton1, String “在主 GUI 上添加操作”:每次用户按下它时,都会在主 GUI 顶部添加一个字符串,例如“SUB GUI Inserted string #x” GUI 列表框(“x”是一个反义词)

SubGUI 使用存储在 Main GUI guidata 中的 Main GUI listbox handle 处理向 Main GUI 列表框中添加字符串(当 Main GUI 打开 SubGUI 时,它接收 @ 作为输入987654343@ 到 Main GUI;通过它 SubGUI 访问 Main GUI guidata)。

您可以在下面找到:

  • 主 GUI OpeningFcn 初始化“动作计数器”
  • 主 GUI pushbutton1_Callback 每次按下 pushbutton 时都会在列表框中添加一个字符串
  • 主 GUI open_subgui_Callback 打开 SubGUI
  • SubGUI OpeningFcn 处理 SubGUI 和 Main GUI guidata
  • SubGUI pushbutton1_Callback 每次按下 pushbutton 时都会在主 GUI 列表框中添加一个字符串

主界面OpeningFcn

% --- Executes just before add_to_listbox is made visible.
function add_to_listbox_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to add_to_listbox (see VARARGIN)

% Choose default command line output for add_to_listbox
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes add_to_listbox wait for user response (see UIRESUME)
% uiwait(handles.figure1);
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Get Main GUI guidata
gui_data=guidata(gcf);
% Add (and initialize) button action counter to Main GUI guidata
gui_data.btn_action=0;
% Set Main GUI guidata
guidata(gcf,gui_data);
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%

主界面pushbutton1_Callback

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Add button action (from Main GUI) string to the listbox
%
% Get Mani GUI guidata
gui_data=guidata(gcf);
% Increment button action counter
gui_data.btn_action=gui_data.btn_action+1;
% Set Main GUI guidata (to store button action counter)
guidata(gcf,gui_data);
% Generate action string and add it to the listbox
% The firt strimg is directly add to the listbox
if(gui_data.btn_action == 1)
   new_str='Main GUI: Inserted string #1';
   set(handles.listbox1,'string',new_str);
else
   new_str=['Main GUI: Inserted string #' num2str(gui_data.btn_action)];
   % The fisrt string in the list box is returned as "string", to add the
   % second one, it has has to be first converted into a cellarray
   if(gui_data.btn_action == 2)
      tmp_str=cellstr(get(handles.listbox1,'string'));
   else
      % The order of the string in the listbox is reversed to have the last
      % one on top
      tmp_str=flipud(get(handles.listbox1,'string'));
   end
   % Set the updated set of seting to the listbox
   tmp_str{end+1,1}=new_str;
   set(handles.listbox1,'string',flipud(tmp_str));
end

主界面open_subgui_Callback

% --- Executes on button press in open_subgui.
function open_subgui_Callback(hObject, eventdata, handles)
% hObject    handle to open_subgui (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Open the Sub GUI; the handle of the Main GUI is passed as argument to
% This allows Sub GUI accessing to the Main GUI guidata
add_to_listbox_subgui(gcf)
% Disable the "Open Sub GUI" button
set(handles.open_subgui,'enable','off')

SubGUIOpeningFcn

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Add Sub GUI button action string to Main GUI listbox
%
% Get Sub GUI guidata
gui_data=guidata(gcf);
% Get Main GUI guidata, "gui_data.main_gui" holds the Main GUI handle
gui_data.main_gui_data=guidata(gui_data.main_gui);
% Increment the button action counter
gui_data.btn_action=gui_data.main_gui_data.btn_action+1;
% Update Main GUI button actin counter
main_gui_data=guidata(gui_data.main_gui);
main_gui_data.btn_action=gui_data.btn_action;
% Store Sub GUI guidata
guidata(gcf,gui_data);
% Store Main GUI guidata
guidata(gui_data.main_gui,main_gui_data);
%
% Add button action (from Main GUI) string to the listbox
%
if(gui_data.btn_action == 1)
   % Generate action string and add it to the Main GUI listbox
   % The firt strimg is directly add to the listbox
   new_str='SUB GUI Inserted string #1';
   set(gui_data.listbox,'string',new_str);
else
   new_str=['SUB GUI Inserted string #' num2str(gui_data.btn_action)];
   % The fisrt string in the list box is returned as "string", to add the
   % second one, it has has to be first converted into a cellarray
   if(gui_data.btn_action == 2)
      tmp_str=cellstr(get(gui_data.listbox,'string'));
   else
      % The order of the string in the listbox is reversed to have the last
      % one on top
      tmp_str=flipud(get(gui_data.listbox,'string'));
   end
   % Set the updated set of seting to the listbox
   tmp_str{end+1,1}=new_str;
   set(gui_data.listbox,'string',flipud(tmp_str));
end

希望这会有所帮助。

【讨论】:

  • 不客气!很高兴我对你有用。也许您可能想接受关闭问题的答案:-)
猜你喜欢
  • 2017-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-01
相关资源
最近更新 更多