pyqt_data_analysis/MainWindows.py

69 lines
3.6 KiB
Python
Raw Normal View History

2024-06-08 03:00:08 +08:00
import Ui_MainWindow
from PyQt5.QtCore import QFile, QFileInfo, pyqtSlot, pyqtSignal
from PyQt5 import QtGui, QtWidgets, QtCore
2024-06-08 19:26:36 +08:00
from PyQt5.QtWidgets import QPushButton, QTableWidgetItem
2024-06-08 03:00:08 +08:00
from PyQt5.QtGui import QIcon, QPixmap
from data_set_read import Data_Read
2024-06-08 19:26:36 +08:00
import pandas as pd
import openpyxl as pyx
2024-06-08 03:00:08 +08:00
class MainWindows:
def __init__(self) -> None:
self.MainWindow = QtWidgets.QMainWindow()
self.ui = Ui_MainWindow.Ui_MainWindow()
self.ui.setupUi(self.MainWindow)
self.window_ui_init()
self.file_object_init()
2024-06-08 19:26:36 +08:00
self.head_num = 10
2024-06-08 03:00:08 +08:00
def window_ui_init(self):
self.MainWindow.setWindowTitle("Data Analysis By Python")
self.MainWindow.setWindowIcon(QIcon(QPixmap(":/src/image/icon.jpg")))
self.ui.progressBar_linear_regression.setValue(0)
def file_object_init(self):
self.ui.pushButton_filepath.clicked.connect(self.pushButton_filepath_clicked)
self.ui.pushButton_filepath_clear.clicked.connect(self.pushButton_filepath_clear_clicked)
self.ui.pushButton_linear_regression.clicked.connect(lambda checked: self.ui.stackedWidget.setCurrentIndex(0))
self.ui.pushButton_Kmeans.clicked.connect(lambda checked: self.ui.stackedWidget.setCurrentIndex(1))
def pushButton_filepath_clicked(self):
file_path = QtWidgets.QFileDialog.getOpenFileName(self.MainWindow, "Open File", ".", "All Files(*);;csv Files(*.csv);;Old Excel Files(*.xls);;New Excel Files(*.xlsx)")
print(file_path[0])
info = QFileInfo(file_path[0])
suffix = info.suffix()
if suffix == "csv":
2024-06-08 19:26:36 +08:00
self.data_read = Data_Read(file_path[0], suffix)
2024-06-08 03:00:08 +08:00
self.ui.lineEdit_filepath.setText(file_path[0])
elif suffix == "xls" or suffix == "xlsx":
2024-06-08 19:26:36 +08:00
self.data_read = Data_Read(file_path[0], suffix)
2024-06-08 03:00:08 +08:00
self.ui.lineEdit_filepath.setText(file_path[0])
2024-06-08 19:26:36 +08:00
if self.ui.stackedWidget.currentIndex() == 0:
self.tableWidget_linear_regression_show()
#data_show = self.data_read.get_head_data(self.head_num)
# self.tableWidget_linear_regression_show(data_show)
2024-06-08 03:00:08 +08:00
# if file_path is not None:
# self.ui.lineEdit_filepath.setText(file_path)
2024-06-08 19:26:36 +08:00
def tableWidget_linear_regression_show(self):
# self.ui.tableWidget_linear_regression.clear()
# # self.ui.tableWidget_linear_regression.setRowCount(data_frame.shape[0])
# # self.ui.tableWidget_linear_regression.setColumnCount(data_frame.shape[1])
# # for row_index, row in data_frame.iterrows():
# # for col_index, data in enumerate(row):
# # item = QTableWidgetItem(str(data))
# # self.ui.tableWidget_linear_regression.setItem(row_index, col_index, item)
self.ui.tableWidget_linear_regression.clear()
workbook = pyx.load_workbook(self.ui.lineEdit_filepath.text())
sheet = workbook.worksheets[0]
self.ui.tableWidget_linear_regression.setRowCount(self.head_num)
self.ui.tableWidget_linear_regression.setColumnCount(sheet.max_column)
for row in sheet.iter_rows(min_row=1, max_row=self.head_num, max_col=sheet.max_column, values_only=True):
print(row)
for col, data in enumerate(row, start=0):
item = QTableWidgetItem(str(data) if data is not None else "")
self.ui.tableWidget_linear_regression.setItem(row[0] - 1, col, item) # 注意行索引从0开始但Excel从1开始
2024-06-08 03:00:08 +08:00
def pushButton_filepath_clear_clicked(self):
self.ui.lineEdit_filepath.clear()