在我刚使用PyQt5的时候,我运行的时候出现以下报错:
pyqt5 'QWidget' object has no attribute 'setCentralWidget'
这种错误我用了一种方法来解决:
在运行的主函数里面,原本是这样的
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
widgets = QtWidgets.QWidget()
ui = MainWin()
ui.main_ui.setupUi(widgets)
widgets.show()
ui.run_function()
sys.exit(app.exec_())
然后修改成这样:
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
widgets = QtWidgets.QMainWindow()
ui = MainWin()
ui.main_ui.setupUi(widgets)
widgets.show()
ui.run_function()
sys.exit(app.exec_())
除了这种方法,Stack Overflow上还有几种不知道可不可行:
1:
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent=parent)
ui = Ui_MainWindow()
ui.setupUi(self)
import sys
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent=parent)
self.setupUi(self)
import sys
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())