PyQt5的QRadioButton的使⽤
QRadioButton⼩部件提供了⼀个带有⽂本标签的单选按钮(本质上它还是⼀个按钮)。QRadioButton是⼀个选项按钮,可以打开(选中)或关闭(取消选中)。单选按钮通常为⽤户提供“多选⼀”操作。多个单选按钮之间是互斥的关系,属于同⼀个⽗窗⼝⼩部件的单选按钮默认是属于⼀组的,如果要将多个单选按钮分为多组,就⽤到
了QButtonGroup和QGroupBox。QButtonGroup仅仅是⼀个容器,没有任何视觉表现,并且QButtonGroup提供了
⽐QGroupBox更⽅便的信号槽操作。当然,如果要显⽰⼀些提⽰信息,⽤QGroupBox会更合适。下⾯是它的布局和事件处理例程:
#-*-coding:utf-8-*-
from PyQt5.QtWidgets import
QMainWindow,QApplication,QRadioButton,QGroupBox,QVBoxLayout,QWidget,QHBoxLayout,QButtonGroup,QLabel
import sys
class RadioRuttonDemo(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.statusBar()
language_gb = QGroupBox("请选择您喜欢的语⾔:") #定义⼀个容器htmlradio设置默认的按钮
vbllayout = QVBoxLayout()
self.python_rb = QRadioButton("Python",language_gb)
vbllayout.addWidget(self.python_rb,11)
self.cplus_rb = QRadioButton("C++",language_gb)
vbllayout.addWidget(self.cplus_rb,12)
self.vb_rb = QRadioButton("Visual Basic",language_gb)
vbllayout.addWidget(self.vb_rb,13)
self.java_rb = QRadioButton("Java",language_gb)
vbllayout.addWidget(self.java_rb,14)
language_gb.setLayout(vbllayout) #定义GroupBox的布局管理器
self.languagelist =[self.python_rb,self.cplus_rb,self.vb_rb,self.java_rb]
self.java_rb.setChecked(True) #设置默认的选项
self.languageconnect() #调⽤函数
lb1 = QLabel("请选择您喜欢的球类运动:") #⽤标签显⽰提⽰信息
self.bg_ball = QButtonGroup() #创建⼀个按钮组
self.baseketball_rb = QRadioButton("篮球",self)
self.volleyball_rb = QRadioButton("排球",self)
self.football_rb = QRadioButton("⾜球",self)
self.badminton_rb = QRadioButton("⽻⽑球",self)
self.football_rb.setChecked(True) #设置默认选中项
self.bg_ball.addButton(self.baseketball_rb,21)
self.bg_ball.addButton(self.volleyball_rb,22)
self.bg_ball.addButton(self.football_rb,23)
self.bg_ball.addButton(self.badminton_rb,24)
hblayout = QHBoxLayout() #设置四个选项的布局管理
hblayout.addWidget(self.baseketball_rb)
hblayout.addWidget(self.volleyball_rb)
hblayout.addWidget(self.football_rb)
hblayout.addWidget(self.badminton_rb)
vlayout = QVBoxLayout()
vlayout.addWidget(lb1)
vlayout.addWidget(lb1)
vlayout.addLayout(hblayout)
self.bg_t(self.balldo) #按钮组事件与函数关联
mainvblayout = QVBoxLayout()
mainvblayout.addWidget(language_gb)
mainvblayout.addLayout(vlayout)
main_frame = QWidget() #此窗格作为以上诸控件的容器
main_frame.setLayout(mainvblayout)
self.setCentralWidget(main_frame) #将窗格放在mainwindow的中间
self.statusBar()
self.setGeometry(100,100,300,250)
self.setWindowTitle("单选按钮⽰例")
self.show()
def languageconnect(self):
for rb in self.languagelist:
t(self.languagedo)
def languagedo(self):
sender = self.sender()
if sender == self.python_rb:
self.statusBar().showMessage('Python')
elif sender == self.cplus_rb:
self.statusBar().showMessage('C++')
elif sender == self.vb_rb:
self.statusBar().showMessage('Visual Basic')
elif sender == self.java_rb:
self.statusBar().showMessage('Java')
def balldo(self):
if self.bg_ball.checkedId() == 21:
self.statusBar().showMessage('你喜欢玩篮球')
elif self.bg_ball.checkedId() == 22:
self.statusBar().showMessage('你喜欢玩排球')
elif self.bg_ball.checkedId() == 23:
self.statusBar().showMessage('你喜欢踢⾜球')
elif self.bg_ball.checkedId() == 24:
self.statusBar().showMessage('你喜欢打⽻⽑球')
if __name__ == "__main__":
app = QApplication(sys.argv)
rb_w = RadioRuttonDemo()
<_())
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论