我有一个用Qt设计师创建的表单。表单必须通过filedialog打开文件。这很好用。
但是我无法使用所选的文件路径更新标签。当我将filedialog的代码直接放在标签代码中时,标签设置正确。
部分代码如下:
import sys
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtWidgets import QWidget, QFileDialog
class Ui_Form(QWidget):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(499, 297)
self.toolButton = QtWidgets.QToolButton(Form)
self.toolButton.setGeometry(QtCore.QRect(20, 150, 25, 19))
self.toolButton.setObjectName("toolButton")
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(20, 180, 311, 16))
self.label.setObjectName("label")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.toolButton.setText(_translate("Form", "..."))
self.toolButton.clicked.connect(self.InputFile)
self.label.setText(_translate("Form", "TextLabel"))
def InputFile(self):
print("Input file printing")
fileName, _ = QFileDialog.getOpenFileName(self,"Select File", "","All Files (*);;Python Files (*.py)")
if fileName:
InputLabel = fileName
print(InputLabel)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())