QFileDialog
在PyQt中不起作用。
主要.py:
from PyQt5 import QtCore, QtGui, QtWidgets, QtMultimedia
import sys
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(634, 312)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.splitter = QtWidgets.QSplitter(self.centralwidget)
self.splitter.setOrientation(QtCore.Qt.Horizontal)
self.splitter.setObjectName("splitter")
self.layoutWidget = QtWidgets.QWidget(self.splitter)
self.layoutWidget.setObjectName("layoutWidget")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.layoutWidget)
self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.layoutWidget)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.lineEdit = QtWidgets.QLineEdit(self.layoutWidget)
self.lineEdit.setObjectName("lineEdit")
self.horizontalLayout.addWidget(self.lineEdit)
self.pushButton = QtWidgets.QPushButton(self.layoutWidget)
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.openFile1) # работает(привязыаем к действию)
self.pushButton.clicked.connect(self.openFile) # работает(привязыаем к действию)
self.horizontalLayout.addWidget(self.pushButton)
self.verticalLayout_2.addLayout(self.horizontalLayout)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_2 = QtWidgets.QLabel(self.layoutWidget)
self.label_2.setObjectName("label_2")
self.horizontalLayout_2.addWidget(self.label_2)
self.lineEdit_2 = QtWidgets.QLineEdit(self.layoutWidget)
self.lineEdit_2.setObjectName("lineEdit_2")
self.horizontalLayout_2.addWidget(self.lineEdit_2)
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_2.addItem(spacerItem)
self.verticalLayout_2.addLayout(self.horizontalLayout_2)
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_3.addItem(spacerItem1)
self.pushButton_2 = QtWidgets.QPushButton(self.layoutWidget)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout_3.addWidget(self.pushButton_2)
spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_3.addItem(spacerItem2)
self.verticalLayout_2.addLayout(self.horizontalLayout_3)
spacerItem3 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout_2.addItem(spacerItem3)
self.layoutWidget1 = QtWidgets.QWidget(self.splitter)
self.layoutWidget1.setObjectName("layoutWidget1")
self.verticalLayout = QtWidgets.QVBoxLayout(self.layoutWidget1)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.checkBox_3 = QtWidgets.QCheckBox(self.layoutWidget1)
self.checkBox_3.setObjectName("checkBox_3")
self.horizontalLayout_4.addWidget(self.checkBox_3)
self.label_3 = QtWidgets.QLabel(self.layoutWidget1)
self.label_3.setObjectName("label_3")
self.horizontalLayout_4.addWidget(self.label_3)
spacerItem4 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_4.addItem(spacerItem4)
self.verticalLayout.addLayout(self.horizontalLayout_4)
self.horizontalLayout_5 = QtWidgets.QHBoxLayout()
self.horizontalLayout_5.setObjectName("horizontalLayout_5")
self.checkBox = QtWidgets.QCheckBox(self.layoutWidget1)
self.checkBox.setObjectName("checkBox")
self.horizontalLayout_5.addWidget(self.checkBox)
spacerItem5 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_5.addItem(spacerItem5)
spacerItem6 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_5.addItem(spacerItem6)
self.verticalLayout.addLayout(self.horizontalLayout_5)
self.textEdit_2 = QtWidgets.QTextEdit(self.layoutWidget1)
self.textEdit_2.setEnabled(False)
self.textEdit_2.setObjectName("textEdit_2")
self.verticalLayout.addWidget(self.textEdit_2)
self.horizontalLayout_6 = QtWidgets.QHBoxLayout()
self.horizontalLayout_6.setObjectName("horizontalLayout_6")
self.checkBox_2 = QtWidgets.QCheckBox(self.layoutWidget1)
self.checkBox_2.setObjectName("checkBox_2")
self.horizontalLayout_6.addWidget(self.checkBox_2)
spacerItem7 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_6.addItem(spacerItem7)
spacerItem8 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_6.addItem(spacerItem8)
self.verticalLayout.addLayout(self.horizontalLayout_6)
self.textEdit = QtWidgets.QTextEdit(self.layoutWidget1)
self.textEdit.setEnabled(False)
self.textEdit.setObjectName("textEdit")
self.verticalLayout.addWidget(self.textEdit)
self.gridLayout.addWidget(self.splitter, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 634, 21))
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", "ввдите имя файла"))
self.pushButton.setText(_translate("MainWindow", "Ввод"))
self.label_2.setText(_translate("MainWindow", "Кол-во частей"))
self.pushButton_2.setText(_translate("MainWindow", "Расчёт"))
self.checkBox_3.setText(_translate("MainWindow", "CheckBox"))
self.label_3.setText(_translate("MainWindow", "Ввод дополнительных строк "))
self.checkBox.setText(_translate("MainWindow", "Начало"))
self.checkBox_2.setText(_translate("MainWindow", "Конец"))
# Для открытия файла используем метод getOpenFileUrl() класса
# QFileDialog, т. к. для создания экземпляра класса
# QMediaContent нам нужен путь к файлу, заданный в виде
# экземпляра класса QUrl
def openFile(self):
file = QtWidgets.QFileDialog.getOpenFileUrl(parent=self,
caption="Выберите звуковой файл",
filter="Звуковые файлы (*.mp3 *.ac3)")
def openFile1(self):
print("test")
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
当我按下“Enter”按钮时,出现错误:
runfile('C:/Users/User/Desktop/Kononov/piton/gui.py', wdir='C:/Users/User/Desktop/Kononov/piton') 测试回溯(最近一次调用):
openFile 文件中的文件 c:\users\user\desktop\kononov\piton\gui.py:142 = QtWidgets.QFileDialog.getOpenFileUrl(parent=self,
TypeError:getOpenFileUrl(父:可选[QWidget] = None,标题:可选[str] = '',目录:QUrl = QUrl(),过滤器:可选[str] = '',initialFilter:可选[str] = '' ,选项:Union[QFileDialog.Options,QFileDialog.Option] = 0,supportedSchemes:Iterable[Optional[str]] = []):参数“parent”具有意外类型“Ui_MainWindow”
我无法弄清楚我做错了什么?
感觉好像我错过了一些重要的点,因为我在Qt Designer中创建了视图,然后在pyuic5.exe中运行它