https://doc.qt.io/qtforpython/PySide6/QtWidgets/QTableWidget.html
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QDesktopWidget, QHBoxLayout, QVBoxLayout, QTableWidget, \
QTableWidgetItem
from PyQt5.QtWidgets import QPushButton, QLineEdit,QLabel
class MainWindow(QWidget):
def __init__(self):
super().__init__()
# 窗体标题和尺寸
self.setWindowTitle("NB的xx系统")
# 窗体的尺寸
self.resize(1228, 450)
# 窗体的位置
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
# 垂直方向的布局
layout = QVBoxLayout()
# 1.创建顶部菜单布局
header_layout = QHBoxLayout()
layout.addLayout(header_layout)
# 1.1 创建按钮
self.btn_start = self.addButton(header_layout, '开始')
self.btn_start = self.addButton(header_layout, '停止')
self.addStretch(header_layout)
# 2.创建上面标题布局
form_layout = QHBoxLayout()
layout.addLayout(form_layout)
# 2.1 输入框
self.txt_asin = self.addLineEdit(form_layout, "请输入商品ID和价格,列入:B0818JJQQ8=88")
# 2.2 添加按钮
self.btn_add = self.addButton(form_layout, '添加')
# 3.创建中间的表格
table_layout = QHBoxLayout()
layout.addLayout(table_layout)
# 3.1 创建表格
title_dict = [
{'field':'asin','text':'ASIN','width':120},
{'field':'title','text':'标题','width':150},
{'field':'url','text':'URL','width':400},
{'field':'price','text':'底价','width':100},
{'field':'success','text':'成功次数','width':100},
{'field':'error','text':'503次数','width':100},
{'field':'status','text':'状态','width':100},
{'field':'frequency','text':'频率(N秒/次)','width':100},
]
self.table_widget = self.addTable(layout,title_dict,8,0)
# 4.创建底部菜单
footer_layout = QHBoxLayout()
layout.addLayout(footer_layout)
self.label_status = self.addLabel(footer_layout,'未检测')
self.addStretch(footer_layout)
self.btn_reinit = self.addButton(footer_layout, '重新初始化')
self.btn_recheck = self.addButton(footer_layout, '重新检查')
self.btn_reset_count = self.addButton(footer_layout, '次数清零')
self.btn_delete = self.addButton(footer_layout, '删除检测项')
self.btn_alert = self.addButton(footer_layout, 'SMTP报警配置')
self.btn_proxy = self.addButton(footer_layout, '代理IP')
# 给窗体设置元素的排列方式
self.setLayout(layout)
def addButton(self, layout, button_name):
button = QPushButton(button_name)
layout.addWidget(button)
return button
def addLabel(self, layout, text):
label = QLabel(text)
layout.addWidget(label)
return label
def addStretch(self, layout):
layout.addStretch()
def addLineEdit(self, layout, place_holder_text):
line_edit = QLineEdit()
line_edit.setPlaceholderText(place_holder_text)
layout.addWidget(line_edit)
return line_edit
def addTable(self, layout, title_list, col, row):
table = QTableWidget(row, col)
for index, value in enumerate(title_list):
item = QTableWidgetItem()
item.setText(value['text'])
table.setColumnWidth(index,value['width'])
table.setHorizontalHeaderItem(index, item)
layout.addWidget(table)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())