请给我一份基于PyQt5的图片选择输出代码,附带界面
时间: 2024-05-27 18:10:50 浏览: 48
以下是基于PyQt5的图片选择输出代码及界面:
```python
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton, QFileDialog
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("图片选择输出")
self.setGeometry(200, 200, 400, 300)
self.label = QLabel("请选择图片", self)
self.label.move(150, 50)
self.button = QPushButton("选择图片", self)
self.button.move(150, 100)
self.button.clicked.connect(self.select_image)
self.show()
def select_image(self):
file_path, _ = QFileDialog.getOpenFileName(self, "选择图片", "", "Images (*.png *.xpm *.jpg)")
if file_path:
self.label.setText(f"已选择图片:{file_path}")
print(f"已选择图片:{file_path}")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())
```
界面截图:
![image](https://user-images.githubusercontent.com/55845812/122932725-0c0fc380-d3a6-11eb-9cd0-07355a2e3c3b.png)
阅读全文