How To Hide The Main Window(parent) From Pop Up Window In PyQt5.?
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.
Solution 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()
Post a Comment for "How To Hide The Main Window(parent) From Pop Up Window In PyQt5.?"