如何仅接受数值作为QTableWidget的输入?禁用字母键

2024-05-05 23:30:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我想验证每个单元格中的Qtablewidget输入。如何只接受数值。例如,QLineEdit小部件。 我对PyQt5 GUI编程非常感兴趣。 我想创建一个只接受数值的应用程序,并想做进一步的计算

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QTableWidget, QApplication, QMainWindow, QTableWidgetItem, QFileDialog,qApp, QAction,QStyledItemDelegate,QLineEdit
from pandas import DataFrame
from numpy.linalg import inv
from PyQt5.QtCore import QRegExp
from PyQt5.QtGui import QRegExpValidator
from PyQt5.QtCore import pyqtSlot
import sqlite3 
import os
import re
import numpy as np 
import pandas as pd
class Ui_MainWindow(object):
   
    def submit(self):
     
        rowCount = self.tableWidgetInput.rowCount()
        columnCount = self.tableWidgetInput.columnCount()
        max_sum = 0
        global TMatrix,Ri,Ci,RiplusCi,RiMinusCi,inputArray_2D
        if (rowCount==columnCount):
            size=rowCount
            print("The size of the matrxi is %d * %d "%(size,size))
            print("The Given  matrxi is",  "SUM of Row" )
            rowData =[]
            
            for row in range(size):
                for column in range (size):
                        widegetItem = self.tableWidgetInput.item(row,column)

                        if(widegetItem and widegetItem.text):
                            rowData.append(float(widegetItem.text()) )
                        else:
                            rowData.append('NULL')
            print(rowData)
            inputArray = np.array(rowData,dtype=np.float64)  ###convert the list into numpy array.
            print(inputArray)
            size_rowdata = len(rowData)
            print("The total number of elemets are ",size_rowdata)
            inputArray_2D = np.reshape(inputArray, (rowCount, columnCount))   ### Reshape the numpy array into 2D
            print(inputArray_2D)
            sumofCol = np.sum(inputArray_2D,axis = 0,dtype='float')  ###find the sum of Column
            sumofRow = np.sum(inputArray_2D,axis = 1,dtype='float') ### find the sum of Row     
            maxInCol = np.amax(sumofCol)
            maxInRows = np.amax(sumofRow)
            print( "The Sum of Column is : ",sumofCol)
            print( "The Sum of Row is :",sumofRow)
            print( "The Maximum value in the  Column is :",maxInCol)
            print( "The Maximum value in the  Row is  : ",maxInRows)  
                   
        else:
            print("The input  is not a Square matrix")
            print("Data is not Submitted Sucessfully")

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1500, 1200)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.tableWidgetInput = QtWidgets.QTableWidget(self.centralwidget)
        self.tableWidgetInput.setGeometry(QtCore.QRect(30, 90, 1049, 520))
        self.tableWidgetInput.setObjectName("tableWidgetInput")
        self.tableWidgetInput.setColumnCount(2)
        self.tableWidgetInput.setRowCount(2)
        self.pushButton_submit = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_submit.setGeometry(QtCore.QRect(350, 625, 131, 51))
        self.pushButton_submit.setObjectName("pushButton_submit")                                        
        self.pushButton_submit.clicked.connect(self.submit)                                                    
        MainWindow.setCentralWidget(self.centralwidget) 
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton_submit.setText(_translate("MainWindow", "Submit"))
      
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_())

这里如何只接受数值


Tags: ofthefromimportselfsizeisnp
1条回答
网友
1楼 · 发布于 2024-05-05 23:30:10

一种可能的解决方案是将验证器设置为委托创建的编辑器:

class NumericDelegate(QStyledItemDelegate):
    def createEditor(self, parent, option, index):
        editor = super(NumericDelegate, self).createEditor(parent, option, index)
        if isinstance(editor, QLineEdit):
            reg_ex = QRegExp("[0-9]+.?[0-9]{,2}")
            validator = QRegExpValidator(reg_ex, editor)
            editor.setValidator(validator)
        return editor
delegate = NumericDelegate(self.tablewidget)
self.tablewidget.setItemDelegate(delegate)

相关问题 更多 >