PyQt5-QTableWidget
1.设计的GUI界面为:
2.对象查看器
3.myWidget.py文件
import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow,QLabel, QTableWidgetItem, QAbstractItemView)
from enum import Enum ##枚举类型
from PyQt5.QtCore import pyqtSlot, Qt, QDate
from PyQt5.QtGui import QFont, QBrush
from ui_QtApp import Ui_MainWindow
class CellType(Enum): ##各单元格的类型
ctName=1000
ctSex =1001
ctBirth =1002
ctNation=1003
ctScore=1004
ctPartyM=1005
class FieldColNum(Enum):##各字段在表格中的列号
colName=0
colSex=1
colBirth=2
colNation=3
colScore=4
colPartyM=5
class QmyMainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent) #调用父类构造函数,创建窗体
self.ui=Ui_MainWindow() #创建UI对象
self.ui.setupUi(self) #构造UI
self.LabCellIndex=QLabel("当前单元格坐标:",self)
self.LabCellIndex.setMinimumWidth(250)
self.LabCellType=QLabel("当前单元格类型:",self)
self.LabCellType.setMinimumWidth(200)
self.LabStudID=QLabel("学生ID:",self)
self.LabStudID.setMinimumWidth(200)
self.ui.statusbar.addWidget(self.LabCellIndex) #添加到状态栏
self.ui.statusbar.addWidget(self.LabCellType)
self.ui.statusbar.addWidget(self.LabStudID)
self.ui.tableWidget.setAlternatingRowColors(True) #交替行颜色
self.__tableInitialized=False # 表格数据未初始化、
@pyqtSlot() ##"设置表头”按钮
def on_pushButton_clicked(self):
headerText = ["姓名", "性别", "出生日期", "民族", "分数", "是否党员"]
self.ui.tableWidget.setColumnCount(len(headerText)) # 列数
for i in range(len(headerText)):
headerItem = QTableWidgetItem(headerText[i])
font = headerItem.font()
font.setPointSize(11)
headerItem.setFont(font)
headerItem.setForeground(QBrush(Qt.red)) # 前景色,即文字颜色
self.ui.tableWidget.setHorizontalHeaderItem(i, headerItem)
@pyqtSlot() ##设置行数按钮
def on_pushButton_2_clicked(self):
self.ui.tableWidget.setRowCount(self.ui.spinBox.value())
#设置工作区行数
self.ui.tableWidget.setAlternatingRowColors(self.ui.checkBox_2.isChecked())
#设置交替行背景色
@pyqtSlot() ##初始化表格数据
def on_pushButton_3_clicked(self):
self.ui.tableWidget.clearContents() # 清除表格内容
birth = QDate(1998, 6, 23)
isParty = True
nation = "汉族"
score = 70
rowCount = self.ui.tableWidget.rowCount() # 表格行数
for i in range(rowCount):
strName = "学生%d" % i
if ((i % 2) == 0):
strSex = "男"
else:
strSex = "女"
self.__createItemsARow(i, strName, strSex, birth, nation, isParty, score)
birth = birth.addDays(20)
isParty = not isParty
self.__tableInitialized = True # 表格数据已初始化
def __createItemsARow(self, rowNo, name, sex, birth, nation, isParty, score):
StudID = 201805000 + rowNo # 学号
##姓名
item = QTableWidgetItem(name, CellType.ctName.value)
item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
font = item.font()
font.setBold(True)
item.setFont(font)
item.setData(Qt.UserRole, StudID) # 关联数据
self.ui.tableWidget.setItem(rowNo, FieldColNum.colName.value, item)
##性别
item = QTableWidgetItem(sex, CellType.ctSex.value)
item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
self.ui.tableWidget.setItem(rowNo, FieldColNum.colSex.value, item)
##出生日期
strBitrh = birth.toString("yyyy-MM-dd") # 日期转换为字符串
item = QTableWidgetItem(strBitrh, CellType.ctBirth.value)
item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
self.ui.tableWidget.setItem(rowNo, FieldColNum.colBirth.value, item)
##民族
item = QTableWidgetItem(nation, CellType.ctNation.value)
item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
if (nation != "汉族"):
item.setForeground(QBrush(Qt.blue))
self.ui.tableWidget.setItem(rowNo, FieldColNum.colNation.value, item)
##分数
strScore = str(score)
item = QTableWidgetItem(strScore, CellType.ctScore.value)
item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
self.ui.tableWidget.setItem(rowNo, FieldColNum.colScore.value, item)
##党员
item = QTableWidgetItem("党员", CellType.ctPartyM.value)
item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
if (isParty == True):
item.setCheckState(Qt.Checked)
else:
item.setCheckState(Qt.Unchecked)
item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsUserCheckable) # 不允许编辑文字
item.setBackground(QBrush(Qt.yellow))
self.ui.tableWidget.setItem(rowNo, FieldColNum.colPartyM.value, item)
@pyqtSlot(int,int,int,int) ##当前单元格发生变化
def on_tableWidget_currentCellChanged(self,currentRow,currentColumn,previousRow,previousColumn):
if (self.__tableInitialized == False): # 表格数据未初始化
return
item = self.ui.tableWidget.item(currentRow, currentColumn)
if (item == None):
return
self.LabCellIndex.setText("当前单元格: %d行,%d列" %(currentRow, currentColumn))
itemCellType = item.type() # 获取单元格的类型
self.LabCellType.setText("当前单元格类型:%d" %itemCellType)
item2 = self.ui.tableWidget.item(currentRow, FieldColNum.colName.value)
studID = item2.data(Qt.UserRole) # 读取用户自定义数据
self.LabStudID.setText("学生ID:%d" %studID)
@pyqtSlot() # 插入行
def on_pushButton_4_clicked(self):
curRow = self.ui.tableWidget.currentRow() # 当前行号
self.ui.tableWidget.insertRow(curRow)
birth = QDate.fromString("1998-4-5","yyyy-M-d")
self.__createItemsARow(curRow,"新学生","男", birth,"苗族",True, 65)
@pyqtSlot() # 添加行
def on_pushButton_5_clicked(self):
curRow = self.ui.tableWidget.rowCount()
self.ui.tableWidget.insertRow(curRow)
birth = QDate.fromString("1999-1-10","yyyy-M-d")
self.__createItemsARow(curRow, "新生","女", birth, "土家族",False,86)
@pyqtSlot() ##删除当前行
def on_pushButton_6_clicked(self):
curRow = self.ui.tableWidget.currentRow()# 当前行号
self.ui.tableWidget.removeRow(curRow)
@pyqtSlot() # 清空表格内容
def on_pushButton_7_clicked(self):
self.ui.tableWidget.clearContents()
@pyqtSlot() ##自动行高
def on_pushButton_8_clicked(self):
self.ui.tableWidget.resizeRowsToContents()
@pyqtSlot() ##自动列宽
def on_pushButton_9_clicked(self):
self.ui.tableWidget.resizeColumnsToContents()
@pyqtSlot(bool) ##"表格可编辑"
def on_checkBox_clicked(self,checked):
if (checked):
trig = (QAbstractItemView.DoubleClicked | QAbstractItemView.SelectedClicked)
else:
trig = QAbstractItemView.NoEditTriggers
# 不允许编辑
self.ui.tableWidget.setEditTriggers(trig)
@pyqtSlot(bool) ##是否显示行表头
def on_checkBox_3_clicked(self, checked):
self.ui.tableWidget.horizontalHeader().setVisible(checked)
@pyqtSlot(bool) # 是否显示列表头
def on_checkBox_4_clicked(self, checked):
self.ui.tableWidget.verticalHeader().setVisible(checked)
@pyqtSlot(bool) ##间隔行底色
def on_checkBox_2_clicked(self, checked):
self.ui.tableWidget.setAlternatingRowColors(checked)
@pyqtSlot() ##选择模式:行选择
def on_radioButton_clicked(self):
selMode = QAbstractItemView.SelectRows
self.ui.tableWidget.setSelectionBehavior(selMode)
@pyqtSlot() ##选择模式:单元格选择
def on_radioButton_2_clicked(self):
selMode = QAbstractItemView.SelectItems
self.ui.tableWidget.setSelectionBehavior(selMode)
@pyqtSlot() ##读取表格到文本
def on_pushButton_10_clicked(self):
self.ui.plainTextEdit.clear()
rowCount = self.ui.tableWidget.rowCount() # 行数
colCount = self.ui.tableWidget.columnCount() # 列表
for i in range(rowCount):
strText = "第 %d行:" % (i + 1)
for j in range(colCount-1):
cellItem = self.ui.tableWidget.item(i, j)
strText = strText + cellItem.text() + " "
cellItem = self.ui.tableWidget.item(i, colCount - 1) # 最后一列
if (cellItem.checkState() == Qt.Checked):
strText = strText + "党员"
else:
strText = strText + "群众"
self.ui.plainTextEdit.appendPlainText(strText)
if __name__ == "__main__": ##用于当前窗体测试
app=QApplication(sys.argv) #创建GUI应用程序
form=QmyMainWindow() #创建窗体
form.show()
sys.exit(app.exec_())
4.appMain.py文件
## GUI应用程序主程序
import sys
from PyQt5.QtWidgets import QApplication
from myWidget import QmyMainWindow
app = QApplication(sys.argv) # 创建app,用QApplication类
myWidget = QmyMainWindow() #创建窗体
myWidget.show()
sys.exit(app.exec_())
5.最终效果图
动态展示:
转载请注明出处,欢迎讨论和交流!