使用PyQt5写窗体时,链接内置打开文件槽函数出现TypeError: getOpenFileName(parent: QWidget = None, caption: str = ‘’, directory: str = ‘’, filter: str = ‘’, initialFilter: str = ‘’, options: Union[QFileDialog.Options, QFileDialog.Option] = 0): argument 1 has unexpected type 'Ui_MainWindow’错误的解决办法
一. 问题由来
近期在使用Python的开发窗体,采用Pyqt5这一强大的库进行开发,但是在编写代码后,将菜单(打开文件)与函数连接时,运行用窗体打开文件选择器时出现了如下问题:
TypeError: getOpenFileName(parent: QWidget = None, caption: str = ‘’, directory: str = ‘’, filter: str = ‘’, initialFilter: str = ‘’, options: Union[QFileDialog.Options, QFileDialog.Option] = 0): argument 1 has unexpected type
二. 部分源代码
(1)openMsg
def openMsg(self):
file,ok = QtWidgets.QFileDialog.getOpenFileName(self,"打开","E:/","All (*);;Text Files (*.txt)")
self.statusbar.showMessage(file)
(二) 连接槽函数
self.menuItem_openetc.triggered.connect(self.openMsg)
注意:槽函数内self.openMsg不带括号,即不是self.openMsg()
三. 分析
根据返回的错误信息,是继承的类出了问题(貌似?有大佬可以指出吗,目前刚刚学习,一知半解,只知道解决办法,或是待些时日,本人再回来解答!)
四. 解决办法
将file,ok = QtWidgets.QFileDialog.getOpenFileName(self,"打开","E:/","All (*);;Text Files (*.txt)")
的 self改成 None
此时运行结果: