self.order_table = QTableWidget(self.central_widget) self.order_table.setColumnCount(4) self.order_table.setHorizontalHeaderLabels(['产品', '单价', '数量', '金额']) self.order_table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)中"QHeaderView" is not defined
时间: 2024-02-13 14:07:04 浏览: 194
这个错误通常是因为您没有正确导入QHeaderView模块。请确保在您的代码中导入了QHeaderView模块,例如:
```python
from PyQt5.QtWidgets import QHeaderView
```
如果您已经导入了QHeaderView模块,那么请检查是否拼写错误或者大小写不正确。另外,您还需要确认PyQt5是否已经正确安装。
相关问题
class MainWindow(QMainWindow): def init(self): super().init() self.setFixedSize(800, 600) main_layout = QVBoxLayout() central_widget = QWidget() central_widget.setLayout(main_layout) self.setCentralWidget(central_widget) button_layout = QVBoxLayout() button1 = QPushButton('当日员工工资') button1.setFixedSize(200, 50) button1.clicked.connect(self.show_query1_result) button_layout.addStretch() button_layout.addWidget(button1) button_layout.addStretch() layout = QHBoxLayout() layout.addStretch() layout.addLayout(button_layout) layout.addStretch() widget = QWidget() widget.setLayout(layout) self.setCentralWidget(widget) main_layout.addLayout(button_layout) self.query1_window = QueryResultWindow() def show_query1_result(self): db = pymysql.connect(host='39.99.214.172', user='root', password='Solotion.123', db='jj_tset') cursor = db.cursor() db_sql = """ """ cursor.execute(db_sql) result = cursor.fetchall() db.close() if len(result) == 0: QMessageBox.information(self, "提示", "今日无员工工资记录") return self.query1_window.table_widget.setRowCount(0) self.query1_window.table_widget.setColumnCount(len(result[0])) self.query1_window.table_widget.setHorizontalHeaderLabels( ["员工ID", "员工姓名", "日期", "领取鸡爪重量(KG)", "效率(每小时KG)", "出成率", "基础工资", "重量奖励", "当日总工资"]) for row_num, row_data in enumerate(result): self.query1_window.table_widget.insertRow(row_num) for col_num, col_data in enumerate(row_data): self.query1_window.table_widget.setItem(row_num, col_num, QTableWidgetItem(str(col_data))) self.query1_window.show() class QueryResultWindow(QWidget): def init(self): super().init() self.setFixedSize(800, 600) self.table_widget = QTableWidget() self.table_widget.setEditTriggers(QTableWidget.NoEditTriggers) self.table_widget.setSelectionBehavior(QTableWidget.SelectRows) layout = QVBoxLayout() layout.addWidget(self.table_widget) self.setLayout(layout) if name == 'main': app = QApplication(sys.argv) loginWindow = LoginWindow() loginWindow.show() sys.exit(app.exec_()))数据展示页面怎么设置筛选器按ID筛选结果并展示的整体代码
以下是整体代码:
```
import sys
import pymysql
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QMessageBox, QTableWidget, QTableWidgetItem
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setFixedSize(800, 600)
main_layout = QVBoxLayout()
central_widget = QWidget()
central_widget.setLayout(main_layout)
self.setCentralWidget(central_widget)
button_layout = QVBoxLayout()
button1 = QPushButton('当日员工工资')
button1.setFixedSize(200, 50)
button1.clicked.connect(self.show_query1_result)
button_layout.addStretch()
button_layout.addWidget(button1)
button_layout.addStretch()
layout = QHBoxLayout()
layout.addStretch()
layout.addLayout(button_layout)
layout.addStretch()
widget = QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
main_layout.addLayout(button_layout)
self.query1_window = QueryResultWindow()
def show_query1_result(self):
db = pymysql.connect(host='39.99.214.172', user='root', password='Solotion.123', db='jj_tset')
cursor = db.cursor()
db_sql = """SELECT * FROM employee_salary WHERE emp_id = %s"""
emp_id, okPressed = QInputDialog.getInt(self, "员工ID筛选", "请输入员工ID:", 0, 0, 999999, 1)
if not okPressed:
return
cursor.execute(db_sql, (emp_id,))
result = cursor.fetchall()
db.close()
if len(result) == 0:
QMessageBox.information(self, "提示", "今日无员工工资记录")
return
self.query1_window.table_widget.setRowCount(0)
self.query1_window.table_widget.setColumnCount(len(result[0]))
self.query1_window.table_widget.setHorizontalHeaderLabels(["员工ID", "员工姓名", "日期", "领取鸡爪重量(KG)", "效率(每小时KG)", "出成率", "基础工资", "重量奖励", "当日总工资"])
for row_num, row_data in enumerate(result):
self.query1_window.table_widget.insertRow(row_num)
for col_num, col_data in enumerate(row_data):
self.query1_window.table_widget.setItem(row_num, col_num, QTableWidgetItem(str(col_data)))
self.query1_window.show()
class QueryResultWindow(QWidget):
def __init__(self):
super().__init__()
self.setFixedSize(800, 600)
self.table_widget = QTableWidget()
self.table_widget.setEditTriggers(QTableWidget.NoEditTriggers)
self.table_widget.setSelectionBehavior(QTableWidget.SelectRows)
layout = QVBoxLayout()
layout.addWidget(self.table_widget)
self.setLayout(layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())
```
主要是在 `show_query1_result` 方法中添加了一个输入框,用于输入要筛选的员工ID,然后根据输入的ID从数据库中查询数据并展示在表格中。注意要导入 `QInputDialog`,这是一个用于获取用户输入的对话框。
def query(self,sql): # 执行 SQL 查询语句 self.cursor.execute(sql) # 创建一个新窗口,并设置其属性 new_window = QWidget(self) new_window.setWindowTitle("查询员工信息") new_window.setGeometry(200, 200, 400, 300) # 创建返回按钮并设置其属性 return_button = QPushButton("返回", new_window) return_button.setGeometry(200, 200, 400, 300) return_button.clicked.connect(new_window.close) # 获取查询结果并显示在表格中 rows = self.cursor.fetchall() self.table = QTableWidget() if rows: self.table.setRowCount(len(rows)) self.table.setColumnCount(len(rows[0])) for i, row in enumerate(rows): for j, col in enumerate(row): item = QTableWidgetItem(str(col)) self.table.setItem(i, j, item) self.table.setHorizontalHeaderLabels(["部门", "姓名", "性别", '职位', '工号', '状态']) self.table.horizontalHeader().setVisible(True) self.input_box = QLineEdit(new_window) self.status_label.setText("输入需要查询的数据,格式为:部门/姓名/性别/职位/工号/状态,对象") self.v_layout = QVBoxLayout(new_window) self.v_layout.addWidget(self.input_box) query_button = QPushButton("单个查询", new_window) query_button.setGeometry(300, 100, 50, 30) query_button.clicked.connect(lambda: self.query_data(new_window)) # 添加表格到新窗口中 self.v_layout.addWidget(self.table) new_window.setLayout(self.v_layout) new_window.show() def query_data(self, new_window): # 获取输入框中的数据 data = self.input_box.text().split(",") if len(data) != 2 or not all(map(lambda x: x.strip(), data)): self.status_label.setText("输入格式错误!请按照 部门/姓名/性别/职位/工号/状态,对象 格式输入。") return # 检查是否有存在的表 if self.table: self.v_layout.removeWidget(self.table) self.table = None # 执行 SQL 查询语句 sql = "select * from Staff where {} = (?)".format(data[0]) self.cursor.execute(sql, (data[1],)) # 获取查询结果并显示在表格中 rows = self.cursor.fetchall() self.table = QTableWidget() if rows: self.table.setRowCount(len(rows)) self.table.setColumnCount(len(rows[0])) for i, row in enumerate(rows): for j, col in enumerate(row): item = QTableWidgetItem(str(col)) self.table.setItem(i, j, item) self.table.setHorizontalHeaderLabels(["部门", "姓名", "性别", '职位', '工号', '状态']) self.table.horizontalHeader().setVisible(True) # 添加表格到新窗口中 self.v_layout = QVBoxLayout(new_window) self.v_layout.addWidget(self.table) new_window.setLayout(self.v_layout) new_window.show()改错中文
这段代码中存在一些中文错误,我来帮你改正一下:
```python
def query(self, sql):
# 执行 SQL 查询语句
self.cursor.execute(sql)
# 创建一个新窗口,并设置其属性
new_window = QWidget(self)
new_window.setWindowTitle("查询员工信息")
new_window.setGeometry(200, 200, 400, 300)
# 创建返回按钮并设置其属性
return_button = QPushButton("返回", new_window)
return_button.setGeometry(10, 10, 50, 30) # 修改返回按钮的位置和大小
return_button.clicked.connect(new_window.close)
# 获取查询结果并显示在表格中
rows = self.cursor.fetchall()
self.table = QTableWidget()
if rows:
self.table.setRowCount(len(rows))
self.table.setColumnCount(len(rows[0]))
for i, row in enumerate(rows):
for j, col in enumerate(row):
item = QTableWidgetItem(str(col))
self.table.setItem(i, j, item)
self.table.setHorizontalHeaderLabels(["部门", "姓名", "性别", '职位', '工号', '状态'])
self.table.horizontalHeader().setVisible(True)
self.input_box = QLineEdit(new_window)
# 修改状态标签的内容
self.status_label.setText("输入需要查询的数据,格式为:部门/姓名/性别/职位/工号/状态,对象")
self.v_layout = QVBoxLayout(new_window)
self.v_layout.addWidget(self.input_box)
query_button = QPushButton("单个查询", new_window)
query_button.setGeometry(300, 100, 50, 30) # 修改查询按钮的位置和大小
query_button.clicked.connect(lambda: self.query_data(new_window))
# 添加表格到新窗口中
self.v_layout.addWidget(self.table)
new_window.setLayout(self.v_layout)
new_window.show()
def query_data(self, new_window):
# 获取输入框中的数据
data = self.input_box.text().split(",")
if len(data) != 2 or not all(map(lambda x: x.strip(), data)):
# 修改状态标签的内容
self.status_label.setText("输入格式错误!请按照 部门/姓名/性别/职位/工号/状态,对象 格式输入。")
return
# 检查是否有存在的表
if self.table:
self.v_layout.removeWidget(self.table)
self.table = None
# 执行 SQL 查询语句
sql = "select * from Staff where {} = (?)".format(data[0])
self.cursor.execute(sql, (data[1],))
# 获取查询结果并显示在表格中
rows = self.cursor.fetchall()
self.table = QTableWidget()
if rows:
self.table.setRowCount(len(rows))
self.table.setColumnCount(len(rows[0]))
for i, row in enumerate(rows):
for j, col in enumerate(row):
item = QTableWidgetItem(str(col))
self.table.setItem(i, j, item)
self.table.setHorizontalHeaderLabels(["部门", "姓名", "性别", '职位', '工号', '状态'])
self.table.horizontalHeader().setVisible(True)
# 添加表格到新窗口中
self.v_layout = QVBoxLayout(new_window)
self.v_layout.addWidget(self.table)
new_window.setLayout(self.v_layout)
new_window.show()
```
阅读全文