from PyQt5 import QtCore, QtGui, QtWidgets class MyQLine(QtWidgets.QLineEdit): """实现文件拖放功能""" def __init__(self, parent=None): super().__init__(parent) self.setAcceptDrops(True) def dragEnterEvent(self, e): if e.mimeData().text().endswith('.xlsx'): # 这里只接受拖入srt文件,别的文件拖入无效 e.accept() else: e.ignore() def dropEvent(self, e): path = e.mimeData().text().replace('file:///', '') self.setText(path)