Info
この質問は閉じられています。 編集または回答するには再度開いてください。
I'm working on my VERY FIRST code ever. I'm attempting to make a simple GUI which asks for the users name, and verifies that it is correct. but I can't seem to use the input.
3 ビュー (過去 30 日間)
古いコメントを表示
Here is my code:
function varargout = NameGame(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @NameGame_OpeningFcn, ...
'gui_OutputFcn', @NameGame_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
end
function NameGame_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
end
function name_input_Callback(hObject, eventdata, handles)
hObject
end
function submit_Callback(hObject, eventdata, handles)
name = 'Chris';
%Verify the response after you submit the name
choice = questdlg(['Are you sure your name is ', name, '?'], 'Yes', 'No');
%Verify Name input
switch choice
case 'Yes'
msgbox(['Welcome ',name,'!']);
case 'No'
msgbox(['Please resubmit your name']);
end
end
What the program does as of right now is that it opens up this window which asks the user to put their name:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/153712/image.png)
Then, once you click submit, it opens up this popup window which asks you to confirm if this is really your name:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/153713/image.png)
If you click "No", you are asked to re-input your name by this window message.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/153714/image.png)
If you click "Yes", it welcomes you like this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/153715/image.png)
Now, there are a few issues with my program that I would like to get fixed:
- As of right now, I don't know how to actually USE the name that the user inputs. I had to stick that line of code in there that defines "name = Chris", just so that I could get the other popup windows to work. **How can I get the input to be defined as the 'name' variable?
- How can I get it so that the entire program closes once you confirm that your name is correct and click "Ok" [when it welcomes you](<http://i.imgur.com/n859t9O.jpeg)>?
- Is there a way to get it to quit the entire program when you click "cancel" or the "x" button on the first window? As of right now, it simply moves onto the verification popup window if you try to cancel.
- On a SUPER picky note... is there any way to get it so that when you start the program, you don't need to click on the text editor to begin typing in your name? (Meaning, it's already 'selected'?)
Sorry if this is a lot of quesitons... but I'd really appreciate any help! I'm really enjoying this learning process, but I do feel a bit lost with some of the "finer" aspects of coding...
0 件のコメント
回答 (1 件)
Geoff Hayes
2016 年 2 月 1 日
Chris - if name_input is the tag for your edit text box (where the user is supposed to type in his or her name), then in the submit button callback, you can access this text box using its handle from the handles structure (the third input to this function). For example,
function submit_Callback(hObject, eventdata, handles)
name = char(get(handles.name_input,'String'));
%Verify the response after you submit the name
% etc.
If you want the GUI to close once the user has pressed the OK button (after correctly entering the name), then use the handle of the message box and uiwait to wait for the message box to close before closing the main GUI. Something like
hMsgBox = msgbox(['Welcome ',name,'!'],'modal'); % create the message box
uiwait(hMsgBox); % block execution until msg box closes
close(handles.figure1); % close the main GUI
We use close to close the GUI using its handle figure1 (this may be different for you). You can use this function if the user presses the Cancel button or presses the x button to close the second dialog (hint look at the output questDlg when the user hits x). As for setting focus to your edit control, consider using uicontrol.
2 件のコメント
Geoff Hayes
2016 年 2 月 1 日
Chris - sometimes the line of code
get(handles.name_input,'String')
returns a cell array (to support multiline inputs) and so I use char to cast (or convert) the result to a character array.
I use modal along with the uiwait to prevent the user from returning to the GUI without first interacting with the message box. (See msgbox for details.)
この質問は閉じられています。
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!