Python 如何将从另一个模块导入的小部件添加到PySide2中的QMainWindow

Python 如何将从另一个模块导入的小部件添加到PySide2中的QMainWindow,python,pyside2,Python,Pyside2,我已经和PySide2一起工作了几天了。我制作了一个单独的模块,名为splash_page.py,用于存储QWidget类。但是当我导入并使用它时,我看不到小部件 splash_page.py from PySide2.QtWidgets import * from PySide2.QtGui import * class SplashPage(QWidget): def __init__(self): super().__init__() self

我已经和PySide2一起工作了几天了。我制作了一个单独的模块,名为
splash_page.py
,用于存储
QWidget
类。但是当我导入并使用它时,我看不到小部件

splash_page.py

from PySide2.QtWidgets import *
from PySide2.QtGui import *


class SplashPage(QWidget):
    def __init__(self):
        super().__init__()

        self.security_code_message = QLabel('Enter Security Code')
        self.ipt_box = QLineEdit()
        self.submit_button = QPushButton()

        vbox = QVBoxLayout()
        vbox.addWidget(self.security_code_message)
        vbox.addWidget(self.ipt_box)
        vbox.addWidget(self.submit_button)
        self.setLayout(vbox)
        self.initUI()
        #self.show()
        #self.showFullScreen()

    def initUI(self):
        self.security_code_message.move(301,170)
        self.ipt_box.move(301,315)
        self.submit_button.move(800,405)
main.py

from PySide2.QtWidgets import *
from PySide2.QtGui import *
import splash_page

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.cw = QWidget(self)
        self.setCentralWidget(self.cw)

        self.setGeometry(0,0,900,1000)
        sp = splash_page.SplashPage()
        vbox = QVBoxLayout()
        vbox.addWidget(sp)
        self.setLayout(vbox)



app = QApplication()
window = MainWindow()
window.show()
app.exec_()


问题很简单:您无法将布局设置为QMainWindow,因为它已经有了布局,正确的做法是在中央小部件中设置它,因此您必须将
self.setLayout(vbox)
更改为
self.cw.setLayout(vbox)