pyqt_data_analysis/MainWindows.py

69 lines
3.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Ui_MainWindow
from PyQt5.QtCore import QFile, QFileInfo, pyqtSlot, pyqtSignal
from PyQt5 import QtGui, QtWidgets, QtCore
from PyQt5.QtWidgets import QPushButton, QTableWidgetItem
from PyQt5.QtGui import QIcon, QPixmap
from data_set_read import Data_Read
import pandas as pd
import openpyxl as pyx
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()
self.head_num = 10
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":
self.data_read = Data_Read(file_path[0], suffix)
self.ui.lineEdit_filepath.setText(file_path[0])
elif suffix == "xls" or suffix == "xlsx":
self.data_read = Data_Read(file_path[0], suffix)
self.ui.lineEdit_filepath.setText(file_path[0])
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)
# if file_path is not None:
# self.ui.lineEdit_filepath.setText(file_path)
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开始
def pushButton_filepath_clear_clicked(self):
self.ui.lineEdit_filepath.clear()