【PyQt】分析承载界面
承载界面:
# coding=utf-8 import sys from PyQt4.QtCore import * from PyQt4.QtGui import * import class_datafile QTextCodec.setCodecForCStrings(QTextCodec.codecForName('utf8')) class Form(QMainWindow): def __init__(self,parent=None): super(Form,self).__init__(parent) self.setWindowTitle('DataAnylaze') self.setWindowIcon(QIcon('image/3.png')) self.setBaseSize(500,500) #self.resize(1000,1000) self.setMinimumSize(600,600) self.showMaximized() self.table=QTableWidget() self.setCentralWidget(self.table) self.createAction() self.createMenu() self.createToolBar() def createAction(self): self.openFileAct=QAction(QIcon('image/open.png'),u'打开',self) self.connect(self.openFileAct,SIGNAL('triggered()'),self.openFile) self.saveFileAct=QAction(QIcon('image/save.png'),u'保存',self) self.connect(self.saveFileAct,SIGNAL('triggered()'),self.saveFile) self.saveAnotherFileAct=QAction(QIcon('image/saveanother.png'),u'另存为',self) self.connect(self.saveAnotherFileAct,SIGNAL('triggered()'),self.saveAnotherFile) self.setRightAct=QAction(QIcon('image/r.png'),u'右对齐',self) self.connect(self.setRightAct,SIGNAL('triggered()'),self.setTableRAlignment) self.setLeftAct=QAction(QIcon('image/l.png'),u'左对齐',self) self.connect(self.setLeftAct,SIGNAL('triggered()'),self.setTableLAlignment) self.setMiddleAct=QAction(QIcon('image/m.png'),u'居中对齐',self) self.connect(self.setMiddleAct,SIGNAL('triggered()'),self.setTableMAlignment) def createMenu(self): fileMenu=self.menuBar().addMenu(u'文件') fileMenu.addAction(self.openFileAct) fileMenu.addAction(self.saveFileAct) fileMenu.addAction(self.saveAnotherFileAct) def createToolBar(self): fileToolBar=self.addToolBar(u'文件') fileToolBar.addAction(self.openFileAct) fileToolBar.addAction(self.saveFileAct) fileToolBar.addAction(self.saveAnotherFileAct) aliToolBar=self.addToolBar(u'对齐') aliToolBar.addAction(self.setLeftAct) aliToolBar.addAction(self.setRightAct) aliToolBar.addAction(self.setMiddleAct) def openFile(self): file=QFileDialog.getOpenFileName() dataFile=class_datafile.dataFileClass() dataFile.setFileDir(file) self.data,self.columnName=dataFile.getAllData() #设置表格行列数量 rowNum=dataFile.getRowLen() colNum=dataFile.getColumnLen() self.table.setColumnCount(colNum) self.table.setRowCount(rowNum) #设置表头 for i in range(len(self.columnName)): self.table.setHorizontalHeaderItem(i,QTableWidgetItem(self.columnName[i])) #设置表格内容 for j in range(len(self.columnName)): for k in range(rowNum): self.table.setItem(k,j,QTableWidgetItem(self.data[self.columnName[j]][k])) mes=QMessageBox() mes.information(self,u'测试读取数据',u'读了%d行数据' % (rowNum+1),QMessageBox.Ok) def saveFile(self): pass def saveAnotherFile(self): pass def setTableRAlignment(self): for j in range(self.table.columnCount()): for i in range(self.table.rowCount()): self.table.item(i,j).setTextAlignment(Qt.AlignRight|Qt.AlignVCenter) def setTableMAlignment(self): for j in range(self.table.columnCount()): for i in range(self.table.rowCount()): self.table.item(i,j).setTextAlignment(Qt.AlignHCenter|Qt.AlignVCenter) def setTableLAlignment(self): for j in range(self.table.columnCount()): for i in range(self.table.rowCount()): self.table.item(i,j).setTextAlignment(Qt.AlignLeft|Qt.AlignVCenter) app=QApplication(sys.argv) form=Form() form.show() app.exec_()
---------------------------------
数据解析:
1 # coding=utf-8 2 import sys 3 import copy 4 from PyQt4.QtCore import * 5 from PyQt4.QtGui import * 6 7 #QTextCodec.setCodecForTr(QTextCodec.codecForName('utf8')) 8 9 class dataFileClass(): 10 def __init__(self,parent=None): 11 self.runflag=0 12 self.data={} 13 self.columnName=[] 14 15 16 17 def readData(self): 18 if self.runflag==1: 19 fileData=open(str(self.file),'r') 20 flagcol=0 21 self.rownum=0 22 for line in fileData: 23 linelist=line.split(',') 24 if flagcol==0: 25 self.columnName=copy.copy(linelist) 26 for name in self.columnName: 27 self.data[name]=[] 28 flagcol+=1 29 else: 30 for i in range(len(linelist)): 31 self.data[self.columnName[i]].append(linelist[i]) 32 self.rownum+=1 33 34 fileData.close() 35 36 def setFileDir(self,dir): 37 self.file=dir 38 self.runflag=1 39 self.readData() 40 41 def getColumnNames(self): 42 return self.data.keys() 43 44 def getColumnLen(self): 45 return len(self.columnName) 46 47 def getRowLen(self): 48 return self.rownum 49 50 def getValue(self,columnName,row): 51 if self.data.has_key(columnName): 52 return self.data[columnName][row] 53 54 def getAllData(self): 55 return self.data,self.columnName 56 57 58