When I first used pyqt5, the following errors occurred during my operation:
pyqt5 ‘QWidget’ object has no attribute ‘setCentralWidget’
I used a method to solve this error:
In the running main function, it was originally like this
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_())
Then modify it as follows:
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_())
In addition to this method, there are several other methods on stack overflow that I don’t know if they are feasible:
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_())