main.py
import sys
import PyQt5.QtWidgets as qw
from mainwindow import Ui_MainWindow
class TestWindow(qw.QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.setAcceptDrops(True)
def dragEnterEvent(self, evn):
print('鼠标拖入窗口')
# self.QLabl.setText('文件路径:\n' + evn.mimeData().text())
# 鼠标放开函数事件
evn.accept()
def dropEvent(self, evn):
print(f'鼠标放开 {evn.posF()}')
path = evn.mimeData().text()
if self.ui.lineEdit_1.hasFocus():
self.ui.lineEdit_1.setText(path)
elif self.ui.lineEdit_2.hasFocus():
self.ui.lineEdit_2.setText(path)
elif self.ui.lineEdit_3.hasFocus():
self.ui.lineEdit_3.setText(path)
else:
print('文件路径:\n' + path)
def dragMoveEvent(self, evn):
pass
# print('鼠标移动')
if __name__ == '__main__':
app = qw.QApplication(sys.argv)
w = TestWindow()
w.show()
sys.exit(app.exec_())
mainwindow.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'mainwindow.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(612, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.lineEdit_1 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_1.setObjectName("lineEdit_1")
self.horizontalLayout.addWidget(self.lineEdit_1)
self.verticalLayout.addLayout(self.horizontalLayout)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setObjectName("label_2")
self.horizontalLayout_2.addWidget(self.label_2)
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setObjectName("lineEdit_2")
self.horizontalLayout_2.addWidget(self.lineEdit_2)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setObjectName("label_3")
self.horizontalLayout_3.addWidget(self.label_3)
self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_3.setObjectName("lineEdit_3")
self.horizontalLayout_3.addWidget(self.lineEdit_3)
self.verticalLayout.addLayout(self.horizontalLayout_3)
spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 612, 23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "测试输入框1"))
self.label_2.setText(_translate("MainWindow", "测试输入框2"))
self.label_3.setText(_translate("MainWindow", "测试输入框3"))