代码
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class Window(QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.paths = ""
self.setWindowTitle('文件拖入')
self.resize(500, 400)
ico_file = "../qt/ico/Ferrari.ico"
self.setWindowIcon(QIcon(ico_file))
self.textBrowser = QTextBrowser()
self.setCentralWidget(self.textBrowser)
self.setAcceptDrops(True)
self.btn = QPushButton("清空", self)
self.btn.setGeometry(200, 300, 100, 30)
self.btn.clicked.connect(self.when_btn_click)
def when_btn_click(self):
self.textBrowser.setText("")
def dragEnterEvent(self, event):
file = event.mimeData().urls()[0].toLocalFile()
if file not in self.paths:
self.paths += file + "\n"
print("拖拽的文件 ==> {}".format(file))
self.textBrowser.setText(self.paths)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
展示
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/bb480a051c581b0a1bce6e1cccdd41bf.jpeg#pic_center)