请使用pyqt
时间: 2023-07-23 09:18:40 浏览: 68
好的,以下是使用PyQt实现在点击按钮时在一个QFrame中显示表格的代码:
```python
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableView, QFrame, QPushButton, QVBoxLayout
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QStandardItemModel, QStandardItem
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# 创建一个QFrame
self.frame = QFrame(self)
self.setCentralWidget(self.frame)
# 创建一个QPushButton,点击时触发show_table函数
self.button = QPushButton("显示表格", self.frame)
self.button.clicked.connect(self.show_table)
# 垂直布局
self.layout = QVBoxLayout()
self.layout.addWidget(self.button)
self.frame.setLayout(self.layout)
def show_table(self):
# 创建一个QStandardItemModel
model = QStandardItemModel()
# 设置表头
model.setHorizontalHeaderLabels(["Name", "Age"])
# 添加数据
model.appendRow([QStandardItem("Alice"), QStandardItem("25")])
model.appendRow([QStandardItem("Bob"), QStandardItem("30")])
model.appendRow([QStandardItem("Charlie"), QStandardItem("35")])
# 创建一个QTableView,并将QStandardItemModel设置为其model
table = QTableView(self.frame)
table.setModel(model)
table.horizontalHeader().setSectionResizeMode(QTableView.Stretch)
# 将QTableView添加到垂直布局中
self.layout.addWidget(table)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
```
当点击按钮时,会在QFrame中显示一个表格,包含姓名和年龄两列,以及三行数据。请注意,这只是一个简单的例子,实际使用中可能需要根据具体需求进行修改和调整。
阅读全文
相关推荐
















