pyqt5中,用户不可选择路径,按一个按钮,把一个label上的Pixmap存储到当前目录下的一个文件夹中
时间: 2023-07-16 07:14:08 浏览: 124
你可以使用QFileDialog来让用户选择存储路径,然后使用QPixmap.save()方法将label上的Pixmap保存到该路径下的文件夹中。以下是示例代码:
```python
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QFileDialog
from PyQt5.QtGui import QPixmap
import os
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 350, 300)
self.setWindowTitle('Save Pixmap')
self.label = QLabel(self)
self.label.setGeometry(50, 50, 200, 200)
self.label.setPixmap(QPixmap("image.png"))
btn = QPushButton('Save Pixmap', self)
btn.move(50, 260)
btn.clicked.connect(self.savePixmap)
self.show()
def savePixmap(self):
filename, _ = QFileDialog.getSaveFileName(self, 'Save Pixmap', os.path.expanduser('~'))
if filename:
self.label.pixmap().save(filename)
if __name__ == '__main__':
app = QApplication([])
ex = Example()
app.exec_()
```
在这个例子中,我们创建了一个窗口,其中包含一个label和一个按钮。当用户点击按钮时,将弹出QFileDialog,让用户选择保存路径。如果用户选择了路径并点击了保存,那么该路径下就会创建一个文件,并将label上的Pixmap保存到该文件中。
阅读全文
相关推荐














