Aaron Henderson
Aaron Henderson

Reputation: 191

How can I self hide and show QDialog() in PyQT5?

I have a GUI that was generated using Qt Designer, I used pyuic5 to generate a .py file. In a separate py (program.py) file I import my UI a do all my work there.

program.py

import sys, os, time
from subprocess import call
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyCred_GUI import Ui_Dialog


class MyGUI(Ui_Dialog):
    def __init__(self, dialog):
        Ui_Dialog.__init__(self)
        self.setupUi(dialog)


        self.pushButton_2.clicked.connect(self.cancelbutton)

    def cancelbutton(self):
        exit()


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    dialog = QtWidgets.QDialog()
    dialog.setWindowFlags(QtCore.Qt.WindowSystemMenuHint)
    prog = MyGUI(dialog)

    dialog.show()
    sys.exit(app.exec_())

I pulled a lot out just to focus on the issue here. When I click my Cancel button, I want the window to hide, set a timer, and then reappear after so many seconds. I have tried every combination of self.close() self.hide() self.destroy() and none of them hide my window. I get an error that says

"AttributeError: 'MyGUI' object has no attribute 'hide'"

Which makes sense because MyGUI doesn't have a hide() function. I am at a complete loss on how to hide this window.

EDIT (Solved) For future people, as suggested by Hi Im Frogatto dialog.hide() worked.

Upvotes: 0

Views: 3490

Answers (1)

frogatto
frogatto

Reputation: 29285

In your code snippet, dialog is of type QDialog and thereby having hide method. However instances of MyGUI class seem to not have such a method. So, if you write dialog.hide() in that __init__() function, you can hide it.

Upvotes: 1

Related Questions