In this tutorial, we are going to learn how to implement image drag and drop feature in PyQt5. The image drga and drop feature is when you drag and image from your PC to a PyQt application, the application will detect the incoming signal and inserting the image.
Buy Me a Coffee? Your support is much appreciated!
PayPal Me: https://www.paypal.me/jiejenn/5
Venmo: @Jie-Jenn
Source Code:
import sys, os
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap
class ImageLabel(QLabel):
def __init__(self):
super().__init__()
self.setAlignment(Qt.AlignCenter)
self.setText('\n\n Drop Image Here \n\n')
self.setStyleSheet('''
QLabel{
border: 4px dashed #aaa
}
''')
def setPixmap(self, image):
super().setPixmap(image)
class AppDemo(QWidget):
def __init__(self):
super().__init__()
self.resize(400, 400)
self.setAcceptDrops(True)
mainLayout = QVBoxLayout()
self.photoViewer = ImageLabel()
mainLayout.addWidget(self.photoViewer)
self.setLayout(mainLayout)
def dragEnterEvent(self, event):
if event.mimeData().hasImage:
event.accept()
else:
event.ignore()
def dragMoveEvent(self, event):
if event.mimeData().hasImage:
event.accept()
else:
event.ignore()
def dropEvent(self, event):
if event.mimeData().hasImage:
event.setDropAction(Qt.CopyAction)
file_path = event.mimeData().urls()[0].toLocalFile()
self.set_image(file_path)
event.accept()
else:
event.ignore()
def set_image(self, file_path):
self.photoViewer.setPixmap(QPixmap(file_path))
app = QApplication(sys.argv)
demo = AppDemo()
demo.show()
sys.exit(app.exec_())
can you tell me how we can store that uploaded picture in some particular location….please?
can we upload our files to Amazon s3 bucket?
Almost certainly sure you can.