pyqt表格实现增删改查
时间: 2023-08-31 10:09:40 浏览: 116
增删改查的实现
在 PyQt 中,可以使用 QTableWidget 或者 QTableView 控件来实现表格的增删改查功能。下面给出一个简单的示例代码:
```python
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableWidget, QTableWidgetItem, QVBoxLayout, QWidget, QPushButton, QHBoxLayout, QLineEdit, QLabel
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Table Example")
self.table_widget = QTableWidget()
self.table_widget.setRowCount(5)
self.table_widget.setColumnCount(3)
self.table_widget.setItem(0, 0, QTableWidgetItem("John"))
self.table_widget.setItem(0, 1, QTableWidgetItem("Doe"))
self.table_widget.setItem(0, 2, QTableWidgetItem("john.doe@gmail.com"))
self.table_widget.setItem(1, 0, QTableWidgetItem("Jane"))
self.table_widget.setItem(1, 1, QTableWidgetItem("Doe"))
self.table_widget.setItem(1, 2, QTableWidgetItem("jane.doe@gmail.com"))
self.table_widget.setHorizontalHeaderLabels(["First Name", "Last Name", "Email"])
# Add buttons
add_button = QPushButton("Add")
add_button.clicked.connect(self.add_row)
delete_button = QPushButton("Delete")
delete_button.clicked.connect(self.delete_row)
update_button = QPushButton("Update")
update_button.clicked.connect(self.update_row)
# Add input fields
self.first_name_input = QLineEdit()
self.last_name_input = QLineEdit()
self.email_input = QLineEdit()
add_layout = QHBoxLayout()
add_layout.addWidget(QLabel("First Name"))
add_layout.addWidget(self.first_name_input)
add_layout.addWidget(QLabel("Last Name"))
add_layout.addWidget(self.last_name_input)
add_layout.addWidget(QLabel("Email"))
add_layout.addWidget(self.email_input)
add_layout.addWidget(add_button)
# Add layout to widget
widget = QWidget()
main_layout = QVBoxLayout()
main_layout.addWidget(self.table_widget)
main_layout.addLayout(add_layout)
main_layout.addWidget(delete_button)
main_layout.addWidget(update_button)
widget.setLayout(main_layout)
self.setCentralWidget(widget)
def add_row(self):
row_count = self.table_widget.rowCount()
self.table_widget.setRowCount(row_count + 1)
first_name = self.first_name_input.text()
last_name = self.last_name_input.text()
email = self.email_input.text()
self.table_widget.setItem(row_count, 0, QTableWidgetItem(first_name))
self.table_widget.setItem(row_count, 1, QTableWidgetItem(last_name))
self.table_widget.setItem(row_count, 2, QTableWidgetItem(email))
self.first_name_input.clear()
self.last_name_input.clear()
self.email_input.clear()
def delete_row(self):
selected_row = self.table_widget.currentRow()
if selected_row != -1:
self.table_widget.removeRow(selected_row)
def update_row(self):
selected_row = self.table_widget.currentRow()
if selected_row != -1:
first_name = self.first_name_input.text()
last_name = self.last_name_input.text()
email = self.email_input.text()
self.table_widget.setItem(selected_row, 0, QTableWidgetItem(first_name))
self.table_widget.setItem(selected_row, 1, QTableWidgetItem(last_name))
self.table_widget.setItem(selected_row, 2, QTableWidgetItem(email))
self.first_name_input.clear()
self.last_name_input.clear()
self.email_input.clear()
if __name__ == "__main__":
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
```
该示例代码中实现了一个简单的表格,可以添加、删除和更新表格中的行。用户可以通过输入框输入数据,并点击相应的按钮来实现增删改查操作。在该示例中,使用了 QTableWidget 控件来展示表格数据,并添加了 QLineEdit、QLabel 和 QPushButton 等控件来实现用户交互。
阅读全文