首页 > 代码库 > ListConfirm【QTableWidget】【表单确认GUI】
ListConfirm【QTableWidget】【表单确认GUI】
#coding=gbk
from PyQt4 import QtGui,QtCore
#-------------------------------------------------------------------
#ListConfirm提供一个对列表项进行应答或确认的GUI接口
#-------------------------------------------------------------------
class ListConfirm(QtGui.QWidget):
u‘‘‘ListConfirm提供一个对列表项进行应答或确认的GUI接口‘‘‘
def __init__(self,items,choices=None,defaultChoiceIndex=0,
ctlType=QtGui.QRadioButton,headers=None,title=‘‘,parent=None):
self.app=QtGui.QApplication([])
super(ListConfirm,self).__init__(parent)
#参数检验及加工处理
if choices is None:
choices=(‘Yes‘,‘No‘)
else:
if type(choices) not in (list,tuple) or len(choices)<2:
raise TypeError(‘Argument choices must be like ‘+
‘[choice1,choice2...] or (choice1,choice2...)‘+
‘, and its length must not less than 2.‘)
else:
choices=tuple(choices)
if type(defaultChoiceIndex)!=int or \
defaultChoiceIndex<0 or defaultChoiceIndex>len(choices)-1:
raise TypeError(‘Argument defaultChoiceIndex must be a integer ‘+
‘object and 0<=defaultChoiceIndex<=len(choices)-1‘)
arg_items_TypeError=‘Argument items must be like ‘+\
‘[(item1,ci),(item2,ci)...] or ‘+\
‘[[item1,ci],[item2,ci]...] or ‘+\
‘simply [item1,item2...] or (item1,item2...).‘+\
‘ ci means choiceIndex.‘
self.Items,self.Choices,self.groups=[],[],[]
if all(map(lambda x:type(x) not in (tuple,list),items)):
self.Items=list(items)
self.Choices=[defaultChoiceIndex for i in range(len(items))]
elif all(map(lambda x:type(x) in (tuple,list),items)):
if all(map(lambda x:len(x)==2,items)):
for x in items:
self.Items.append(x[0])
if type(x[1])!=int:
raise TypeError(‘Bad choice index. It must be a int object.‘)
else:
if not 0<=x[1]<=len(choices)-1:
raise ValueError(‘Bad choice index. Its value ‘+
‘is out the range‘)
else:
self.Choices.append(x[1])
else:
raise TypeError(arg_items_TypeError)
else:
raise TypeError(arg_items_TypeError)
if ctlType not in (QtGui.QRadioButton,QtGui.QComboBox):
raise ValueError(‘Argument ctlType must be a str whose value ‘+
"is ‘ComboBox‘ or ‘RadioButton‘.")
if headers is not None:
if type(headers) not in (tuple,list) or len(headers)!=2:
raise TypeError(‘Argument headers must be like ‘+
‘[header1,header2...] or (header1,header2...)‘+
‘, and its length must be 2.‘)
else:
if len(choices)==2:
headers=(‘Item‘,‘State‘)
else:
headers=(‘Item‘,‘Choice‘)
if title==‘‘:title=‘列表项应答或确认窗口‘.decode(‘gbk‘)
self.setWindowTitle(title)
screen=QtGui.QDesktopWidget().availableGeometry(0)
self.setGeometry(screen.width()/4-1,
screen.height()/5-1,
screen.width()*1/3,
screen.height()*3/5
)
#layv
layv=QtGui.QVBoxLayout()
self.setLayout(layv)
#self.tw
self.tw=QtGui.QTableWidget(len(items),2,self)
layv.addWidget(self.tw)
self.tw.setHorizontalHeaderLabels(headers)
for i in range(len(items)):
self.tw.setItem(i,0,QtGui.QTableWidgetItem(self.Items[i]))
if ctlType==QtGui.QComboBox:
ctl=ctlType()
ctl.setEditable(False)
ctl.addItems(choices)
ctl.setCurrentIndex(defaultChoiceIndex)
self.tw.setCellWidget(i,1,ctl)
else:
gp=QtGui.QGroupBox()
layhGroup=QtGui.QHBoxLayout()
gp.setLayout(layhGroup)
for j in range(len(choices)):
ctl=ctlType()
layhGroup.addWidget(ctl)
ctl.setText(choices[j])
if j==defaultChoiceIndex:
ctl.setChecked(True)
else:
ctl.setChecked(False)
self.tw.setCellWidget(i,1,gp)
self.tw.setRowHeight(i,35)
self.tw.resizeColumnsToContents()
if self.tw.columnWidth(0)+self.tw.columnWidth(1)<self.width()-50:
self.tw.setColumnWidth(0,self.width()-self.tw.columnWidth(1)-50)
#layh
layh=QtGui.QHBoxLayout()
layv.addLayout(layh)
#self.btnOk
self.btnOk=QtGui.QPushButton(‘确定‘.decode(‘gbk‘))
self.btnOk.clicked.connect(self.btnOk_Clicked)
layh.addWidget(self.btnOk)
#self.btnCancel
self.btnCancel=QtGui.QPushButton(‘取消‘.decode(‘gbk‘))
self.btnCancel.clicked.connect(self.btnCancel_Clicked)
layh.addWidget(self.btnCancel)
self.show()
self.app.exec_()
def btnOk_Clicked(self,event):
for i in range(len(self.Items)):
ctl=self.tw.cellWidget(i,1)
if type(ctl)==QtGui.QComboBox:
self.Choices[i]=cbb.currentIndex()
else:
lay=ctl.layout()
for j in range(lay.count()):
cb=lay.itemAt(j).widget()
if cb.isChecked():
self.Choices[i]=j
self.close()
def btnCancel_Clicked(self,event):
self.close()
def ListConfirm_Test():
items=[‘(1)智能模块导入‘.decode(‘gbk‘),
‘(2)联网检查‘.decode(‘gbk‘),
‘(3)模块下载‘.decode(‘gbk‘),
‘(4)模块安装‘.decode(‘gbk‘),
‘(5)软件下载‘.decode(‘gbk‘),
‘(6)软件安装‘.decode(‘gbk‘),
‘(7)改掉所有硬编码‘.decode(‘gbk‘),
‘(8)离场文件转储‘.decode(‘gbk‘)
]
headers=[‘Task‘,‘Done‘]
choices=(‘Yes‘,‘No‘)
defaultChoiceIndex=1
title=‘任务完成情况报表填写‘.decode(‘gbk‘)
print ListConfirm(items,choices,1,QtGui.QRadioButton,headers,title).Choices
if __name__==‘__main__‘:
ListConfirm_Test()