GUI中Button Types and Button Group的介绍
- 格式:doc
- 大小:437.00 KB
- 文档页数:57
GUI的种类及uCGUI的架构(说明:本博文转载自他人笔下,希望可以帮助同僚更深刻的认识GUI)GUI的种类及uC/GUI的架构一. GUI概述GUI(Graphic User Interface)是图形化的用户界面,它能提供友好的人机交互接口。
它有以下特性:体积小,运行时耗用系统资源少,层次化的结构,易移植,可靠性高嵌入式GUI种类嵌入式GUI种类有很多,下面列举几种:1. WINCE的GWES(图形、窗口、事件子系统),由应用程序接口(API)、用户接口(UI)和图形设备接口(GDI)组成,包含了消息机制2. Trolltech公司的产品:QT、QTE、QTOPIA,它们跨平台、功能强大,但资源消耗多3.MINIGUI是魏永明创建的嵌入式GUI中间件,可以以多线程、多进程、以及单任务运行,是比较成熟的商用系统4.ucGUI能支持多种环境的GUI,可以以多任务形式运行或者以前后台模式运行。
商用化,但功能相对简单GUI的两种模式:1. Windows模式,采用类似windows的API和相应的消息机制,如ucGUI、MicroWindows、miniGUI2.C/S模式,采用一个XServer,所有的显示都以客户端的形式请求服务,如Nano-XGUI在嵌入式系统或实时系统中的地位越来越多的市场需求数据显示,包括PDA、娱乐消费电子、机顶盒、DVD等影音设备、WAP 手机等高端电子产品得到广泛应用,原先仅在军工、工业控制等领域中使用的GUI图形系统,受到越来越多的关注。
对于轻量级 GUI 的系统而言,对 GUI 的要求相对较低,如传统51类型单片机这类系统一般不希望建立在庞大累赘的、非常消耗系统资源的操作系统和 GUI 之上,如 Windows 或 X Window。
目前此类系统都直接使用原有编程手段,采用比较简单的手法实现GUI。
对于太过庞大和臃肿的GUI系统而言,μc/GUI这类可运用于此类资源较紧张的轻型 GUI 的需求更加突出uc/GUI简介μc/GUI是美国Micrium公司出品的一款针对嵌入式系统的优秀图形软件。
Button的设置及各种属性(1)UIButton类继承⾃UIControl,⽽UIControl继承⾃UIView,因为UIView就是个矩形区域,所以UIButton实例化的对象其实都是⼀个矩形,虽然有各种圆⾓、增加联系⼈、信息按钮等等,给它们加个背景它们就现形成矩形了,⽽且它们有个frame属性,这就是设置位置和矩形框的。
(2)UIButton创建⼀个按钮不⽤实例化,也就是不⽤alloc和init,⽽是直接调⽤内置的⼏个⼯⼚⽅法即可,这⼀点和UILabel *label1= [[UILabel alloc]init]不同,⽽且这些类型⾥⾯最常⽤的时Custom类型,因为我们可以⾃定义图⽚,以及图⽚和⽂字的位置。
(3)按钮有很多状态,正常状态Normal、被点击时状态Highlighted等等,所以可以分别对不同状态设置属性。
(4)其实按钮最重要的不是上⾯那些设置属性,⽽是按钮关联的操作是什么?即点击后发⽣什么,这需要⼀个addtarget操作函数,如果多个按钮⽤到同⼀个函数,则需要tag属性来区别是哪个按钮。
(5)要⾃定义按钮,⼀种⽅式是我们先⾃定义⼀个继承UIButton的类,然后对这个类进⾏重写函数,相当于定制,最后⽤这个类去创建按钮,这些按钮也就具有⾃定义的样式(这种⽅法只针对⾃定义按钮类型有效)。
1 - (void)viewDidLoad {2//⽣成⼀个btn1对象,不需要alloc和init,⽽是直接⽤内置的⼯⼚⽅法,有很多可CMD+点击查看3 UIButton *btn1=[UIButton buttonWithType:UIButtonTypeRoundedRect];4//设置位置和宽⾼5 btn1.frame=CGRectMake(30, 30, 300, 30);6//设置按钮的⽂字,状态有好⼏种常⽤的时Normal和Highlighted(点击时状态),可CMD+点击查看7 [btn1 setTitle:@"点我啊!" forState:UIControlStateNormal];8//设置点击时的⽂本9 [btn1 setTitle:@"我被点了!" forState:UIControlStateHighlighted];10//设置⽂字颜⾊11 [btn1 setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];12 [btn1 setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted];13//设置点击时按钮背景颜⾊,呃,完全不起作⽤,即⽆效果14 [btn1 setTintColor:[UIColor purpleColor]];15//点击时按钮发光,就是在按钮中间发亮光,这个有效果16 btn1.showsTouchWhenHighlighted=YES;17//设置tag标签,唯⼀标记⽤,可⽤于分辨是哪个按钮控件18 btn1.tag=1;19//设置背景颜⾊20 btn1.backgroundColor=[UIColor redColor];21//现在⾼版本的iOS⾥这个⽅法会让⼈抓狂,因为我们发现,不设置背景时,圆⾓按钮没有边框,所以上⾯设置frame其实意义不⼤22//设置了背景或者图⽚后,背景是矩形,说好的圆⾓呢?坑爹呢!23//所以现在⼤多数开发都是⽤UIButtonTypeCustom,⽽不是UIButtonTypeRoundedRect2425//最重要的添加触发事件⽤户交互26//self是指调⽤哪个对象的⽅法27//btnClick:是调⽤的⽅法,btnClick和btnClick:不⼀样,后者表⽰有参数28//UIControlEventTouchUpInside是触发事件,有很多,可以CMD+点击查看29//这⾥三个参数都可以随意更换,⽐如新建⼀个类Hi,在类⾥定义⼀个⽅法-(void)report;30//然后在此⽂件引⼊Hi.h头⽂件,在这⾥实例化⼀个对象hi1,然后就可以⽤hi1代替self,⽤report代替btnClick31//意思就是点击后调⽤的是hi1对象⾥⾯的report⽅法32 [btn1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];3334//再增加⼀个按钮35 UIButton *btn2=[UIButton buttonWithType:UIButtonTypeContactAdd];36 btn2.frame=CGRectMake(30, 80, 300, 30);37//这个增加联系⼈按钮其实也是⼀个矩形,和上⾯的⼀样,都是继承⾃UIControl,⽽后者⼜继承⾃UIView,所以是矩形38//虽然按钮就⼀点点⼤,但点击整个矩形区域都是相当于点击按钮39 btn2.backgroundColor=[UIColor greenColor];40//设置标签41 btn2.tag=2;42//增加事件:和btn1调⽤同⼀个⽅法,但问题是我们如果需要区分是哪个按钮的话,就需要⽤到tag,并且把控件作为参数传递给btnClick43 [btn2 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];4445//再增加⼀个最常⽤的Custom按钮,其他按钮⾃⼰尝试46 UIButton *btn3=[UIButton buttonWithType:UIButtonTypeCustom];47 btn3.frame=CGRectMake(30 , 150 , 300, 90);48 btn3.backgroundColor=[UIColor redColor];49 btn3.tag=3;50 [btn3 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];51//设置图⽚背景被点击时变暗(但没有图⽚背景时则⽆效果)52 btn3.adjustsImageWhenHighlighted=YES;53//所以,增加图⽚⽅式之⼀是增加背景图⽚,这个图⽚如⼩会被放⼤充满整个背景54 [btn3 setBackgroundImage:[UIImage imageNamed:@"logo.png"] forState:UIControlStateNormal];55//还有⼀种增加图⽚的⽅式,是在按钮上⾯加⽽不是背景,这种⽅式不会缩放图⽚,⽽且会居中56 [btn3 setImage:[UIImage imageNamed:@"logo.png"] forState:UIControlStateNormal];57//设置按钮⽂字,增加的⽂字会和setImage图⽚⼀并居中,图⽚在左边,⽂字紧随其后58 [btn3 setTitle:@"⾃定义按钮" forState:UIControlStateNormal];59//如果需要重新排版这个图⽚和按钮⽂字的位置,则需要重写UIButton类⾥⾯的两个函数,点击UIButton可查看60//- (CGRect)titleRectForContentRect:(CGRect)contentRect;⽂字相对于按钮的位置61//- (CGRect)imageRectForContentRect:(CGRect)contentRect;图⽚相对于按钮的位置62//第⼀步:可以重新定义⼀个UIButton类叫myButton,在.m⾥重写如下函数63//- (CGRect)titleRectForContentRect:(CGRect)contentRect{64// return CGRectMake(50, 25, 100, 40);65//}66//- (CGRect)imageRectForContentRect:(CGRect)contentRect{67// return CGRectMake(150, 25, 40, 40);68//}69//第⼆步,在这个⽂件中引⼊myButton.h头⽂件,然后实例化btn3的时候,⽤myButton,⽽不⽤原始的UIButton 70//myButton相当于稍微定制了⼀下原⽣的UIButton,所以前者实例出得对象也就具有定制效果71//这种⽅式仅对UIButtonTypeCustom有效,其他⽆效7273//把三个按钮显⽰出来74 [self.view addSubview:btn1];75 [self.view addSubview:btn2];76 [self.view addSubview:btn3];77 [super viewDidLoad];78// Do any additional setup after loading the view, typically from a nib.79 }80//增加⼀个参数,即由原先的-(void)btnClick{}变成如下81//因为我们知道这⾥都是按钮对象,所以可以⽤(UIButton *)sender,但通常我们⽤通⽤指针id82 -(void)btnClick:(id)sender{83//把传递过来的控件参数转化成按钮84 UIButton *btn=(UIButton *)sender;85//把btn.tag转化成整型86 NSLog(@"OMG,it is %i",(int)btn.tag);87 }。
pythonGUI库图形界⾯开发之PyQt5单选按钮控件QRadioButton详细使⽤⽅法与实例PyQt5单选按钮控件QRadioButton简介QRadioButton 继承⾃ QAbstractButton,其主要作⽤提供⽤户⼀些互斥的按钮。
QRadioButton 只有⼀个的时候,功能类似于复选框,可以选择和取消,但是如果有多个,则必须有⼀个被选中QRadioButton类中常⽤的⽅法⽅法描述setCheckanle()设置按钮是否已经被选中,可以改变单选按钮的选中状态,如果设置为True则表⽰单选按钮将保持以点击和释放状态isChecked()返回单选按钮的状态,返回值True或FalsesetText()设置单选按钮显⽰的⽂本text()返回单选按钮显⽰的⽂本QRadioButton按钮的使⽤实例import sysfrom PyQt5.QtWidgets import *from PyQt5.QtGui import *from PyQt5.QtCore import *class RadioDemo(QWidget):def __init__(self,parent=None):super(RadioDemo,self).__init__(parent)#⽔平布局layout=QHBoxLayout()self.btn1=QRadioButton('Button1')#默认选中btn1self.btn1.setChecked(True)#toggled信号与槽函数绑定self.btn1.toggled.connect(lambda :self.btnstate(self.btn1))layout.addWidget(self.btn1)self.btn2 = QRadioButton('Button2')self.btn2.toggled.connect(lambda: self.btnstate(self.btn2))layout.addWidget(self.btn2)self.setLayout(layout)self.setWindowTitle('RadioButton demo')def btnstate(self,btn):#输出按钮1与按钮2的状态,选中还是没选中if btn.text()=='Button1':if btn.isChecked()==True:print(btn.text()+"is selected")else:print(btn.text()+"is deselected")if btn.text()=="Button2":if btn.isChecked() == True:print(btn.text() + "is selected")else:print(btn.text() + "is deselected")if __name__ == '__main__':app=QApplication(sys.argv)radioDemo=RadioDemo()radioDemo.show()sys.exit(app.exec_())效果如图QRadioButton代码分析在这个例⼦中,两个互斥的单选框被放置在窗⼝中第⼀个单选按钮btn1,被设置成默认状态self.btn1.setChecked(True)当选择两个按钮相互切换时,按钮的状态发⽣改变,将触发toggle信号,并与槽函数btnstate()连接。
buttons参数Buttons是一种常见的UI元素,它们可以用于各种应用场景,为用户提供交互和操作的方式。
在本文中,我们将介绍一些关于Buttons的相关知识和使用技巧。
一、Buttons的基本概念和作用Buttons是一种用户界面组件,通常以可点击的形式呈现在屏幕上。
它们可以用于触发操作、提交表单、导航页面等多种用途。
通过点击Button,用户可以与应用程序进行交互,触发特定的功能或操作。
Buttons是用户与应用程序之间的桥梁,起到了非常重要的作用。
二、Buttons的分类Buttons可以按照不同的标准进行分类。
这里我们将介绍一些常见的分类方式。
1. 根据外观和样式分类Buttons可以根据不同的外观和样式进行分类,例如:圆形按钮、方形按钮、带有边框的按钮、带有阴影效果的按钮等。
不同的样式可以用来区分不同的功能或操作。
2. 根据功能分类Buttons也可以根据其具体的功能进行分类。
例如:提交按钮、取消按钮、删除按钮、编辑按钮等。
不同的功能需要使用不同的按钮来表示,以便用户能够清楚地理解和操作。
3. 根据位置分类Buttons还可以根据其在页面上的位置进行分类。
例如:导航栏上的按钮、侧边栏上的按钮、页面底部的按钮等。
不同位置的按钮可以提供不同的操作和导航方式,以便用户能够方便地进行操作。
三、Buttons的设计原则和注意事项在设计和使用Buttons时,有一些原则和注意事项需要遵循,以提高用户体验和界面美观。
1. 易于辨识Buttons应该具有明显的辨识度,用户能够一眼就能够识别出它们。
合适的颜色、形状和大小都可以提高按钮的辨识度。
2. 易于点击Buttons应该具有足够的点击区域,以便用户能够轻松点击。
过小的按钮会增加用户误触的概率,过大的按钮则会占据过多的空间。
3. 一致性Buttons的样式和风格应该保持一致,以便用户能够快速识别它们。
一致的按钮样式可以提高用户的操作效率和体验。
4. 适当的反馈当用户点击Button时,应该给予适当的反馈,以示操作已经成功触发或正在进行中。
Java技巧: 推动JButtonGroup发布时间:2005-01-19 08:00:00 来源:作者:点击:283 摘要Swing的ButtonGroup 类允许单选按钮分组以保证单一选择;但是,这个实现引起了许多问题标记。
你不能检索到组中当前选择的按钮的引用,并且类允许你选择或者取消选择通过引用访问的任何按钮,不是组中成员也可。
本技巧描述了当提供便利的、使得JButtonGroup 更加易于使用的方法时JButtonGroup 子类ButtonGroup如何提供更为可靠的实现。
Swing有许多有用的类,这些类可简化绘图用户界面(GUI)的开发。
但是,其中有一些类实现起来不那么容易,如ButtonGroup。
本文解释了为什么ButtonGroup 难于设计并提供了一个代替的类,JButtonGroup,它继承了ButtonGroup并解决了ButtonGroup的某些问题。
注意: 你可从Resources处下载本文的源代码。
ButtonGroup 突破口在开发Swing GUI 经常会出现这样的情况:你建立了一个表格来收集关于输入到数据库或者保存到文件中的数据项。
这个表格可能包括文本框、单选按钮和其他窗口小部件。
你使用ButtonGroup 类将所有需要单一选择的单选按钮分组。
当表格设计准备好了的时候,你开始填写表格数据。
你遇到一组单选按钮,你需要知道组中哪个按钮被选了这样你就能储存恰当的信息到数据库或者文件中。
你现在卡住了。
为什么?ButtonGroup 类不提供到组中当前所选按钮的引用。
ButtonGroup有一个getSelection()方法,它返回所选按钮的模型(作为ButtonModel 类型),但不是按钮本身。
现在,如果你能够从模型中得到按钮引用那也好,但是你得不到。
ButtonModel 接口和它的实现类不允许你从它的模型中检索按钮引用。
那你怎么办?你仔细看看ButtonGroup 文档你会看到getActionCommand()方法。
GUI小结GUI(Graphical User Interface)是一种通过图形方式和用户交互的方式来呈现和操作计算机程序的界面。
它的出现极大地提高了计算机用户的使用体验,使得计算机操作更加友好和直观。
以下是对GUI的小结。
首先,GUI提供了一种直观和可视化的操作界面。
传统的命令行界面需要用户输入命令来操作计算机,而GUI通过图形化的方式呈现操作界面,用户只需要点击鼠标或者拖拽文件即可完成相应的操作。
这种可视化的界面极大地降低了对计算机的操作难度,使得普通用户也能够轻松上手。
其次,GUI提供了一种多任务并行操作的能力。
传统的命令行界面只能一次处理一个任务,而GUI可以同时打开多个窗口,每个窗口对应一个任务。
用户可以随时切换不同的窗口来操作不同的任务,而不需要等待上一个任务的完成。
这种多任务并行的能力使得用户可以同时进行多个操作,提高了工作效率。
此外,GUI还提供了一种易于使用和学习的界面。
GUI的操作方式通常采用鼠标点击、拖拽等直观的动作,相对于复杂的命令行操作,更容易理解和学习。
而且,GUI通常会提供一些直观的图形化元素,如按钮、滑动条、选择框等,使得用户可以直接点击和拖拽来完成操作。
这种用户友好的设计大大降低了用户学习和操作的难度,使得任何人都能够轻松上手。
最后,GUI还提供了一些额外的功能和效果。
GUI可以支持图形化的界面元素,如图标、图片、动画等,使得界面更加生动和吸引人。
此外,GUI还可以提供一些方便的功能,如自动完成功能、拼写检查功能等,使得用户的操作更加便捷和高效。
这些额外的功能和效果丰富了GUI的使用体验,使得用户能够更好地享受到计算机的乐趣。
总之,GUI作为一种直观、可视化和易于使用的界面形式,极大地提高了计算机用户的使用体验。
它提供了直观和可视化的操作界面,多任务并行的能力,易于使用和学习的界面设计,以及一些额外的功能和效果。
通过GUI,用户可以更加方便和高效地操作计算机,提高工作效率,享受数字化时代的便利。
gui基本原理GUI基本原理1. 什么是GUIGUI(Graphical User Interface),即图形用户界面,是一种人机交互界面,使用图形和图像来显示信息、实现操作。
与之相对应的是命令行界面(CLI),用户通过输入命令来操作。
2. GUI的基本组成GUI由以下几个基本组成部分构成:•窗口(Window):用户界面的基本单元,用于显示和接受用户操作。
窗口可以包含标题栏、菜单栏、工具栏、状态栏等。
•控件(Widget):窗口中的各种元素,用于展示信息和接受用户输入。
常见的控件有按钮、文本框、下拉框等。
•布局管理器(Layout Manager):用于控制窗口内控件的布局方式,如居中、靠左等。
•事件处理(Event Handling):用户的操作会触发相应的事件,通过事件处理机制,可以响应、处理这些事件,实现用户与应用程序的交互。
3. GUI的工作原理GUI的工作原理可以分为以下几个步骤:1.绘制窗口:首先,应用程序创建一个窗口对象,并通过操作系统调用,绘制窗口的基本框架和样式。
2.加载控件:界面设计师根据需求,在窗口中加载各种控件,设定控件的属性和样式。
3.事件循环:一旦窗口加载完成,应用程序就进入事件循环中。
在事件循环中,应用程序不断监听用户的操作,如鼠标点击、键盘输入等。
4.事件处理:当用户进行某种操作时,操作系统会根据操作类型生成相应的事件。
应用程序通过事件处理机制,捕获并处理这些事件,如执行相应的操作、更新窗口内容等。
5.刷新窗口:在事件处理过程中,应用程序可能需要更新窗口内容、样式等。
一旦更新完成,应用程序通过操作系统调用,将最新的窗口信息绘制出来。
6.循环更新:一旦窗口更新完成,应用程序再次进入事件循环,等待用户的下一次操作。
GUI的工作就是不断重复这个过程,实现用户与应用程序的交互。
4. GUI的优势和应用场景GUI相比于CLI具有以下优势:•直观性:通过图形和图像的展示,用户能够更直观地理解和操作应用程序。
gui教程图形用户界面(Graphical User Interface,简称GUI)是一种用户界面,通过图形化的方式给用户展示信息和实现交互操作。
相对于传统的命令行界面,GUI更加直观和易于操作,广泛应用于各种软件和系统中。
本文将介绍GUI的基本原理和用法,希望对初学者有所帮助。
首先,GUI的基本原理是通过图形化的界面元素,如按钮、文本框、菜单等,来展示和接收用户的操作。
用户可以通过鼠标点击、键盘输入等方式与界面进行交互。
GUI的主要优点是直观和可视化,使用户更容易理解和掌握软件的功能和操作方式。
在GUI的设计中,需要考虑到界面的布局、颜色、字体等各个方面。
一个好的GUI设计应该具备简洁、美观、易用的特点,同时要符合用户的习惯和预期。
一般来说,GUI的设计需要结合软件的功能和用户的需求进行综合考虑。
在实际应用中,GUI的开发可以采用各种编程语言和开发工具。
常见的GUI开发工具有Visual Studio、Eclipse、Qt等。
这些工具提供了丰富的库和组件,可以方便地创建各种界面元素,并进行布局和交互设置。
在GUI的开发中,需要掌握一些基本的概念和技巧。
比如,掌握界面元素的创建和设置方法,了解布局管理器的使用,熟悉事件处理和消息传递机制。
此外,还需要学习一些UI设计的原则和技巧,如色彩搭配、字体选择、图标设计等。
总结一下,GUI是一种直观和易用的用户界面,广泛应用于各种软件和系统中。
GUI的开发需要掌握一些基本的概念和技巧,如界面元素的创建和设置、布局管理器的使用、事件处理和消息传递机制等。
希望本文能够对初学者了解GUI有所帮助。
MATLAB GUI Tutorial - Button Types and Button Group03 Nov 2007 Quan Quach93 comments 25,002 views IntroductionIn this three-part Matlab GUI Tutorial, you will learn how to use thedifferent types of buttons available within Matlab GUIs. These buttontypes 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 PushbuttonThe 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 wi ll 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. Goto the command window and type in guide.2.You should see the following screen appear. Choose the first option BlankGUI (Default).3.Click on and add one Static Textcomponent to the GUI figure. Next, click onand add one Push button component onto the GUI figure.4.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.5.Here’s what your figure should look like after you add the components andmodify them.6.Save your GUI wherever you please with your desired filename.7.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 h ave to worry about any of the other function types.Open up the .m file that was automatically generated when you saved your GUI. Inthe Matlab editor, click on the icon, which will bring up a list of the functions within the .m file. SelectdisplayText_pushbutton_Callback.Add the following code to the function:%display "Hello Wordl!" in the static text component when the%pushbutton is pressedset(handles.display_staticText,'String','Hello World!');8.Now that we’ve completed both the visual and code aspects of the GUI, itstime 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 editor, you can click on theto launch the GUI. The GUI should appear once you click the icon. Now try clicking on the button to make sure that Hello World! appears on the GUI.And that’s it. Those are the basics of using the Push button component. No w we’re ready to move onto the Check box component.Part Two: The Check BoxThe Check Box component has two states, checked and unchecked. Thesecomponents 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.1.The first thing we need to do is to add a Check Box Component to the GUIfigure that we were just working with. So if you closed GUIDE, reopen it again. Once you have GUIDE opened again, Click onand add one Check Box component to the GUI figure.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 Boxcomponent and modify it.4.Add the following code to the bold_checkbox_Callback function:5.%checkboxStatus = 0, if the box is unchecked,6.%checkboxStatus = 1, if the box is checked7.checkboxStatus = get(handles.bold_checkbox,'Value');8.if(checkboxStatus)9.%if box is checked, text is set to bold10.set(handles.display_staticText,'FontWeight' , 'bold');11.else12.%if box is unchecked, text is set to normal13.set(handles.display_staticText,'FontWeight', 'normal');endNote: 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, itstime 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 comp onent. 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 PanelRadio 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 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 issaid 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 GUIfigure that we were just working with. So if you closed GUIDE, reopen it again. Once you have GUIDE opened again, click onand add one Button Panel component to the GUI figure. Make sure it’s large enough t o fit in three radio buttons. Next, clickon and add three radio buttons onto the button group panel.2.Double click on the first Radio Button component to bring up the PropertyInspector. Change the String property to 8. Change the Tag property tofontsize08_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 andmodify them.4.Before we move on, we should check the hierarchical structure of the GUIfigure. Click on the icon and thefollowinging 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 tutorialexample, 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_buttongr oup_SelectionChangeFcn);Make sure the previous line was added right before the line:guidata(hObject, handles);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 structurehandles = guidata(hObject);switch get(eventdata.NewValue,'Tag')% Get Tag of selected object case'fontsize08_radiobutton'%execute this code when fontsize08_radiobutton is selectedset(handles.display_staticText,'FontSize',8);case'fontsize12_radiobutton'%execute this code when fontsize12_radiobutton is selectedset(handles.display_staticText,'FontSize',12);case'fontsize16_radiobutton'%execute this code when fontsize16_radiobutton is selectedset(handles.display_staticText,'FontSize',16);otherwise% Code for when there is no match.end%updates the handles structureguidata(hObject, handles);6.Notice that the callback functions for the radio buttons were notautomatically 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, itstime 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.And that’s it. Those are the basics of using the different buttons within the Matlab GUI.Download Source FilesSource files can be downloaded here.This is the end of the tutorial.93 Responses to “MATLAB GUI Tutorial - ButtonJim: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.QuanHello!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.BenjaHi 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.QuanThank you very much. This example helps greatly.I’d like to suggest that Matlab makes GUI more easier for using by doing the following1. 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 QuanGreetings . I like your clear succint style. As to the Button groupwhere 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 replyAll the bestudayaUdaya,Try these links for a brief introduction of handles:/matlab/matlab-gui-tutorial-a-brief-introduction-to-h andlesTry this one for an intro on how to share data between callbacks/matlab/matlab-gui-tutorial-sharing-data-among-call backs-and-sub-functionsThank 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_checkbox1 . set(hObject_checkbox, ‘Value’, ‘Max’);or2 . 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,AshwiniBonjour M .QuanThanks 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 (yourbutton_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 tutorial on these topics will be of interest to many. Keep up the good work! udayaHi,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 but ton 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 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 newTEST_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 EDITgui_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});endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});elsegui_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_grouphandles.output = hObject;% Update handles structureset(handles.fontSelect_buttongroup,’SelectionChangeFcn’,@fontSelect_buttongro up_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 structurevarargout{1} = handles.output;function fontSelect_buttongroup_SelectionChangeFcn(hObject, eventdata)%retrieve GUI data, i.e. the handles structurehandles = guidata(hObject);switch get(hObject,’Tag’) % Get Ta g of selected objectcase ‘fontsize08_radiobutton’%execute this code when fontsize08_radiobutton is selectedset(handles.display_staticText,’FontSize’,8);case ‘fontsize12_radiobutton’%execute this code when fontsize12_radiobutton is selectedset(handles.display_staticText,’FontSize’,12);case ‘fontsize14_radiobutton’%execute this code when fontsize16_radiobutton is selectedset(handles.display_staticText,’FontSize’,16);otherwise% Code for when there is no match.end%updates the handles structureguidata(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,SmithHi,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 58set(handles.fontSelect_buttongroup,’SelectionChangeFcn’,@fontSelect_buttongro up_SelectionChangeFcn);Error in ==> gui_mainfcn at 166feval(gui_State.gui_OpeningFcn, gui_hFigure, [], guidata(gui_hFigure),varargin{:});Error in ==> button_tutorial at 42gui_mainfcn(gui_State, varargin{:});_________________________________________________________________ ______P/S: How to solve this error?Thanks.Regards,JessieJessie,Are you using an old version of MATLAB? It works fine for me on 2006 and 2007. (I assume it works on 2008 too)QuanHion my computer thís program works fine, but only if i type in guide and start the figure from there (the green button). If I start the figure directly nothing happens at all.I tried it on 3 computers with matlab 2007 an 2008.Does any know how to help me?Hello Quan,This is slightly irrelevant, but you seem to be on the ball. I would like a push button such that each time it is pushed, my variable, let’s call it “counter” increases by 1. My problem is that I would like this to be a global variabl e, but I don’t know where [in the m-file code] to first assign it a value. I want the value of counter to revert back *only* when I run the code, but not every time a callback function is called (via ui inputs). Any thoughts? Thanks very much for all your work.LiamMany thanks. The MATLAB help documentation does not mention the need for adding the “set” statement for the button panel in the opening function. Theirbutton panel example does not have it yet somehow works. Your tutorial really got me off dead center.Then, the real difference between radio buttons and toggle buttons is merely aesthetical? Am I wrong?Hehe, in that case, it’s always nice to check that there is humanity behind the big wizard of Oz!Great tutorial, as always, Quan!@Mr Quan:ur tutorials are great.am just learning the GUI for the last 5 dasy and ur tutorials have been verry helpful.my question is that while suing the function#function fontSelect_buttongroup_SelectionChangeFcn(hObject, eventdata)#%retrieve GUI data, i.e. the handles structure#handles = guidata(hObject);#switch get(eventdata.NewValue,’Tag’)why is this eventdata.NewValue is used here? can u please explain me this‘eventdata.NewValue’ in details?alsowhy cant we use# switch get (handles.fontSelect_buttongroup,’Tag’)or# switch get(hObject,’Tag’)here?Please reply fast Sir.@Edvieri guess ur code doesnt work because u have used hObject and noteventdata.NewValue.hello,how i can take the output for button as input to othor buttonfor example:there are 2 buttons one for select image the other for store image, if i click select image after i select the image i will click store image to this image. when i did that the store button display an error msg there is no image select.thanksHi, Thanks for very good tutorial. But I’m wondering why I received this error for part 3 tutorial?Hope to have some advice from you.Thanks again.There is no ‘SelectionChangeFcn’ property in the ‘uipanel’ class.Hi Quan,The tutorials have helped immensely. I have been working on a button group which passes a value between two GUIs. To elaborate a bit, I call up the main GUI and set a value for units (whether English or SI). I then want to change the properties of a material and call up the sub GUI to do so. My goal is to have the sub GUI display the same units as the main GUI. To do so, I have to pass the value from the main to the sub. I have used your examples and managed to pass the values. My sub nowreceives the units value from the main. I now want to have the sub display the proper units graphically when it is called, rather than the default.This appears to be the same request as Ashwini (#20 in the chat). I did notice that after running on the variable value passing all day, I had to shut the matlab program down and restart to pass the value correctly. Since I had no “clear” function in my routine, this makes sense. Hopefully this bit of advice helps someone.So how do I set the initial value of the button group before I use the‘SelectionChangeFcn’ to the value I passed so I don’t just see the default button highlighted at boot-up of the dialog?Thanks.Hi Quan,I solved the issue. It always looks simple when you get to the right answer. Here is what I did.In the opening function of the sub GUI,1. I access the handle from the main GUI to get value being passed.2. use the “set” function to access the ‘SelectionChangeFcn’ as noted in your tutorials.3. then write an “if” routine as follows:if (handles.UnitsTracker == 1)set(handles.UnitsChoice_English_radiobutton,’Value’,0)set(handles.UnitsChoice_SI_radiobutt on,’Value’,1)end% Update handles structureguidata(hObject, handles);The default is ‘0′. So I only enter this routine if I am passing a ‘1′.Hopefully this helps some other users.Quick question. There is the Opening function to execute code just BEFORE the figure window appears. But I have four plots that I want to modify in the beginning. Any there any way to add code to change them just AFTER the figure appears? I get an error t rying to adjust the axes in the Opening Function because the axes don’t exist yet. Thanks for the help.hi, i tried to use the radiobutton for my project, according to this tutorial, but the MATLAB give me this error:There is no ‘SelectionChangeFcn’ property in the ‘uipanel’ class.can anyone tell me what that means?FYI, in my m-file, i put:set(ha ndles.database_buttongroup,’SelectionChangeFcn’,@database_buttongroup _SelectionChangeFcn);since my tag for the radiobutton group is ‘database_radiobutton’i’m really new to GUI, so pardon me if this is just a silly question.thanks in advance…hello,I am creating a program where i can calculate body mass index, and i am including an option where the user gets to choose the units (US or SI) through radio buttons. but for US units, i should multiply the formula by 708 (coefficient). how can i make the program multiply by 708 when the user chooses us units and not to when the user chooses si units?thanks for the helpElieElie,Please look at the third part of this tutorial, I believe it will show you how to do exactly what you want:/matlab/matlab-gui-tutorial-buttons-button-group/3QuanHello,I am creating a GUI program that calculates body mass index (bmi) and i want it to classify the user in a range according to the bmi value. (e.g. if bmi is < 18 — normal, if bmi between 18 and 24 —overweight …) .any idea how i can do that ?Thanks !!!!!!!Elie[...] Tutorial - Slider MATLAB GUI Tutorial - Pop-up Menu MATLAB GUI Tutorial - Plotting Data to Axes MATLAB GUI Tutorial - Button Types and Button Group MATLAB GUI Tutorial - A Brief Introduction to handles MATLAB GUI Tutorial - Sharing Data among [...]Hellow ,Shall I ask you in which math ver does support this ?My one doesn’t display the button group icon from ‘guide ‘ !.So I coudn’t complete your advice with m file .If then , can I make any sentence to simulate as like your did with goup icon ?Thanks with your advice in advance .Brs,lichelHey.Thanks for the turorial. It gives ad great introduction. I was wondering how is it possible to move or create a pushbutton or edit in a program. For example, by pressing button A, button B and C appears. But by pressing Button D, button E and F appears.Any ideas anyone??Have a great weekend.Hi, I have a question about using button groups within panels. When I create a GUI with a button group within a panel, none of the buttons show up with a callback (i.e. function radiobutton1_Callback(hObject, eventd ata, handles) doesn’t show up in the GUI’s m-file). I just want the buttons to modify what’s in an Edit Text box. Is there a simple way to do this? Any advice would be greatly appreciated.Hi QuanMany thanks for the tutorialsI have one question about my program.I have some variables for my program and One of the variable value I am getting from the Button Group (three Radio Buttons). I want to assign a value depending upon the radio button selected by user.I have used these lines to define the variable so I can use in my function called bya Push Button..function millingmode_guiold_SelectionChangeFcn(hObject,eventdata)%retrieve GUI data, i.e. the handles structurehandles = guidata(hObject);switch get(eventdata.NewValue,'Tag')% Get Tag of selected object case'upmilling_gui'% execute this code when fontsize08_radiobutton is selectedset(lingmode_guiold,'string','1');case'downmilling_gui'%execute this code when fontsize12_radiobutton is selectedset(lingmode_guiold,'string','2');case'slotmilling_gui'%execute this code when fontsize16_radiobutton is selectedset(lingmode_guiold,'string','3');otherwise%Code for when there is no match.end%updates the handles structureguidata(hObject, handles);And I want to extract this value to call my main function using Push button millingmode=str2num(get(lingmode_guiold,'String'))But Every time I got the error msg.Error using ==> setThere is no 'string' property in the 'uibuttongroup'class.Error in ==> SLDGUI>millingmode_guiold_SelectionChangeFcn at 504 set(lingmode_guiold,'string','1');Error in ==> hgfeval at 63feval(fcn{1},varargin{:},fcn{2:end});Error in ==> uitools.uibuttongroup.childAddedCbk>manageButtons at 80hgfeval(cbk, source, evdata);??? Error while evaluating uicontrol Callback。