python 实现表格(实例)
2020-02-29 python 1854
import sys from PyQt5.QtWidgets import QWidget, QMainWindow, QApplication, QDesktopWidget, QHeaderView, QTableWidgetItem, QToolBar, \ QHBoxLayout, QAction, QAbstractItemView, QTableView, QTableWidget, QPushButton from PyQt5.QtGui import QIcon class MainWin(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): toolbar = QToolBar() toolbar.addAction("创建配置文件") toolbar.addAction("测试规则") # toolbar.actionTriggered[QAction].connect(self.createConfig) self.addToolBar(toolbar) self.setWindowIcon(QIcon("logo.png")) # self.setWindowFlags(Qt.WindowStaysOnTopHint) self.setContentsMargins(0, 0, 0, 0) layout = QHBoxLayout() # self.setStyleSheet("background-color:#fff;") self.tableWidget = QTableWidget() # tableWidget.horizontalHeader().setVisible(False)#设置列标题隐藏(针对列标题横向排列) # tableWidget.verticalHeader().setVisible(False)#设置列标题隐藏(针对列标题纵向排列) self.tableWidget.setSelectionMode(QAbstractItemView.SingleSelection) # 设置只能选中一行 self.tableWidget.setEditTriggers(QTableView.NoEditTriggers) # 不可编辑 self.tableWidget.setSelectionBehavior(QAbstractItemView.SelectRows) # 设置只有行选中 self.tableWidget.resizeColumnsToContents() # 设置列宽高按照内容自适应 self.tableWidget.resizeRowsToContents() # 设置行宽和高按照内容自适应 #tableWidget.horizontalHeader().setSectionResizeMode(0, QHeaderView.ResizeToContents) # 第一列 自适应 #tableWidget.horizontalHeader().setSectionResizeMode(1, QHeaderView.ResizeToContents) # 第二列 自适应 self.initTable() layout.addWidget(self.tableWidget) # self.timer = QTimer() # self.timer.setInterval(1000) # self.timer.start() # self.timer.timeout.connect(self.initTable) wg = QWidget() wg.setLayout(layout) self.setCentralWidget(wg) self.setWindowTitle("网站管理") self.setGeometry(300, 300, 600, 300) self.center() def initTable(self): # print("init table") self.lists = {0: {"name": "test1", "path": "aaa", "total": "3", "index": 1}} self.tableWidget.setRowCount(len(self.lists)) # 行数 self.tableWidget.setColumnCount(5) # 列数 self.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) # 所有列自动拉伸,充满界面 self.tableWidget.horizontalHeader().setSectionResizeMode(2, QHeaderView.ResizeToContents) self.tableWidget.setHorizontalHeaderLabels( ['文件', '地址', '总章数', '更新章节', '操作']) # 横向标题排列,如果使用setVerticalHeaderLabels则是纵向排列标题 # items = [['JONES'],['JONES'],['JONES'],['JONES'],['JONES']] items = [] for x in self.lists: items.append( [self.lists[x]["name"], self.lists[x]["path"], self.lists[x]["total"], self.lists[x]["index"], ]) for i in range(len(items)): # 注意上面列表中数字加单引号,否则下面不显示(或者下面str方法转化一下即可) item = items[i] for j in range(len(item)): item = QTableWidgetItem(str(items[i][j])) self.tableWidget.setItem(i, j, item) index = len(items[0]) for i in range(len(items)): self.tableWidget.setCellWidget(i, index, self.buttonForRow(i)) def buttonForRow(self, id): widget = QWidget() # 修改 caijiBtn = QPushButton("采集") caijiBtn.setStyleSheet('''QPushButton{ text-align : center;width:50px;background-color : #4169E1;color:#fff; height : 30px; border-style: outset; font : 13px} QPushButton:hover{background-color:#0000CD} ''') caijiBtn.clicked.connect(lambda: self.caiji(id)) openBtn = QPushButton("打开") openBtn.setStyleSheet('''QPushButton{ text-align : center;width:50px;background-color : #4169E1;color:#fff; height : 30px; border-style: outset; font : 13px} QPushButton:hover{background-color:#0000CD} ''') openBtn.clicked.connect(lambda: self.opendir(id)) hLayout = QHBoxLayout() hLayout.addWidget(caijiBtn) hLayout.addWidget(openBtn) hLayout.setContentsMargins(5, 2, 5, 2) widget.setLayout(hLayout) return widget def center(self): qr = self.frameGeometry() cp = QDesktopWidget().availableGeometry().center() qr.moveCenter(cp) self.move(qr.topLeft()) if __name__ == "__main__": app = QApplication(sys.argv) ex = MainWin() ex.show() sys.exit(app.exec_())
很赞哦! (0)
相关文章
文章评论
-
-
-
0条评论