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.setWindowFlags(Qt.WindowMinimizeButtonHint | Qt.WindowMaximizeButtonHint | Qt.WindowCloseButtonHint) self.download_btn = QPushButton('下载数据', self) self.download_btn.clicked.connect(self.download_data) # 创建表格控件 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)这个界面 怎么添加一个号下载界面所有数据的按钮
时间: 2023-11-22 14:55:04 浏览: 69
python-mysql.zip_MYSQL_pymysql_python MySQL_python连接mysql_连接数据库
5星 · 资源好评率100%
你可以在QueryResultWindow类中添加一个下载按钮,并连接到一个下载数据的函数。这个函数可以使用pandas库将查询结果转成DataFrame,然后保存为csv文件。
以下是修改后的代码:
```
from PyQt5.QtWidgets import QPushButton, QMessageBox
import pandas as pd
class QueryResultWindow(QWidget):
def __init__(self):
super().__init__()
# 设置窗口大小
self.setFixedSize(800, 600)
self.setWindowFlags(Qt.WindowMinimizeButtonHint | Qt.WindowMaximizeButtonHint | Qt.WindowCloseButtonHint)
# 创建表格控件
self.table_widget = QTableWidget()
self.table_widget.setEditTriggers(QTableWidget.NoEditTriggers)
self.table_widget.setSelectionBehavior(QTableWidget.SelectRows)
# 创建下载按钮
self.download_btn = QPushButton('下载数据', self)
self.download_btn.clicked.connect(self.download_data)
# 创建窗口布局
layout = QVBoxLayout()
layout.addWidget(self.table_widget)
layout.addWidget(self.download_btn)
self.setLayout(layout)
def download_data(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
# 将查询结果转成DataFrame
columns = ["员工ID", "员工姓名", "日期", "领取鸡爪重量(KG)", "效率(每小时KG)", "出成率", "基础工资", "重量奖励", "当日总工资"]
df = pd.DataFrame(list(result), columns=columns)
# 保存为csv文件
save_path, _ = QFileDialog.getSaveFileName(self, "保存文件", "", "CSV Files (*.csv)")
if save_path:
df.to_csv(save_path, index=False)
QMessageBox.information(self, "提示", "数据已保存至{}".format(save_path))
```
阅读全文