# -*- coding: utf-8 -*-
# python:2.x
__author__ = 'Administrator'
#如上图要求:创建指定多个复选框,一种是通过QT设计器Designer,这种是在知情的情况下。另一种是通过配置文件读出来,或者从数据库中取出来,还有其他情况就需要动态创建
#思路是循环列表,创建对象,插入布局,进行信号槽绑定和处理信号,通过sender()方法来获取那个对象发出的信号
from PyQt4.Qt import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Widget(QWidget):
def __init__(self,parnet=None):
super(Widget,self).__init__(parnet)
layout=QVBoxLayout()
inters=[(0,'python'),(1,'go'),(2,'c++'),(3,'sql')]
for id_,text in inters:
checkbox=QCheckBox(text,self)
checkbox.id_ = id_
checkbox.stateChanged.connect(self.checks)
layout.addWidget(checkbox)
self.la=QLabel(self)
layout.addWidget(self.la)
self.setLayout(layout)
def checks(self,start):
checkbox=self.sender()#获取发射信号对象
if start==Qt.Unchecked:
self.la.setText(u'取消{0}:{1}'.format(checkbox.id_,checkbox.text()))
elif start==Qt.Checked:
self.la.setText(u'选择了{0}: {1}'.format(checkbox.id_, checkbox.text()))
if __name__=='__main__':
import sys
app =QApplication(sys.argv)
widget = Widget()
widget.show()
sys.exit(app.exec_())
如图: