【发布时间】:2021-01-10 10:59:32
【问题描述】:
我已经使用 PyQt5 QTableWidget、QLineEdit、QComboBox 创建了学生数据输入对话框。
这是 PyQt5 代码
import sys
from PyQt5.QtWidgets import (QDialog,
QApplication,
QGridLayout,
QPushButton,
QLabel,
QLineEdit,
QComboBox,
QTableWidget,
QTableWidgetItem,
QAbstractItemView)
from PyQt5.QtCore import Qt
class MyWindow(QDialog):
def __init__(self):
super().__init__()
self.setWindowTitle('Student Data Entry')
grdLayout = QGridLayout()
self.lblStudentId = QLabel('Student Id')
self.cboStudentId = QComboBox(self)
self.cboStudentId.addItems(['1000','6001','5000','5002','9000','1004'])
self.cboStudentId.setCurrentIndex(1)
self.lblStudentName = QLabel('Student name',self)
self.ledStudentName = QLineEdit(self)
self.lblAge = QLabel('Age',self)
self.ledAge = QLineEdit(self)
self.ledAge.setAlignment(Qt.AlignCenter)
self.ledAge.setPlaceholderText('0')
self.lblAdd = QLabel('Add',self)
self.btnAdd = QPushButton('Add',self)
self.btnAdd.clicked.connect(self.addFeeRow)
# student data table
self.tblStudent = QTableWidget(self)
self.tblStudent.setColumnCount(3)
self.tblStudent.setShowGrid(True)
self.tblStudent.setHorizontalHeaderLabels(('Student Id', 'Student name','Age'))
self.tblStudent.verticalHeader().hide()
self.tblStudent.setEditTriggers(QTableWidget.NoEditTriggers)
self.tblStudent.setSelectionBehavior(QAbstractItemView.SelectRows)
self.tblStudent.setColumnWidth(0, 50)
self.tblStudent.setColumnWidth(1, 150)
self.tblStudent.setColumnWidth(2, 25)
# Add Label, LineEdit and ComboBox
grdLayout.addWidget(self.lblStudentId,0,0)
grdLayout.addWidget(self.lblStudentName,0,1,1,2)
grdLayout.addWidget(self.lblAge,0,3)
grdLayout.addWidget(self.lblAdd,0,4)
grdLayout.addWidget(self.cboStudentId,1,0)
grdLayout.addWidget(self.ledStudentName,1,1,1,2)
grdLayout.addWidget(self.ledAge,1,3)
grdLayout.addWidget(self.btnAdd,1,4)
# Add table
grdLayout.addWidget(self.tblStudent,2,0,1,4)
grdLayout.setColumnStretch(0,1)
grdLayout.setColumnStretch(1,1)
grdLayout.setColumnStretch(2,1)
self.setLayout(grdLayout)
def addFeeRow(self):
rowCount = self.tblStudent.rowCount()
self.tblStudent.insertRow(rowCount)
# insert into table
self.tblStudent.setItem(rowCount,0, QTableWidgetItem(self.cboStudentId.currentText()))
self.tblStudent.setItem(rowCount,1, QTableWidgetItem(self.ledStudentName.text()))
self.tblStudent.setItem(rowCount,2, QTableWidgetItem(self.ledAge.text()))
if __name__ == '__main__':
app = QApplication()
window = MyWindow()
window.show()
我的要求是,
-
当用户点击Add QPushButton 或Age QLineEdit 时按Enter 键时,应将数据(学生ID、学生姓名和学生年龄)输入到表中。当用户在 Age QLineEdit 或 Add QPushButton 中按 Enter 键时应将数据输入到表格中。
-
此外,当用户在 Student Id QComboBox 中按 Enter 键 时,焦点应移至 下一个小部件(学生姓名)。当用户在 Student Name QLineEdit 中按下 Enter 键 时,焦点应移至 下一个小部件(学生年龄)
在当前场景中,当用户在学生 ID、学生姓名等任何小部件中按 Enter 键时,数据正在输入到表中,这是我不想要的。 请提供限制这种行为的解决方案。
【问题讨论】:
标签: python python-3.x pyqt pyqt5 pyside2