简体   繁体   中英

PyQt5 Custom Widget Opens in Another Window

I started out with PyQt5 recently. I wanted to create a custom widget and then insert it into the main window of an application.

The custom Widget:

class ScoreCard(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(ScoreCard, self).__init__(parent=parent)
        self.setWindowFlags(QtCore.Qt.CustomizeWindowHint)
        self.pressing = False
        self.init_ui()
        self.show()

    def init_ui(self):
        # Layout in here

And this is the main Application:

from PyQt5.QtWidgets import *
from scorecard import ScoreCard
import sys


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

    def initUi(self):
        self.setGeometry(300,300,800,700)
        window_layout = QVBoxLayout()
        recent_playcard = ScoreCard()
        window_layout.addWidget(recent_playcard)
        self.setLayout(window_layout)

if __name__ == "__main__":

    app = QApplication(sys.argv)
    execute = MainWindow()
    sys.exit(app.exec_())

Why is it that whenever I run the main Application, the custom widget appears in another window? I even tried removing the frame and setting the parent to none, but none of that changed this behavior. How do I fix this?

Looks like I mixed up QMainWindow and QDialog I should be using a central widget for the main application instead of setting a layout..

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