最近新学了PyQt5中的QTableWidget, 总结了一下其的用法,具体如何用已经放入代码块中,若有错误,还望各位高手指正
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class TableWidgetDemo(QWidget):
def __init__(self):
super(TableWidgetDemo, self).__init__()
self.initUI()
def initUI(self):
self.setWindowTitle("QTableWidget")
self.resize(430, 230)
layout = QHBoxLayout()
tablewidget = QTableWidget()
tablewidget.setRowCount(4)
tablewidget.setColumnCount(3)
layout.addWidget(tablewidget)
# 设置水平的头标签
tablewidget.setHorizontalHeaderLabels(['姓名', '年龄', '籍贯'])
nameItem = QTableWidgetItem("老王")
tablewidget.setItem(0, 0, nameItem)
ageItem = QTableWidgetItem("45")
tablewidget.setItem(0, 1, ageItem)
jgItem = QTableWidgetItem("北京")
tablewidget.setItem(0, 2, jgItem)
# 禁止编辑
tablewidget.setEditTriggers(QAbstractItemView.NoEditTriggers)
# 整行显示
tablewidget.setSelectionBehavior(QAbstractItemView.SelectRows)
# 调整行和列
tablewidget.resizeRowsToContents()
tablewidget.resizeColumnsToContents()
# 表头的显示和隐藏(默认为True)
tablewidget.horizontalHeader().setVisible(False)
# 设置垂直的头标签,默认为1,2,3,4
tablewidget.setVerticalHeaderLabels(['a', 'b', 'c'])
# 隐藏表格线
tablewidget.setShowGrid(False)
self.setLayout(layout)
if __name__ == "__main__":
app = QApplication(sys.argv)
main = TableWidgetDemo()
main.show()
sys.exit(app.exec_())
来源:CSDN
作者:qq_45690024
链接:https://blog.csdn.net/qq_45690024/article/details/104405746