繁体   English   中英

如何在 PySide2 中添加从另一个模块导入的 Widgets 到 QMainWindow

[英]How to add Widgets imported from another module to QMainWindow in PySide2

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

飞溅页面.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)

主文件

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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