[英]How to accept only numeric values as input for the QTableWidget ? Disable the alphabet keys
我想验证每个单元格中的 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_())
如何在这里只接受数值?
一种可能的解决方案是将验证器设置为委托创建的编辑器:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.