pyqt5读取文件并绘图代码实现
时间: 2023-05-15 09:06:14 浏览: 163
以下是一个简单的 Pyqt5 读取文件并绘图的代码实现:
```python
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog, QGraphicsScene, QGraphicsView
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtCore import Qt
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.scene = QGraphicsScene(self)
self.view = QGraphicsView(self.scene)
self.setCentralWidget(self.view)
self.open_file()
def open_file(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
file_name, _ = QFileDialog.getOpenFileName(self, "Open File", "", "Images (*.png *.xpm *.jpg *.bmp);;All Files (*)", options=options)
if file_name:
image = QImage(file_name)
pixmap = QPixmap.fromImage(image)
self.scene.addPixmap(pixmap)
self.view.fitInView(self.scene.sceneRect(), Qt.KeepAspectRatio)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
```
这个程序会打开一个文件对话框,让用户选择一个图片文件。然后程序会读取这个文件,并在窗口中显示出来。用户可以使用鼠标滚轮来缩放图片。
阅读全文