How to hide the Main window(parent) from pop up window in PyQt5.?

让人想犯罪 __ 提交于 2021-02-11 14:47:14

问题


I have a Main window from where I am calling Waiver window(popup). On clicking OK on Waiver popup, I want to close the Waiver pop up and hide the Main window. I have included self.parent().hide in waiver pop up but its throwing error "Process finished with exit code 1073741845" . The pop up closes and the main window terminates abruptly.

waiver_window.py

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt, QSize, QRect


class popup_on_waiver(QMainWindow):

    def __init__(self):
        QMainWindow.__init__(self)
        self.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.CustomizeWindowHint)
        self.setMinimumSize(QSize(660, 340))
        self.setWindowTitle("Waiver")

        vbox = QVBoxLayout()

        centralWidget = QWidget(self)
        self.setCentralWidget(centralWidget)
        self.cb = QComboBox(centralWidget)
        self.cb.setGeometry(QRect(40, 40, 100, 30))
        self.cb.addItem("1")
        self.cb.addItem("2")
        self.cb.addItem("3")
        self.cb.addItem("4")
        self.cb.setObjectName("combobox")
        self.cb.move(80, 80)

        self.OK = QPushButton('OK',self)
        self.OK.setStyleSheet('QPushButton {font-size: 14px; font: Bold; font-family: Verdana; background-color: Orange; color: White}')
        self.OK.move(400,280)

        self.Cancel = QPushButton('Cancel', self)
        self.Cancel.setStyleSheet('QPushButton {font-size: 14px; font: Bold; font-family: Verdana; background-color: Orange; color: White}')
        self.Cancel.move(520, 280)

        vbox.addWidget(self.cb)
        vbox.addWidget(self.OK)
        vbox.addWidget(self.Cancel)

        self.setLayout(vbox)

        self.OK.clicked.connect(self.hide_main)
        self.Cancel.clicked.connect(self.close)

        self.show()

    def hide_main(self):
        self.close
        self.parent().hide()

    def waiverClicked(self):
        self.p = popup_on_waiver()

main.py

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import QUrl, Qt, QTimer
from gui_package.waiver_window import popup_on_waiver
import sys

class Main(QMainWindow):

    def __init__(self):
        QMainWindow.__init__(self)
        self.setWindowFlags(Qt.WindowStaysOnTopHint)
        self.initUI()

    def initUI(self):
        self.centralwidget = QWidget(self)
        hbox = QHBoxLayout()
        self.Waiver = QPushButton('Waiver', self)
        self.Waiver.clicked.connect(lambda: popup_on_waiver.waiverClicked(self))
        hbox.addWidget(self.Waiver)
        self.centralwidget.setLayout(hbox)
        self.setGeometry(50, 50, 1200, 600)
        self.setWindowTitle("Timesheet")
        self.setWindowIcon(QIcon(""))
        self.setStyleSheet("background-color:")
        self.setCentralWidget(self.centralwidget)
        self.show()

回答1:


You are not creating the popup window with a parent, so self.parent() returns None, and then you are trying to call hide() on an object that doesn't exist. The way you call the popup window, where you create an instance of the popup_on_waiver class within the class itself, is kind of strange. Additionally, it makes more sense for the popup to inherit from QDialog instead of QMainWindow. Now construct the popup widget inside the Main class with parent self, and connect the waiver button to the QDialog.exec_() method:

popup = popup_on_waiver(self)
self.Waiver.clicked.connect(popup.exec_)

Here is the edited code.

class popup_on_waiver(QDialog):

    def __init__(self, parent=None):
        super().__init__(parent)
        self.setMinimumSize(QSize(660, 340))
        self.setWindowTitle("Waiver")

        self.cb = QComboBox()
        self.cb.setGeometry(QRect(40, 40, 100, 30))
        self.cb.addItems(["1", "2", "3", "4"])
        self.cb.setObjectName("combobox")
        self.cb.move(80, 80)

        self.OK = QPushButton('OK')
        self.OK.setStyleSheet('QPushButton {font-size: 14px; font: Bold; font-family: Verdana; background-color: Orange; color: White}')
        self.OK.move(400, 280)

        self.Cancel = QPushButton('Cancel')
        self.Cancel.setStyleSheet('QPushButton {font-size: 14px; font: Bold; font-family: Verdana; background-color: Orange; color: White}')
        self.Cancel.move(520, 280)

        vbox = QVBoxLayout(self)
        vbox.addWidget(self.cb)
        vbox.addWidget(self.OK)
        vbox.addWidget(self.Cancel)

        self.OK.clicked.connect(self.hide_main)
        self.Cancel.clicked.connect(self.reject)

    def hide_main(self):
        self.accept()
        self.parent().hide()


class Main(QMainWindow):

    def __init__(self):
        super().__init__()
        self.setWindowFlags(Qt.WindowStaysOnTopHint)
        self.initUI()

    def initUI(self):
        self.centralwidget = QWidget(self)
        self.Waiver = QPushButton('Waiver')

        popup = popup_on_waiver(self)
        self.Waiver.clicked.connect(popup.exec_)

        hbox = QHBoxLayout()
        hbox.addWidget(self.Waiver)
        self.centralwidget.setLayout(hbox)
        self.setGeometry(50, 50, 1200, 600)
        self.setWindowTitle("Timesheet")
        self.setWindowIcon(QIcon(""))
        self.setStyleSheet("background-color:")
        self.setCentralWidget(self.centralwidget)
        self.show()


来源:https://stackoverflow.com/questions/60775809/how-to-hide-the-main-windowparent-from-pop-up-window-in-pyqt5

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!