简体   繁体   中英

QMainWindow disappears when called from another QMainWindow

This is second window which i want to import in first window but it will flash and disappear.

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow_6(object):

def setupUi_6(self, MainWindow1):
    MainWindow1.setObjectName("MainWindow")
    MainWindow1.setFixedSize(462, 488)
    icon = QtGui.QIcon()
    icon.addPixmap(QtGui.QPixmap(":/newPrefix/snooker1.png"), 
    QtGui.QIcon.Normal, QtGui.QIcon.Off)
    MainWindow1.setWindowIcon(icon)
    MainWindow1.setStyleSheet("QMainWindow{\n"
                             "background-image: url(:/newPrefix/snk.jpg);\n"
                             "}")
    self.centralwidget = QtWidgets.QWidget(MainWindow1)

if name == " main ": import sys

app = QtWidgets.QApplication(sys.argv)
MainWindow1 = QtWidgets.QMainWindow()
ui = Ui_MainWindow_6()
ui.setupUi_6(MainWindow1)
MainWindow1.show()
sys.exit(app.exec_())

This is my first window:

class Ui_MainWindow(object):

def setupUi(self, MainWindow):
    MainWindow.setObjectName("MainWindow")
    MainWindow.resize(1419, 768)
    icon = QtGui.QIcon()
    icon.addPixmap(QtGui.QPixmap(":/newPrefix/snooker1.png"), 
    QtGui.QIcon.Normal, QtGui.QIcon.Off)
    MainWindow.setWindowIcon(icon)
    MainWindow.setStyleSheet("QMainWindow{\n"
                             "background-image: url(:/newPrefix/snk.jpg);\n"
                             "}\n"
                             "")
    MainWindow.setIconSize(QtCore.QSize(38, 38))
    self.centralwidget = QtWidgets.QWidget(MainWindow)

if name == " main ": import sys

app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

This is the function which i'm using:

def data(self):
    MainWindow1 = QtWidgets.QMainWindow()
    ui = Ui_MainWindow_6()
    ui.setupUi_6(MainWindow1)
    MainWindow1.show()  

Not quite sure what and where you want to import from, but try the example below:

main.py

from PyQt5 import QtCore, QtGui, QtWidgets
from MainWindow_1 import Ui_MainWindow
from MainWindow_6 import Ui_MainWindow_6


class Window2(QtWidgets.QMainWindow, Ui_MainWindow_6):
    def __init__(self):
        super().__init__()

        self.setupUi_6(self)

        self.btn2 = QtWidgets.QPushButton("import \n first window", self)
        self.btn2.setGeometry(5, 5, 70, 70)


class Window(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()

        self.setupUi(self)

        btn = QtWidgets.QPushButton("import \n second window", self)
        btn.setGeometry(50, 50, 100, 100)
        btn.clicked.connect(self.on_clicked_1)

    def on_clicked_1(self):
        self.window2 = Window2()
        self.window2.btn2.clicked.connect(self.on_clicked_2)
        self.hide()
        self.window2.show()

    def on_clicked_2(self):
        self.window2.hide()
        self.show()


if __name__ == '__main__':  
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = Window()
    w.show()
    sys.exit(app.exec_())

MainWindow_1.py

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1419, 768)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("D:/_Qt/img/py-qt.png"), # ":/newPrefix/snooker1.png"
        QtGui.QIcon.Normal, QtGui.QIcon.Off)
        MainWindow.setWindowIcon(icon)
        MainWindow.setStyleSheet("""
                    QMainWindow {
                        background-image: url(D:/_Qt/img/pyqt.jpg);
                    }""")               # url(:/newPrefix/snk.jpg)

        MainWindow.setIconSize(QtCore.QSize(38, 38))
        self.centralwidget = QtWidgets.QWidget(MainWindow)

MainWindow_6.py

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow_6(object):

    def setupUi_6(self, MainWindow1):
        MainWindow1.setObjectName("MainWindow")
        MainWindow1.setFixedSize(462, 488)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("D:/_Qt/img/py-qt.png"),   # ":/newPrefix/snooker1.png"
            QtGui.QIcon.Normal, QtGui.QIcon.Off)
        MainWindow1.setWindowIcon(icon)

        MainWindow1.setStyleSheet("""
                    QMainWindow {
                        background-image: url(D:/_Qt/img/cat.jpg);
                    }""")               # url(:/newPrefix/snk.jpg)

        self.centralwidget = QtWidgets.QWidget(MainWindow1)

在此处输入图片说明

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