1 #-*- coding:utf-8 -*-
2 '''
3 Created on 2018年7月19日
4
5 @author: Administrator
6 '''
7 import sys
8 from PyQt5.QtCore import Qt
9 from PyQt5.QtWidgets import QWidget, QApplication, QDesktopWidget, QTableWidget, QHBoxLayout, QTableWidgetItem,QComboBox, QFrame
10 from PyQt5.QtGui import QFont, QColor, QBrush, QPixmap
11
12 class TableSheet(QWidget):
13 def __init__(self):
14 super().__init__()
15 self.initUi()
16
17 def initUi(self):
18 horizontalHeader = ["编号", "姓名", "性别", "年龄", "职业"]
19
20 self.table = QTableWidget()
21 self.table.setColumnCount(5)
22 self.table.setRowCount(3)
23 self.table.setHorizontalHeaderLabels(horizontalHeader)
24 #self.table.setEditTriggers(QTableWidget.NoEditTriggers)#设置编辑模式
25 self.table.setEditTriggers(QTableWidget.SelectedClicked)
26 #self.table.setSelectionBehavior(QTableWidget.SelectColumns)#单元格选择设置
27 self.table.setSelectionBehavior(QTableWidget.SelectItems)
28
29 self.table.setSelectionMode(QTableWidget.ContiguousSelection)#行选择方式
30
31
32 for index in range(self.table.columnCount()):
33 headItem = self.table.horizontalHeaderItem(index)
34 headItem.setFont(QFont("song", 14, QFont.Bold))
35
36 headItem.setForeground(QBrush(Qt.gray))
37 headItem.setTextAlignment(Qt.AlignLeft | Qt.AlignVCenter)
38
39 self.table.setColumnWidth(4, 100)
40 self.table.setRowHeight(0, 40)
41 # self.table.setFrameShape(QFrame.HLine)#设定样式
42 # self.table.setShowGrid(False) #取消网格线
43 # self.table.verticalHeader().setVisible(False) #隐藏垂直表头
44
45 self.table.setItem(0, 0, QTableWidgetItem("001"))
46 self.table.setItem(0, 1, QTableWidgetItem("刘亦菲"))
47 genderComb = QComboBox()
48 genderComb.addItem("男")
49 genderComb.addItem("女")
50 genderComb.setCurrentIndex(1)
51 self.table.setCellWidget(0, 2, genderComb)
52 self.table.setItem(0, 3, QTableWidgetItem("30"))
53 self.table.setItem(0, 4, QTableWidgetItem("演员"))
54
55
56 self.table.setItem(1, 0, QTableWidgetItem("002"))
57 self.table.setItem(1, 1, QTableWidgetItem("马云"))
58 genderComb = QComboBox()
59 genderComb.addItem("男")
60 genderComb.addItem("女")
61 genderComb.setCurrentIndex(0)
62 self.table.setCellWidget(1, 2, genderComb)
63 self.table.setItem(1, 3, QTableWidgetItem("50"))
64 self.table.setItem(1, 4, QTableWidgetItem("企业家"))
65
66 self.table.setItem(2, 0, QTableWidgetItem("003"))
67 self.table.setItem(2, 1, QTableWidgetItem("牛油刀"))
68 genderComb = QComboBox()
69 genderComb.addItem("男")
70 genderComb.addItem("女")
71 genderComb.setCurrentIndex(0)
72 self.table.setCellWidget(2, 2, genderComb)
73 self.table.setItem(2, 3, QTableWidgetItem("28"))
74 self.table.setItem(2, 4, QTableWidgetItem("企业家"))
75
76
77 row_count = self.table.rowCount()
78 self.table.insertRow(row_count)
79 mainLayout = QHBoxLayout()
80 mainLayout.addWidget(self.table)
81 self.setLayout(mainLayout)
82
83 self.resize(800, 280)
84 self.center()
85 self.setWindowTitle("表格演示")
86
87 def center(self):
88 qr = self.frameGeometry()
89 cp = QDesktopWidget().availableGeometry().center()
90 qr.moveCenter(cp)
91 self.move(qr.topLeft())
92
93
94
95 if __name__ =="__main__":
96 app = QApplication(sys.argv)
97 table = TableSheet()
98 table.show()
99 sys.exit(app.exec_())
100