从另一个QMainWindow调用时,QMainWindow消失

时间:2019-01-30 23:58:57

标签: python pyqt5

这是我要在第一个窗口中导入的第二个窗口,但它将闪烁并消失。

从PyQt5导入QtCore,QtGui,QtWidgets

Ui_MainWindow_6类(对象):

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)

如果名称 ==“ 主要”:     导入系统

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

这是我的第一个窗口:

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)

如果名称 ==“ 主要”:     导入系统

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

这是我正在使用的功能:

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

1 个答案:

答案 0 :(得分:0)

不太清楚要从哪里导入,但是请尝试以下示例:

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)

enter image description here