-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyFileDrop.py
More file actions
48 lines (40 loc) · 1.39 KB
/
PyFileDrop.py
File metadata and controls
48 lines (40 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import sys
from PyQt5.QtWidgets import QApplication, QLabel, QVBoxLayout, QWidget
from PyQt5.QtCore import Qt, QUrl
from PyQt5.QtGui import QPixmap
class DropLabel(QLabel):
def __init__(self, parent=None):
super().__init__(parent)
self.setAcceptDrops(True)
self.setText("Drag and drop a file here")
self.setAlignment(Qt.AlignCenter)
self.setStyleSheet("QLabel { border: 2px dashed #aaa; }")
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.acceptProposedAction()
else:
event.ignore()
def dropEvent(self, event):
if event.mimeData().hasUrls():
event.setDropAction(Qt.CopyAction)
event.accept()
for url in event.mimeData().urls():
file_path = url.toLocalFile()
self.setPixmap(QPixmap(file_path)) # Assuming the file is an image
self.setText(file_path)
else:
event.ignore()
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("File Drop Example")
self.resize(400, 300)
layout = QVBoxLayout()
self.label = DropLabel(self)
layout.addWidget(self.label)
self.setLayout(layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())