class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1093, 670)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(120, 320, 93, 28))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
...
self.retranslateUi(MainWindow)
self.pushButton.clicked.connect(self.pushButtonClicked)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
def pushButtonClicked(self):
print("hi")
dialog = QFileDialog
dialog.show()
##dialog.getOpenFileName(None,tr("Open Image"), "/home", tr("Image Files (*.png *.jpg *.bmp)"))
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
我从PYQT5 Designer制作此代码,我想打开QFileDialog来选择文件。 但是当我点击pushButtonClicked的connet按钮时,程序停止并崩溃。 请帮帮我
答案 0 :(得分:0)
你也可以这样做:
def pushButtonClicked(self):
self.op_dir = QFileDialog.getOpenFileName(None,tr("Open Image"), "/home", tr("Image Files (*.png *.jpg *.bmp)"))
变量self.op_dir
将包含您选择的路径
答案 1 :(得分:0)
path_to_project = QFileDialog.getExistingDirectory(None, tr("Open Directory"), "C:/", QFileDialog.ShowDirsOnly|QFileDialog.DontResolveSymlinks)
也没有为我工作。调用时崩溃。 但是,删除转换函数tr()并只放入字符串即可解决问题:
path_to_project = QFileDialog.getExistingDirectory(None, "Open Directory", "C:/", QFileDialog.ShowDirsOnly|QFileDialog.DontResolveSymlinks)