奇趣5分彩

PyQt5完奇趣5分彩tableWidget 居奇趣5分彩显现

 更新时候:2022年07月12日 11:32:11   作者:shiyue41  
这篇文章首要先容了PyQt5完奇趣5分彩tableWidget 居奇趣5分彩显现体例,具备很奇趣5分彩的参考代价,但愿对大师奇趣5分彩所赞助。若奇趣5分彩毛病或未斟酌完整的处所,望不吝见教

PyQt5 tableWidget 居奇趣5分彩显现

newItem = QTableWidgetItem("内容")
# 居奇趣5分彩显现
newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)

PyQt5 TableWidGet题目

利用pyqt5展现excel的数据到桌面,并获得选奇趣5分彩的数据内容

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import pandas as pd
import numpy as np

class Ui_MainWindow(QMainWindow):
    def __init__(self):
        super(QtWidgets.QMainWindow, self).__init__()
        self.setupUi(self)
        self.retranslateUi(self)

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(666, 488)
        self.centralWidget = QtWidgets.QWidget(MainWindow)
        self.centralWidget.setObjectName("centralWidget")
        self.retranslateUi(MainWindow)
        self.tableWidget = QtWidgets.QTableWidget(self.centralWidget)
        self.tableWidget.setGeometry(QtCore.QRect(0, 60, 813, 371))
        self.tableWidget.setObjectName("tableWidget")
        self.tableWidget.setColumnCount(0)
        self.tableWidget.setRowCount(0)
        self.tableWidget.setStyleSheet("selection-background-color:pink")
        self.tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)
        self.tableWidget.setSelectionBehavior(QTableWidget.SelectRows)
        self.tableWidget.raise_()
        # 设置图标

        self.pushButton = QtWidgets.QPushButton(self.centralWidget)
        self.pushButton.setGeometry(QtCore.QRect(90, 20, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.pushButton.setText("翻开")
        MainWindow.setCentralWidget(self.centralWidget)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
        self.pushButton.clicked.connect(self.openfile)
        self.pushButton.clicked.connect(self.creat_table_show)
        # 肯定
        self.okButton = QtWidgets.QPushButton(self.centralWidget)
        self.okButton.setGeometry(QtCore.QRect(180, 20, 75, 23))
        self.okButton.setObjectName("okButton")
        self.okButton.setText("肯定")
        MainWindow.setCentralWidget(self.centralWidget)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
        self.okButton.clicked.connect(self.get_select)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "测试数据"))
        MainWindow.setWindowIcon(QIcon("./head.jpg"))
        # MainWindow.show()

    def get_select(self):
        # print(self.tableWidget.columnCount()) # 前往列数
        # print(self.tableWidget.rowCount())  # 前往行数
        colomn = self.tableWidget.columnCount()
        row_list = set()
        for i in self.tableWidget.selectionModel().selection().indexes():
            row_list.add(i.row())
        # print(row_list)
        select_data = []
        for row in row_list:
            row_data = [self.tableWidget.item(row, p).text() for p in range(colomn)]
            select_data.append(row_data)
        print(select_data)

    def openfile(self):
        # 获得途径
        openfile_name = QFileDialog.getOpenFileName(self, '挑选文件', '', 'Excel files(*.xlsx , *.xls)')
        #print(openfile_name)
        global path_openfile_name
        path_openfile_name = openfile_name[0]

    def creat_table_show(self):
        # 读取表格,转换表格
        if len(path_openfile_name) > 0:
            input_table = pd.read_excel(path_openfile_name)
            # print(1,input_table)
            input_table_rows = input_table.shape[0]
            input_table_colunms = input_table.shape[1]
            # print(2,input_table_rows)
            # print(3,input_table_colunms)
            input_table_header = input_table.columns.values.tolist()
            #print(input_table_header)
            #读取表格,转换表格,给tablewidget设置行列表头
            self.tableWidget.setColumnCount(input_table_colunms)
            self.tableWidget.setRowCount(input_table_rows)
            self.tableWidget.setHorizontalHeaderLabels(input_table_header)
            #给tablewidget设置行列表头
            #遍历表格每一个元素,同时增加到tablewidget奇趣5分彩
            for i in range(input_table_rows):
                input_table_rows_values = input_table.iloc[[i]]
                #print(input_table_rows_values)
                input_table_rows_values_array = np.array(input_table_rows_values)
                input_table_rows_values_list = input_table_rows_values_array.tolist()[0]
                 #print(input_table_rows_values_list)
                for j in range(input_table_colunms):
                    input_table_items_list = input_table_rows_values_list[j]
                    #print(input_table_items_list)
                    # print(type(input_table_items_list))
                    #将遍历的元素增加到tablewidget奇趣5分彩并显现
                    input_table_items = str(input_table_items_list)
                    newItem = QTableWidgetItem(input_table_items)
                    newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                    self.tableWidget.setItem(i, j, newItem)
        #遍历表格每一个元素,同时增加到tablewidget奇趣5分彩
        else:
            self.centralWidget.show()
if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

以上为小我经历,但愿能给大师一个参考,也但愿大师多多撑持剧本之奇趣5分彩。

相干文章

最新批评