Python Forum
Pyqt5 - Close a modal window
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pyqt5 - Close a modal window
#1
Hi, guys...

I'm struggling with a quite simple question: How can I close my modal windows ?
I've created them using Pyqt5 and my code looks like:
class Ui_MyClass(object):
  def setupUi(self):
    .....
    .....
When I try to connect my buttons to a close_function, the method close() is not available.
What do I need ?
Reply
#2
Example

from PyQt5.QtWidgets import (QMainWindow, QApplication, QVBoxLayout, QHBoxLayout, 
                             QWidget, QPushButton)

class MainWindow(QMainWindow):
    def __init__(self, parent = None):
        super(MainWindow, self).__init__(parent)
        self.setupUI()
        
    def setupUI(self):
        self.setGeometry(0, 0, 800, 600)
        central_widget = QWidget()
        vbox = QVBoxLayout()
        tb = self.addToolBar("toolbar")
        btn = QPushButton("close window", clicked=self.btn_clicked)
        tb.addWidget(btn)
        central_widget.setLayout(vbox)
        self.setCentralWidget(central_widget)
        
    def btn_clicked(self):
        self.close()


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    win = MainWindow()
    win.setWindowTitle("Main Window")
    win.show()

    sys.exit(app.exec_())
Reply
#3
object doesn't have a close method. Did you define a close method in Ui_MyClass?

From your code it looks like you are working with a designer program. Please pose the code from the setupUI method. There must be something in there that serves as a top level window (can be QMainWindow, QWidget or QDialog). This object has a close method. You could write something like this:
class Gui:

    def __init__(self):
        self.toplevel = QtWidgets.QWidget()
        button = QtWidgets.QPushButton("Close")
        button.clicked.connect(self.close)
        layout = QtWidgets.QVBoxLayout(self.toplevel)
        layout.addWidget(button)
        self.toplevel.show()

    def close(self):
        self.toplevel.close()


app = QtWidgets.QApplication()
gui = Gui()
app.exec()
Reply
#4
Hello...yeah...I've created my window as a widget and set up as modal.
Still can't find the solution to close it.
Wasn't created as 'mainwindow'.
Here's is the code for setupUi:
def setupUi(self, pagMensalid,matr):
      dadosocio = ["",""] #Acrescent
      dadosocio = matr.split(",") #Acrescent
      pagMensalid.setObjectName("pagMensalid")
      pagMensalid.setWindowModality(QtCore.Qt.ApplicationModal)
      pagMensalid.resize(500, 500)
      pagMensalid.setMinimumSize(QtCore.QSize(500, 500))
      pagMensalid.setMaximumSize(QtCore.QSize(500, 500))
      self.centralwidget = QtWidgets.QWidget(pagMensalid)
      self.centralwidget.setObjectName("centralwidget")
      self.label = QtWidgets.QLabel(self.centralwidget)
      self.label.setGeometry(QtCore.QRect(100, 10, 341, 41))
      font = QtGui.QFont()
      font.setFamily("Arial")
      font.setPointSize(16)
      font.setBold(True)
      font.setWeight(75)
      self.label.setFont(font)
      self.label.setObjectName("label")
      self.label_2 = QtWidgets.QLabel(self.centralwidget)
      self.label_2.setGeometry(QtCore.QRect(40, 70, 81, 31))
      font = QtGui.QFont()
      font.setFamily("Arial")
      font.setPointSize(14)
      self.label_2.setFont(font)
      self.label_2.setObjectName("label_2")
      self.label_3 = QtWidgets.QLabel(self.centralwidget)
      self.label_3.setGeometry(QtCore.QRect(40, 120, 71, 31))
      font = QtGui.QFont()
      font.setFamily("Arial")
      font.setPointSize(14)
      self.label_3.setFont(font)
      self.label_3.setObjectName("label_3")
      self.txtmatr = QtWidgets.QTextEdit(self.centralwidget)
      self.txtmatr.setGeometry(QtCore.QRect(130, 70, 104, 31))
      font = QtGui.QFont()
      font.setFamily("Arial")
      font.setPointSize(12)
      self.txtmatr.setFont(font)
      self.txtmatr.setObjectName("txtmatr")
      self.txtnome = QtWidgets.QTextEdit(self.centralwidget)
      self.txtnome.setGeometry(QtCore.QRect(110, 120, 311, 31))
      font = QtGui.QFont()
      font.setFamily("Arial")
      font.setPointSize(12)
      self.txtnome.setFont(font)
      self.txtnome.setObjectName("txtnome")
      self.label_4 = QtWidgets.QLabel(self.centralwidget)
      self.label_4.setGeometry(QtCore.QRect(40, 180, 211, 16))
      font = QtGui.QFont()
      font.setFamily("Arial")
      font.setPointSize(14)
      font.setBold(True)
      font.setWeight(75)
      self.label_4.setFont(font)
      self.label_4.setObjectName("label_4")
      self.tblparc = QtWidgets.QTableWidget(self.centralwidget)
      self.tblparc.setGeometry(QtCore.QRect(40, 210, 441, 192))
      self.tblparc.setStyleSheet("border-color: rgb(0, 0, 0);")
      self.tblparc.setObjectName("tblparc")
      self.tblparc.setColumnCount(3)
      self.tblparc.setRowCount(0)
      item = QtWidgets.QTableWidgetItem()
      font = QtGui.QFont()
      font.setFamily("Arial")
      font.setPointSize(12)
      font.setBold(True)
      font.setWeight(75)
      item.setFont(font)
      item.setBackground(QtGui.QColor(255, 255, 0))
      self.tblparc.setHorizontalHeaderItem(0, item)
      item = QtWidgets.QTableWidgetItem()
      font = QtGui.QFont()
      font.setFamily("Arial")
      font.setPointSize(12)
      font.setBold(True)
      font.setWeight(75)
      item.setFont(font)
      item.setBackground(QtGui.QColor(255, 255, 0))
      self.tblparc.setHorizontalHeaderItem(1, item)
      item = QtWidgets.QTableWidgetItem()
      font = QtGui.QFont()
      font.setFamily("Arial")
      font.setPointSize(12)
      font.setBold(True)
      font.setWeight(75)
      item.setFont(font)
      item.setBackground(QtGui.QColor(255, 255, 0))
      self.tblparc.setHorizontalHeaderItem(2, item)
      self.label_5 = QtWidgets.QLabel(self.centralwidget)
      self.label_5.setGeometry(QtCore.QRect(40, 440, 500, 600))
      self.label_5.setMinimumSize(QtCore.QSize(500, 600))
      self.label_5.setMaximumSize(QtCore.QSize(500, 600))
      font = QtGui.QFont()
      font.setFamily("Arial")
      font.setPointSize(14)
      font.setBold(False)
      font.setWeight(50)
      self.label_5.setFont(font)
      self.label_5.setObjectName("label_5")
      self.label_6 = QtWidgets.QLabel(self.centralwidget)
      self.label_6.setGeometry(QtCore.QRect(40, 420, 81, 20))
      font = QtGui.QFont()
      font.setFamily("Arial")
      font.setPointSize(14)
      font.setBold(True)
      font.setWeight(75)
      self.label_6.setFont(font)
      self.label_6.setObjectName("label_6")
      self.txttotal = QtWidgets.QTextEdit(self.centralwidget)
      self.txttotal.setGeometry(QtCore.QRect(120, 410, 104, 31))
      font = QtGui.QFont()
      font.setFamily("Arial")
      font.setPointSize(12)
      font.setBold(True)
      font.setWeight(75)
      self.txttotal.setFont(font)
      self.txttotal.setObjectName("txttotal")
      self.btnsair = QtWidgets.QPushButton(self.centralwidget)
      self.btnsair.setGeometry(QtCore.QRect(250, 460, 75, 23))
      font = QtGui.QFont()
      font.setFamily("Arial")
      font.setPointSize(14)
      font.setBold(True)
      font.setWeight(75)
      self.btnsair.setFont(font)
      self.btnsair.setStyleSheet("background-color: rgb(170, 170, 255);")
      self.btnsair.setObjectName("btnsair")
      self.btnquitar = QtWidgets.QPushButton(self.centralwidget)
      self.btnquitar.setGeometry(QtCore.QRect(150, 460, 75, 23))
      font = QtGui.QFont()
      font.setFamily("Arial")
      font.setPointSize(14)
      font.setBold(True)
      font.setWeight(75)
      self.btnquitar.setFont(font)
      self.btnquitar.setStyleSheet("background-color: rgb(170, 170, 255);")
      self.btnquitar.setObjectName("btnquitar")
      pagMensalid.setCentralWidget(self.centralwidget)
      self.statusbar = QtWidgets.QStatusBar(pagMensalid)
      self.statusbar.setObjectName("statusbar")
      pagMensalid.setStatusBar(self.statusbar)
      self.txtmatr.setReadOnly(True) 
      self.txtnome.setReadOnly(True)
      self.txttotal.setReadOnly(True)
      self.retranslateUi(pagMensalid)
      QtCore.QMetaObject.connectSlotsByName(pagMensalid)
Reply
#5
Post the code, otherwise nobody can help.

deanhystad's example works. (in PyQt5 you should use app.exec_

from PyQt5.QtWidgets import (QApplication, QVBoxLayout,  
                             QWidget, QPushButton)

class Gui(object):
    def __init__(self):
        self.toplevel = QWidget()
        button = QPushButton("Close")
        button.clicked.connect(self.close)
        layout = QVBoxLayout(self.toplevel)
        layout.addWidget(button)
        self.toplevel.show()
 
    def close(self):
        self.toplevel.close()
 
if __name__ == '__main__': 
    app = QApplication([])
    gui = Gui()
    app.exec_()
Reply
#6
Yes, Axel...I edited my post and included above how the code looks like. I created window as widget.
Reply
#7
In your code I don't see anything that tries to close a window. Where is that code?

This is my example written more along the lines of your code.
from PySide6 import QtWidgets, QtCore


class Dialog:
    def setup_ui(self, window):
        self.window = window
        window.setWindowTitle("Dialog")
        window.setWindowModality(QtCore.Qt.ApplicationModal)
        button = QtWidgets.QPushButton("Push this to close the dialog window.")
        button.clicked.connect(self.close)
        layout = QtWidgets.QVBoxLayout(window)
        layout.addWidget(button)

    def close(self):
        self.window.close()

    def show(self):
        self.window.show()


class Window(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Main Window")
        self.dialog = Dialog()
        self.dialog.setup_ui(QtWidgets.QWidget())
        layout = QtWidgets.QVBoxLayout(self)
        button = QtWidgets.QPushButton("Open up the dialog window.")
        button.clicked.connect(self.dialog.show)
        layout.addWidget(button)
        button = QtWidgets.QPushButton("Try to close the dialog window.  Will fail.")
        button.clicked.connect(self.dialog.close)
        layout.addWidget(button)

app = QtWidgets.QApplication()
window = Window()
window.show()
app.exec()
Pressing the button on the main window will not close the dialog window because the dialog window is modal and the main window cannot get mouse or keyboard focus while the dialog is visible. Your modal window needs to have a control for closing itself. If you want controls on another window to close the modal window, the window is not modal.
Reply
#8
(Apr-19-2025, 08:52 PM)Ninja2112 Wrote: ... above how the code looks like. I created window as widget.

On your code you can see the QtDesigner produced terrible code when you do everything by drag & drop.
Buttons should be placed in a toolbar or a box.
Everything should be placed in a reasonable layout.
If you really want to learn something then forget the designer.
Reply
#9
Yes, I have a method in this class that tries to close the window.
When I try to close this way:
def closewindow(self):
      self.centralwidget.close()
The content dissapears but the window remains.
In fact my great doubt is which object do I have to call inside this function.
Reply
#10
centralwidget is not the window. It is just a widget inside the window. The window you want to close is pagMensalid. You'll need to assign an instance variable to remember pagMensalid for later
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Modal window DPaul 16 9,345 Nov-05-2024, 04:18 PM
Last Post: kaliumster
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 2,624 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  [PyQt] PyQt5 window closing when trying to display a graph bianca 4 4,062 Aug-12-2023, 03:25 PM
Last Post: bianca
  [PyQt] command require close window Krissstian 14 6,291 Nov-19-2022, 04:18 PM
Last Post: Krissstian
  tkinter window and turtle window error 1885 3 8,291 Nov-02-2019, 12:18 PM
Last Post: 1885
  Huge code problems (buttons(PyQt5),PyQt5 Threads, Windows etc) ZenWoR 0 3,515 Apr-06-2019, 11:15 PM
Last Post: ZenWoR
  [PyQt] Can't show PyQt5 mediaPlayer in window DecaK 1 6,267 Sep-15-2018, 09:54 AM
Last Post: Axel_Erfurt
  Launch pdf and close on delete window event ashtona 6 7,931 Mar-22-2018, 03:04 PM
Last Post: ashtona
  Closing Modal Window in QT nieselfriem 0 5,477 Apr-19-2017, 03:32 PM
Last Post: nieselfriem
  update a variable in parent window after closing its toplevel window gray 5 11,229 Mar-20-2017, 10:35 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020