所以我正在尝试使用 PyQt5 制作一个应用程序。试图给按钮一些功能,抛出错误
So I'm trying to make an app using PyQt5. Tried to give some functionality to a button, throws an error
def openFileNameDialog(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
fileName, _ = QFileDialog.getOpenFileName(self,"QFileDialog.getOpenFileName()", "","All Files (*);;Python Files (*.py)", options=options)
if fileName:
print(fileName)
对于这段代码,这是我的输出,
fileName, _ = QFileDialog.getOpenFileName(self,"QFileDialog.getOpenFileName()", "","All Files ();;Python Files (.py)", options=options)
TypeError: getOpenFileName(parent: QWidget = None, caption: str = '', directory: str = '', filter: str = '', initialFilter: str = '', options: Union[QFileDialog.Options, QFileDialog.Option] = 0): 参数 1 具有意外类型 'Ui_MainWindow'
我该如何解决这个问题?
(Post 编辑) :
这是我的 GUI 代码:
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(443, 427)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(350, 100, 75, 23))
self.pushButton.setObjectName("pushButton")
**self.pushButton.clicked.connect(self.openFileNameDialog)**
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(40, 100, 281, 21))
self.lineEdit.setObjectName("lineEdit")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 443, 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.pushButton.setText(_translate("MainWindow", "Browse"))
当我 运行 python 脚本时抛出错误。
感谢您提供 GUI 代码。我相信这个错误很可能是由继承并发症引起的。我不确定为什么要将单独的 MainWindow 对象传递给各种函数。相反,尝试从 QMainWindow() 继承并将所有 UI 元素分配给 self.
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setObjectName("MainWindow")
self.resize(443, 427)
self.centralwidget = QWidget(self)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QPushButton(self.centralwidget)
self.pushButton.setGeometry(QRect(350, 100, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.openFileNameDialog)
self.lineEdit = QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QRect(40, 100, 281, 21))
self.lineEdit.setObjectName("lineEdit")
self.setCentralWidget(self.centralwidget)
self.menubar = QMenuBar(self)
self.menubar.setGeometry(QRect(0, 0, 443, 21))
self.menubar.setObjectName("menubar")
self.setMenuBar(self.menubar)
self.statusbar = QStatusBar(self)
self.statusbar.setObjectName("statusbar")
self.setStatusBar(self.statusbar)
self.retranslateUi()
QMetaObject.connectSlotsByName(self)
def retranslateUi(self):
_translate = QCoreApplication.translate
self.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Browse"))
def openFileNameDialog(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
fileName, _ = QFileDialog.getOpenFileName(self,"QFileDialog.getOpenFileName()", "","All Files (*);;Python Files (*.py)", options=options)
if fileName:
print(fileName)
if __name__ == '__main__':
app = QApplication(sys.argv)
MW = MainWindow()
MW.show()
sys.exit(app.exec_())
def openFileNameDialog(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
fileName, _ = QFileDialog.getOpenFileName(self,"QFileDialog.getOpenFileName()", "","All Files (*);;Python Files (*.py)", options=options)
if fileName:
print(fileName)
对于这段代码,这是我的输出,
fileName, _ = QFileDialog.getOpenFileName(self,"QFileDialog.getOpenFileName()", "","All Files ();;Python Files (.py)", options=options) TypeError: getOpenFileName(parent: QWidget = None, caption: str = '', directory: str = '', filter: str = '', initialFilter: str = '', options: Union[QFileDialog.Options, QFileDialog.Option] = 0): 参数 1 具有意外类型 'Ui_MainWindow'
我该如何解决这个问题?
(Post 编辑) : 这是我的 GUI 代码:
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(443, 427)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(350, 100, 75, 23))
self.pushButton.setObjectName("pushButton")
**self.pushButton.clicked.connect(self.openFileNameDialog)**
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(40, 100, 281, 21))
self.lineEdit.setObjectName("lineEdit")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 443, 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.pushButton.setText(_translate("MainWindow", "Browse"))
当我 运行 python 脚本时抛出错误。
感谢您提供 GUI 代码。我相信这个错误很可能是由继承并发症引起的。我不确定为什么要将单独的 MainWindow 对象传递给各种函数。相反,尝试从 QMainWindow() 继承并将所有 UI 元素分配给 self.
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setObjectName("MainWindow")
self.resize(443, 427)
self.centralwidget = QWidget(self)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QPushButton(self.centralwidget)
self.pushButton.setGeometry(QRect(350, 100, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.openFileNameDialog)
self.lineEdit = QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QRect(40, 100, 281, 21))
self.lineEdit.setObjectName("lineEdit")
self.setCentralWidget(self.centralwidget)
self.menubar = QMenuBar(self)
self.menubar.setGeometry(QRect(0, 0, 443, 21))
self.menubar.setObjectName("menubar")
self.setMenuBar(self.menubar)
self.statusbar = QStatusBar(self)
self.statusbar.setObjectName("statusbar")
self.setStatusBar(self.statusbar)
self.retranslateUi()
QMetaObject.connectSlotsByName(self)
def retranslateUi(self):
_translate = QCoreApplication.translate
self.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Browse"))
def openFileNameDialog(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
fileName, _ = QFileDialog.getOpenFileName(self,"QFileDialog.getOpenFileName()", "","All Files (*);;Python Files (*.py)", options=options)
if fileName:
print(fileName)
if __name__ == '__main__':
app = QApplication(sys.argv)
MW = MainWindow()
MW.show()
sys.exit(app.exec_())