前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >GUIDE例 开发简单计算器

GUIDE例 开发简单计算器

作者头像
巴山学长
发布2019-07-15 16:15:17
4180
发布2019-07-15 16:15:17
举报
文章被收录于专栏:巴山学长巴山学长

之前简单介绍了GUIDE的大致情况,受小可爱反应,本期推出一个容易理解的例子----计算器。过程如下:

1.打开一个新的GUI界面

到此,这个初始界面就出来了

2.控件属性的修改 比如要修改GUI画布的标题,直接双击该对象,会弹出它的属性框:

可以看到,它的所有属性,都可以修改,比如name 用代码改某个控件的某个属性也可以(set)

3.得到我想要的计算器界面

界面左侧有支持的控件,鼠标点住拖动即可放到GUI画布里,还有上部运行,布局,工具,对齐等等功能。里面包含了按钮,文本框和frame框架,相信大家一看便知。控件对象属性最常用的俩个属性是它的callback回调函数(选中控件后右键里函数里可以找到callbac函数)以及

4.

Tag属性,Tag就是这个控件的身份证,用来找到它

5.代码展示 比如按钮 1 2 3 4 5 6 7 8 9 0 ....里按钮5的回调函数代码:

6.

代码语言:javascript
复制
function five_Callback(hObject, eventdata, handles)
% hObject    handle to five (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%上面三行英文是GUIDE自己生成的注释语句,可以读一下
str=get(handles.input,'String');
%得到身份编号为input控件的字符串属性,存到字符型变量str里
str=strcat(str,'5');
%按下5后,把之前的字符后面,加上5这个字符,再存到str
set(handles.input,'String',str);
%把得到的这个新字符更新身份编号为input控件的字符串属性里,让用户看到

比如等于号(=)按钮的回调函数代码:

7.

代码语言:javascript
复制
function solve_Callback(hObject, eventdata, handles)
% hObject    handle to solve (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str=get(handles.input,'String');
str=eval(str);
%逻辑字符转结果字符
set(handles.input,'String',str);
%显示出来

是不是很简单,相信大家好好看绝对没问题,我把整个gui(fig界面和对应的m文件)贴出来供大家参考,在生成的m文件的OpeningFcn函数里加了 movegui(gcf,'center');为了让界面运行后自己居中。

8.界面

9.整个m文件

10.

代码语言:javascript
复制

% 简单计算器
function varargout = calculator_2(varargin)
% CALCULATOR_2 MATLAB code for calculator_2.fig
%      CALCULATOR_2, by itself, creates a new CALCULATOR_2 or raises the existing
%      singleton*.
%
%      H = CALCULATOR_2 returns the handle to a new CALCULATOR_2 or the handle to
%      the existing singleton*.
%
%      CALCULATOR_2('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in CALCULATOR_2.M with the given input arguments.
%
%      CALCULATOR_2('Property','Value',...) creates a new CALCULATOR_2 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before calculator_2_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to calculator_2_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help calculator_2

% Last Modified by GUIDE v2.5 26-Aug-2015 16:07:11

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @calculator_2_OpeningFcn, ...
                   'gui_OutputFcn',  @calculator_2_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 initialization code - DO NOT EDIT


% --- Executes just before calculator_2 is made visible.
function calculator_2_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no textoutput 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 calculator_2 (see VARARGIN)

% Choose default command line textoutput for calculator_2
handles.textoutput = hObject;
movegui(gcf,'center');
% Update handles structure
guidata(hObject, handles);

% UIWAIT makes calculator_2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = calculator_2_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning textoutput args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line textoutput from handles structure
varargout{1} = handles.textoutput;


% --- Executes on button press in minus.
function minus_Callback(hObject, eventdata, handles)
% hObject    handle to minus (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str=get(handles.input,'String');
str=strcat(str,'-');
set(handles.input,'String',str);


% --- Executes on button press in plus.
function plus_Callback(hObject, eventdata, handles)
% hObject    handle to plus (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str=get(handles.input,'String');
str=strcat(str,'+');
set(handles.input,'String',str);

% --- Executes on button press in division.
function division_Callback(hObject, eventdata, handles)
% hObject    handle to division (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str=get(handles.input,'String');
str=strcat(str,'/');
set(handles.input,'String',str);

% --- Executes on button press in multiplication.
function multiplication_Callback(hObject, eventdata, handles)
% hObject    handle to multiplication (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str=get(handles.input,'String');
str=strcat(str,'*');
set(handles.input,'String',str);

% --- Executes on button press in one.
function one_Callback(hObject, eventdata, handles)
% hObject    handle to one (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str=get(handles.input,'String');
str=strcat(str,'1');
set(handles.input,'String',str);

% --- Executes on button press in four.
function four_Callback(hObject, eventdata, handles)
% hObject    handle to four (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str=get(handles.input,'String');
str=strcat(str,'4');
set(handles.input,'String',str);


% --- Executes on button press in seven.
function seven_Callback(hObject, eventdata, handles)
% hObject    handle to seven (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str=get(handles.input,'String');
str=strcat(str,'7');
set(handles.input,'String',str);


% --- Executes on button press in two.
function two_Callback(hObject, eventdata, handles)
% hObject    handle to two (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str=get(handles.input,'String');
str=strcat(str,'2');
set(handles.input,'String',str);


% --- Executes on button press in three.
function three_Callback(hObject, eventdata, handles)
% hObject    handle to three (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str=get(handles.input,'String');
str=strcat(str,'3');
set(handles.input,'String',str);

% --- Executes on button press in five.
function five_Callback(hObject, eventdata, handles)
% hObject    handle to five (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str=get(handles.input,'String');
str=strcat(str,'5');
set(handles.input,'String',str);


% --- Executes on button press in six.
function six_Callback(hObject, eventdata, handles)
% hObject    handle to six (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str=get(handles.input,'String');
str=strcat(str,'6');
set(handles.input,'String',str);


% --- Executes on button press in eight.
function eight_Callback(hObject, eventdata, handles)
% hObject    handle to eight (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str=get(handles.input,'String');
str=strcat(str,'8');
set(handles.input,'String',str);


% --- Executes on button press in zero.
function zero_Callback(hObject, eventdata, handles)
% hObject    handle to zero (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str=get(handles.input,'String');
str=strcat(str,'0');
set(handles.input,'String',str);


% --- Executes on button press in nine.
function nine_Callback(hObject, eventdata, handles)
% hObject    handle to nine (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str=get(handles.input,'String');
str=strcat(str,'9');
set(handles.input,'String',str);

% --- Executes on button press in rightbracket.
function rightbracket_Callback(hObject, eventdata, handles)
% hObject    handle to rightbracket (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str=get(handles.input,'String');
str=strcat(str,')');
set(handles.input,'String',str);


% --- Executes on button press in leftbracket.
function leftbracket_Callback(hObject, eventdata, handles)
% hObject    handle to leftbracket (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str=get(handles.input,'String');
str=strcat(str,'(');
set(handles.input,'String',str);


% --- Executes on button press in decimal.
function decimal_Callback(hObject, eventdata, handles)
% hObject    handle to decimal (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str=get(handles.input,'String');
str=strcat(str,'.');
set(handles.input,'String',str);


% --- Executes on button press in solve.
function solve_Callback(hObject, eventdata, handles)
% hObject    handle to solve (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str=get(handles.input,'String');
str=eval(str);
set(handles.input,'String',str);


% --- Executes on button press in clear.
function clear_Callback(hObject, eventdata, handles)
% hObject    handle to clear (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.input,'String','');


% --- Executes on button press in backspace.
function backspace_Callback(hObject, eventdata, handles)
% hObject    handle to backspace (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str=get(handles.input,'String');
str=str(1:end-1);
set(handles.input,'String',str);


% --- Executes when uipanel4 is resized.
function uipanel4_ResizeFcn(hObject, eventdata, handles)
% hObject    handle to uipanel4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

11.俩个文件在百度云盘已经上传,后台回复 ‘计算器’即可获取链接。

欢迎大家评论转发,指出不足 小编改正!

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-06-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 巴山学长 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档