pyqt5 'QWidget' object has no attribute 'setCentralWidget'
版权声明:本博客内容为原创,若要转载,请注明出处!否则禁止转载! https://blog.csdn.net/wardenjohn/article/details/87628891
在我刚使用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_())
2:
-
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()
-