如何在 PyQt5 的弹出窗口中隐藏主窗口(父)? [英] How to hide the Main window(parent) from pop up window in PyQt5.?

查看:481
本文介绍了如何在 PyQt5 的弹出窗口中隐藏主窗口(父)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主窗口,从中我可以调用豁免窗口(弹出窗口).在豁免弹出窗口上单击确定后,我想关闭豁免弹出窗口并隐藏主窗口.我已将 self.parent().hide 包含在豁免弹出窗口中,但其抛出错误进程已完成,退出代码为 1073741845".弹出窗口关闭,主窗口突然终止.

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

<小时>

ma​​in.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()

推荐答案

您没有使用父级创建弹出窗口,因此 self.parent() 返回 None,然后您尝试对不存在的对象调用 hide().调用弹出窗口的方式,在其中创建popup_on_waiver 类的实例在类本身,有点奇怪.此外,从 QDialog 继承而不是 QMainWindow 的弹出窗口更有意义.现在在Main和父self 中构造弹出窗口小部件,并将豁免按钮连接到QDialog.exec_() 方法:

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

这是编辑后的代码.

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

这篇关于如何在 PyQt5 的弹出窗口中隐藏主窗口(父)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆