GUI中Button Types and Button Group的介绍

更新时间:2023-07-29 03:04:01 阅读量: 实用文档 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

网上的对GUI控件的介绍!

MATLAB GUI Tutorial - Button Types and Button Group

03 Nov 2007 Quan Quach 93 comments 25,002 views

Introduction

In this three-part Matlab GUI Tutorial, you will learn how to use the

different types of buttons available within Matlab GUIs. These button

types are: push button, radio button, check box, and toggle buttons. In addition, you will learn how to use the button panel to control a group of buttons.

This tutorial is written for those with little or no experience creating a Matlab GUI (Graphical User Interface). If you re new to creating GUIs in Matlab, you should visit this tutorial first. Basic knowledge of Matlab is recommended. Matlab version 2007a is used in writing this tutorial. Both earlier versions and new versions should be compatible as well (as long as it isan t too outdated). Let s get started! Part One: The Pushbutton

The push button is a very simple component. When the user clicks on a push button, it causes an action to occur. This action is dictated by the code that is programmed in the push button s callback function. In this part of the tutorial, we will program the push button to display some text when it is pressed.

1. First, we are going to create the visual aspect of the GUI. Open up Matlab. Go to the command window and type in guide.

网上的对GUI控件的介绍!

2. You should see the following screen appear. Choose the first option Blank

GUI (Default).

3.

on

4. Click on and add one Static Text component to the GUI figure. Next, click and add one Push button component onto the GUI figure. Double click the Static Text component to bring up the Property Inspector.

Change the String property so that there is nothing inside. Change the Tag property to display_staticText. Similarly, double click on the Pushbutton component and change the String property to Display Text! and change the Tag property to displayText_pushbutton.

网上的对GUI控件的介绍!

5.

Here

s what your figure should look like after you add the components and modify them.

6.

7. Save your GUI wherever you please with your desired filename. Now, we are going to write the code for the GUI. When you save your GUI,

Matlab automatically generates an .m file to go along with the figure that you just put together. The .m file is where we attach the appropriate code to the callback of each component. For the purposes of this tutorial, we are primarily concerned only with the callback functions. You don t have to worry about any of the other function types.

Open up the .m file that was automatically generated when you saved your GUI. In the Matlab editor, click on the icon, which will bring up a list of the functions

网上的对GUI控件的介绍!

within the .m file. Select displayText_pushbutton_Callback.

Add the following code to the function:

%display "Hello Wordl!" in the static text component when the

%pushbutton is pressed

set(handles.display_staticText,'String'

,'Hello World!');

8. Now that we ve completed both the visual and code aspects of the GUI, its

time to run the GUI to make sure it works before we move on. From the m-file editor, you can click on the icon to save and run the GUI. Alternatively, from the GUIDE

to launch the GUI. The GUI should appear once you editor, you can click on the

on the GUI. click the icon. Now try clicking on the button to make sure that Hello World! appears

And that s it. Those are the basics of using the Push button component. Now we re ready to move onto the Check box component.

Part Two: The Check Box

The Check Box component has two states, checked and unchecked. These

components are usually used to control options that can be turned on and off. In this part of the tutorial, we will add the functionality of making the display text become bold or unbolded.

网上的对GUI控件的介绍!

1. The first thing we need to do is to add a Check Box Component to the GUI

figure that we were just working with. So if you closed GUIDE, reopen it again. Once you have GUIDE opened again, Click on

the GUI figure. and add one Check Box component to

2.

Double click the Check Box component to bring up the Property Inspector. Change the String property to Bold. Change the Tag property to bold_checkbox.

3.

Here s what your figure should look like after you add the Check Box component and modify it.

4.

5. Add the following code to the bold_checkbox_Callback function: %checkboxStatus = 0, if the box is unchecked,

网上的对GUI控件的介绍!

6.

7.

8.

9.

10.

11.

12.

13.

end %checkboxStatus = 1, if the box is checked checkboxStatus = get(handles.bold_checkbox,'Value'); if(checkboxStatus

) %if box is checked, text is set to bold set(handles.display_staticText,'FontWeight' , 'bold'); else %if box is unchecked, text is set to normal set(handles.display_staticText,'FontWeight', 'normal');

Note: The bold_checkbox_Callback function triggers when the user activates the check box AND when the user deactivates the check box.

14. Now that we ve completed both the visual and code aspects of the GUI, its

time to run the GUI to make sure it works before we move on. Try checking and unchecking the Check Box component to make sure that the text “Hello World!” is being bolded and unbolded.

And that s it. Those are the basics of using the Check Box component. Now we re ready to move onto the Button Group, which is the most challenging part of this tutorial.

Part Three: Radio Buttons, Toggle Buttons, and Button Group Panel

Radio buttons and Toggle buttons are used exactly the same way that check boxes are used in Matlab GUIs, so we won t go over how to use them. But there is one special case that needs to be covered. When either radio buttons or toggle buttons are used in conjunction with the button group panel, they exhibit mutually exclusive behavior. Simply put, this means that only one radio button or one toggle button

网上的对GUI控件的介绍!

can be selected at a time. This behavior can come in very useful for some GUIs. Since radio buttons and toggle buttons are identical in their functionality, what is said about one, is true for the other. Thus, only radio buttions will be discussed from here on out.

In this part of the tutorial, we will create a button group that will allow you to choose between different font sizes for the display text.

1. The first thing we need to do is to add a Button Panel component to the GUI

figure that we were just working with. So if you closed GUIDE, reopen it again. Once you have GUIDE opened again, click on and add one Button Panel component to the GUI figure. Make sure it s large enough to fit in three radio buttons. Next, click on

2. and add three radio buttons onto the button group panel. Double click on the first Radio Button component to bring up the Property

Inspector. Change the String property to 8. Change the Tag property to

fontsize08_radiobutton.

Next, double click on the second Radio Button component, and change the String property to 12, and change the Tag property to fontsize12_radiobutton.

Next, double click on the third Radio Button component, and change the String property to 16, and change the Tag property to fontsize16_radiobutton.

Finally, double click on the button group panel and change the Tag property to fontSelect_buttongroup. You should also change the String property for the button group panel to Fontsize.

3. Here s what your figure should look like after you add the components and

modify them.

网上的对GUI控件的介绍!

4.

Before we move on, we should check the hierarchical structure of the GUI figure. Click on the

icon and the followinging should appear:

Make sure that the three radio buttons are one hierarchy below the button group icon. 5. Add the following line of code to the opening function. In this tutorial

example, it is named button_tutorial_OpeningFcn function. Yours will be the name of the file you saved it as, followed by “_OpeningFcn”. set(handles.fontSelect_buttongroup,'SelectionChangeFcn',@fontSelect_buttongroup_SelectionChangeFcn);

Make sure the previous line was added right before the line:

guidata(hObject, handles);

网上的对GUI控件的介绍!

Next, add the following function at the very end of the .m file.

function fontSelect_buttongroup_SelectionChangeFcn(hObject, eventdata)

%retrieve GUI data, i.e. the handles structure

handles = guidata(hObject);

switch get(eventdata.NewValue,'Tag') % Get Tag of selected object

case 'fontsize08_radiobutton'

%execute this code when fontsize08_radiobutton is selected

set(handles.display_staticText,'FontSize',8);

case 'fontsize12_radiobutton'

%execute this code when fontsize12_radiobutton is selected

set(handles.display_staticText,'FontSize',12);

case 'fontsize16_radiobutton'

%execute this code when fontsize16_radiobutton is selected

set(handles.display_staticText,'FontSize',16);

otherwise

% Code for when there is no match.

end

%updates the handles structure

guidata(hObject, handles);

6. Notice that the callback functions for the radio buttons were not

automatically generated by Matlab. This is completely normal. Each time a button is selected within the Button Group Panel component, the function defined within the SelectionChangeFcn property of Button Group Panel component is called. The line of code that was added in the opening function specifies the callback function when a button within the button group is selcted. The selection change function is then defined at the end of the .m file.

7. Now that we ve completed both the visual and code aspects of the GUI, its

time to run the GUI again. Try clicking on all of the buttons to make sure they

perform their function correctly. Specifically, make sure that the font size changes accordingly.

网上的对GUI控件的介绍!

And that s it. Those are the basics of using the different buttons within the Matlab

GUI.

Download Source Files Source files can be downloaded

here

.

This is the end of the tutorial.

网上的对GUI控件的介绍!

Jim: Here is a relatively simple example of how this can be done. Download the files here. The example is an adder GUI that will add two numbers together when you press the enter key or when you press the “add” pushbutton.

Good luck. Stay tuned for the tutorial on this topic.

Quan

网上的对GUI控件的介绍!

Hello! and thank you very much, the info in this page was very usefull and I think it s fair to let you know it.

anyway, I implemented my code just like you said here, and it works very good, except for the case when I don t change the initial position of the buttons… in that

case it never enter to the selectionchangefnc, and therefore, it never performs what I need.

am I missing something?

thank you very much in advanced.

Benja

Hi froggy,

I don t have that version installed, but i would imagine there is some sort of option

to change the title on the button group panel. Unfortunately, I do not know how to do it since I use version 2007a.

Quan

网上的对GUI控件的介绍!

I d like to suggest that Matlab makes GUI more easier for using by doing the following

1. After click button group, asking for No. of button in the group, then generate the group with buttons automatically.

2. Generate selectionChangeFcn automatically when saving.

Hallo Mr Quan

Greetings . I like your clear succint style. As to the Button group

where exactly in the massive Simulink documentation does one find the instruction to be entered in the opening function.(Section 5 of part3). The problem here is suppression of individual callbacks for the radio buttons to ensure exclusivity i.e one

button is active at a time. The Eventdata thing is supposed to be in a future version of Simulink. Could you please elaborate on function_handles and scope of variables

in the context! Will look forward to your reply

All the best

udaya

网上的对GUI控件的介绍!

Try these links for a brief introduction of handles:

/matlab/matlab-gui-tutorial-a-brief-introduction-to-handles

Try this one for an intro on how to share data between callbacks

/matlab/matlab-gui-tutorial-sharing-data-among-callbacks-and-sub-functions

Thank u very much,

I have a doubt in setting the Value property of Check Box in MATLAB GUI, can u pls help me in solving the same.

My question is:

How to change the state of a check box by programmatically by setting the check box Value property ???

I tried with following two methods and both are not working:

function GUI_OpeningFcn(hObject, eventdata, handles, varargin)

global hObject_checkbox

1 . set(hObject_checkbox, Value , Max ); or

2 . set(hObject_checkbox, Value , 1);

Actually I want to set the Check Box property from different GUI (Main/ Parent GUI) before calling the Sub GUI, How do i do it????

PLs reply as early as possible, it is very urgent requirement to me.

Thanks and Regards,

Ashwini

Bonjour M .Quan

Thanks for the references to your tutorials. My questions were not quite at that user level but more on the use of function handles , event detections (past and current like swich closures), reference cells. The line to be inserted in the opening function (your

button_tutorial part 3 section 5) does replace individual callbacks for the radio buttons by one main with subfunctions chosen by , current tags. I feel a detailed

网上的对GUI控件的介绍!

tutorial on these topics will be of interest to many. Keep up the good work! udaya

Hi, I used a calculate pushbutton to calculate some values. When i run the gui and press the calculate button, the whole gui looks blocked. None of the buttons worked after that. Do I have to change something in the property inspector to correct it?

hi, l used push button for showing my succive data, i don t how to make the button , can l have fu rther information about it.

thanks,

Hey, I am doing the same thing as you, but it doesn t work.

Look at my code:

function varargout = test_button_group(varargin)

% TEST_BUTTON_GROUP M-file for test_button_group.fig

% TEST_BUTTON_GROUP, by itself, creates a new TEST_BUTTON_GROUP or raises

网上的对GUI控件的介绍!

the existing

% singleton*.

%

% H = TEST_BUTTON_GROUP returns the handle to a new TEST_BUTTON_GROUP or the handle to

% the existing singleton*.

%

% TEST_BUTTON_GROUP( CALLBACK ,hObject,eventData,handles,…) calls the local

% function named CALLBACK in TEST_BUTTON_GROUP.M with the given input arguments.

%

% TEST_BUTTON_GROUP( Property ,'Value ,…) creates a new

TEST_BUTTON_GROUP or raises the

% existing singleton*. Starting from the left, property value pairs are

% applied to the GUI before test_button_group_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to test_button_group_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 test_button_group % Last Modified by GUIDE v2.5 17-Jul-2008 01:38:31

% Begin initialization code - DO NOT EDIT

gui_Singleton = 1;

gui_State = struct( gui_Name , mfilename, …

gui_Singleton , gui_Singleton, …

gui_OpeningFcn , @test_button_group_OpeningFcn, …

gui_OutputFcn , @test_button_group_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{:});

网上的对GUI控件的介绍!

else

gui_mainfcn(gui_State, varargin{:});

end

% End initialization code - DO NOT EDIT

% — Executes just before test_button_group is made visible.

function test_button_group_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 test_button_group (see VARARGIN) % Choose default command line output for test_button_group

handles.output = hObject;

% Update handles structure

set(handles.fontSelect_buttongroup, SelectionChangeFcn ,@fontSelect_buttongroup_SelectionChangeFcn);

guidata(hObject, handles);

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

% — Outputs from this function are returned to the command line.

function varargout = test_button_group_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output 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 output from handles structure

varargout{1} = handles.output;

function fontSelect_buttongroup_SelectionChangeFcn(hObject, eventdata) %retrieve GUI data, i.e. the handles structure

handles = guidata(hObject);

switch get(hObject, Tag ) % Get Tag of selected object

case fontsize08_radiobutton

%execute this code when fontsize08_radiobutton is selected

set(handles.display_staticText, FontSize ,8);

网上的对GUI控件的介绍!

case fontsize12_radiobutton

%execute this code when fontsize12_radiobutton is selected

set(handles.display_staticText, FontSize ,12);

case fontsize14_radiobutton

%execute this code when fontsize16_radiobutton is selected

set(handles.display_staticText, FontSize ,16);

otherwise

% Code for when there is no match. end

%updates the handles structure

guidata(hObject, handles);

Let me know please, I need to get this done!!

Thanks!

Edvier, I don t know what your error is.

In addition, we are not in the business of debugging code. You can download the source file for this GUI to troubleshoot. Maybe you didn t name your buttons correctly or set up the button panel incorrectly.

Goodluck debugging. If you have specific questions, come back and ask them! Hello, How can i increase the size of the tick in the checkbox?

Thanks,

Smith

网上的对GUI控件的介绍!

I tried to run the source code downloaded above. But there are some error occurred. The errors are as follows:

_______________________________________________________________________

There is no SelectionChangeFcn property in the uipanel class.

Error in ==> button_tutorial>button_tutorial_OpeningFcn at 58

set(handles.fontSelect_buttongroup, SelectionChangeFcn ,@fontSelect_buttongroup_SelectionChangeFcn);

Error in ==> gui_mainfcn at 166

feval(gui_State.gui_OpeningFcn, gui_hFigure, [], guidata(gui_hFigure), varargin{:});

Error in ==> button_tutorial at 42

gui_mainfcn(gui_State, varargin{:});

_________________________________________________________________

______

P/S: How to solve this error?

Thanks.

Regards,

Jessie

Jessie,

Are you using an old version of MATLAB? It works fine for me on 2006 and 2007. (I assume it works on 2008 too)

Quan

本文来源:https://www.bwwdw.com/article/ovdm.html

Top