简体   繁体   中英

How to add Widgets imported from another module to QMainWindow in PySide2

I have been working with PySide2 for a couple of days now. I have made a separate module called splash_page.py for storing a QWidget class. But when I import it and use it, I can not see the widgets.

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) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM