0
我想在pyqt5中使用python3創建一個路徑瀏覽器,但我有一些疑惑。我的代碼是這樣的:使用pyqt5庫QFileDialog的Python錯誤
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(300, 150, 160, 80))
self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.horizontalLayoutWidget)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.lineEdit = QtWidgets.QLineEdit(self.horizontalLayoutWidget)
self.lineEdit.setObjectName("lineEdit")
self.horizontalLayout.addWidget(self.lineEdit)
self.toolButton = QtWidgets.QToolButton(self.horizontalLayoutWidget)
self.toolButton.setObjectName("toolButton")
self.horizontalLayout.addWidget(self.toolButton)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
# Button action
self.toolButton.pressed.connect(self.selectDirectory(MainWindow, self.lineEdit))
def selectDirectory(self, MainWindow, editText):
editText.setText(str(QtWidgets.QFileDialog.getExistingDirectory(MainWindow, "Select Directory", str(editText.text()))))
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "Path"))
self.toolButton.setText(_translate("MainWindow", "..."))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
的想法是使用的EditText寫的文件夾的路徑,但作爲幫助我要介紹一個按鈕,以選擇目錄打開文件瀏覽器。爲了實現這一點,我試圖使用QFileDialog沒有成功。我現在的問題是當我運行我的應用程序時顯示QFileDialog,當我選擇一個文件夾時,應用程序崩潰。的第一件事,我不想沒有按顯示QFileDialog按鈕,第二件事我不明白爲什麼應用程序與此錯誤崩潰:
Traceback (most recent call last):
File "test.py", line 48, in <module>
ui.setupUi(MainWindow)
File "test.py", line 31, in setupUi
self.toolButton.pressed.connect(self.selectDirectory(MainWindow, self.lineEdit))
TypeError: argument 1 has unexpected type 'NoneType'
感謝您的幫助
謝謝!這是工作 – Dhorka