pyqt5 - TableWidGet

 pyqt5を使用したデータショーの内容は、デスクトップにデータをExcel、および選択します

PyQt5 輸入QtCore、QtGui、QtWidgets
 から PyQt5.QtGuiの輸入QIcon
 から PyQt5.QtWidgetsがインポート *
 から PyQt5.QtCoreのインポート *の
 インポートPDのようパンダ
 輸入のNPとしてnumpyの


クラス:Ui_MainWindow(QMainWindow)

    デフ __init__ (自己):
        スーパー(QtWidgets。 QMainWindow、自己)。__init__ ()
        self.setupUi(自己)
        self.retranslateUi(自己)


    デフsetupUi(自己、メインウィンドウ):
        MainWindow.setObjectName("メインウィンドウ" 
        MainWindow.resize( 666、488 
        self.centralWidget = QtWidgets.QWidget(メインウィンドウ)
        self.centralWidget.setObjectName(" centralWidget 
        self.retranslateUi(メインウィンドウ)

        self.tableWidget = QtWidgets.QTableWidget(self.centralWidget)
        自己.tableWidget.setGeometry(QtCore.QRect(0、 60、813、371 ))
        self.tableWidget.setObjectName(" tableWidget " 
        self.tableWidget.setColumnCount(0) 
        self.tableWidget.setRowCount(0)
        self.tableWidget.setStyleSheet(" 選択背景色:ピンク" 
        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(" プッシュボタン" 
        self.pushButton.setText(" 打开" 
        MainWindow.setCentralWidget(self.centralWidget)
        QtCore.QMetaObject.connectSlotsByName(メインウィンドウ)

        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(" OKボタン" 
        self.okButton.setText(" 确定" 
        MainWindow.setCentralWidget(self.centralWidget ) 
        QtCore.QMetaObject.connectSlotsByName(メインウィンドウ)

        self.okButton.clicked.connect (self.get_select)


    デフretranslateUi(自己、メインウィンドウ): 
        _translate = QtCore.QCoreApplication.translate 
        MainWindow.setWindowTitle(_translate(" メインウィンドウ"" 测试数据" ))
        MainWindow.setWindowIcon(QIcon(" ./head.jpg " ))
        MainWindow.show()


    デフget_select(自己):
        プリント(self.tableWidget.columnCount())#返回列数
        #の印刷(self.tableWidget.rowCount())#返回行数

        colomn = self.tableWidget.columnCount()
        ROW_LIST = セット()
         のための I self.tableWidget.selectionModel()選択()のインデックス():
            row_list.add(i.row()) 
        プリント(ROW_LIST) 
        select_data = []
         のためのROW_LIST:
            row_data = [self.tableWidget.item(行、P)の.text()のための P における範囲(colomn)] 
            select_data.append(row_data)
        プリント(select_data)


    DEF のOpenFile(自己): 

        获取路径 
        openfile_name = QFileDialog.getOpenFileName(自己、' 选择文件''' ' Excelファイル(*。* .XLS、XLSX)'

        #1 プリント(openfile_name)
        グローバルpath_openfile_name 
        path_openfile_name = openfile_name [0] 


    DEF creat_table_show(自己):
        读取表格、转换表格
        場合でlen(path_openfile_name)> 0:
            input_table = pd.read_excel(path_openfile_name)
            印刷(1、input_table) 
            input_table_rows =  input_table.shape [0]
            input_table_colunms = input_table.shape [1 ]
             #のプリント(2、input_table_rows)
            #1 プリント(3、input_table_colunms) 
            input_table_header = input_table.columns.values.tolist()
            印刷(input_table_header)

            は、テーブル、リスト提供tablewidgetの最初の行への変換テーブルを読み出します

            self.tableWidget.setColumnCount(input_table_colunms)
            self.tableWidget.setRowCount(input_table_rows)
            self.tableWidget.setHorizo​​ntalHeaderLabels(input_table_header) 

            tablewidget提供されたリストの最初の行に

            #の横断素子を各テーブルに、tablewidgetに追加
            するための I レンジ(input_table_rows) :
                input_table_rows_values= input_table.iloc [I]
                 #1 プリント(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]
                 プリント(input_table_rows_values_list)
                のための J :範囲(input_table_colunms)
                    input_table_items_list = input_table_rows_values_list [J]
                    プリント(input_table_items_list)
                    #1 プリント(型(input_table_items_list)) 

                    将遍历的元素添加到tablewidget中并显示

                    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する追加要素ごとにトラバーステーブル、
        
            self.centralWidget.show()

IF  __name__ == " __main__ " インポートSYS 
    のApp = QtWidgets.QApplication(sys.argvの)
    メインウィンドウ = QtWidgets.QMainWindow()
    UI = Ui_MainWindow()
    ui.setupUi(メインウィンドウ)
    MainWindow.show()
    sys.exit(app.exec_())

 

おすすめ

転載: www.cnblogs.com/feifeifeisir/p/11592622.html
おすすめ