Как можно заморозить главное окно на время, пока открыто другое?

Есть простенький код на PyQt5:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QAction
from PyQt5.QtGui import QIcon, QFont


class Example(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()           # Main window initialization

    def initUI(self):
        grid = QWidget()             # add QWidget
        self.setCentralWidget(grid)  # set on QMainWindow

        # action settings
        # Create Action
        saveAction = QAction(QIcon(r'images\icon.png'), "File", self)
        saveAction.setShortcut("Ctrl+N")                       # ShortCut(call)
        saveAction.setStatusTip('Open new File')               # hint on hover
        saveAction.setFont(QFont("Arial", 21))                 # Font

        # reference action
        # create Action
        hintAction = QAction(QIcon('image.png'), 'Reference', self)
        # Path to slot
        hintAction.triggered.connect(self.show_reference_window)
        hintAction.setFont(QFont("Arial", 21))                    # Set font
        saveAction.setStatusTip('Show quick guide')               # Show hint

        # settings of menubar
        menuBar = self.menuBar()                               # Add menu bar
        menuBar.setFont(QFont("Arial", 21))        # Font size of menubar items
        fileMenu = menuBar.addMenu("File")                     # Add 'File'
        fileMenu.setFont(QFont("Arial", 18))                   # ShortCut font
        fileMenu.addAction(saveAction)                      # add subparagraph
        referenceMenu = menuBar.addMenu("Reference")
        referenceMenu.addAction(hintAction)

        # window settings
        self.setWindowTitle("Diary")
        self.setGeometry(60, 70, 1602, 727)
        self.show()

    def show_reference_window(self):                # hint window
        self.w = QWidget()
        # to set it to always be the current size
        self.setWindowTitle("Application Help")     # Title
        self.w.setFixedSize(self.w.size())
        self.w.show()


if __name__ == "__main__":
    app = QApplication(sys.argv)        # Create application
    win = Example()
    sys.exit(app.exec())                # start processing

Как можно "заморозить" основное окно Diary, пока открыто окно Application Help?

Хочу реализовать такую возможность:

введите сюда описание изображения

То есть, окно блокнота не реагирует на действия, пока открыто другое, которое ждет команды 'ок'. Заранее благодарю!


Ответы (1 шт):

Автор решения: S. Nick

Класс QDialog является базовым классом диалоговых окон.

int QDialog::exec()

Отображает диалоговое окно как модальное диалоговое окно, блокируя его до тех пор, пока пользователь не закроет его. Функция возвращает результат DialogCode.

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QAction, \
    QLabel, QVBoxLayout, QDialog
from PyQt5.QtGui import QIcon, QFont
from PyQt5.QtCore import Qt


class Example(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()                

    def initUI(self):
        self.centralWidget = QWidget()                    # centralWidget
        self.setCentralWidget(self.centralWidget)         # set on QMainWindow

        self.label = QLabel('Hello World', alignment=Qt.AlignCenter)            # +
        
        self.layout = QVBoxLayout(self.centralWidget)                           # +
        self.layout.addWidget(self.label)                                       # +
        

        # action settings
        # Create Action
        saveAction = QAction(QIcon(r'images\IOS.png'), "File", self)
        saveAction.setShortcut("Ctrl+N")                       
        saveAction.setStatusTip('Open new File')               
        saveAction.setFont(QFont("Arial", 21))                 

        # reference action
        # create Action
        hintAction = QAction(QIcon('im.png'), 'Reference', self)
        # Path to slot
        hintAction.triggered.connect(self.show_reference_window)
        hintAction.setFont(QFont("Arial", 21))                    
        saveAction.setStatusTip('Show quick guide')               

        # settings of menubar
        menuBar = self.menuBar()                               
        menuBar.setFont(QFont("Arial", 21))        
        fileMenu = menuBar.addMenu("File")                   
        fileMenu.setFont(QFont("Arial", 18))                  
        fileMenu.addAction(saveAction)                     
        referenceMenu = menuBar.addMenu("Reference")
        referenceMenu.addAction(hintAction)

    def show_reference_window(self):                
#        self.w = QWidget()
        self.w = QDialog()                                                   # !!! +++   QDialog 
        
        self.label = QLabel('Hello QDialog', alignment=Qt.AlignCenter)       # +
        
        self.layout = QVBoxLayout(self.w)                                    # +
        self.layout.addWidget(self.label)                                    # +        
        
        # to set it to always be the current size
        self.w.setWindowTitle("Application Help")                            # self.w
#        self.w.setFixedSize(self.w.size())
        self.w.resize(self.w.size())                                         

#        self.w.show()
        self.w.exec()                                                        # !!! +++   exec                                              
        

if __name__ == "__main__":
    app = QApplication(sys.argv)        
    win = Example()
    win.setWindowTitle("Diary")
    win.resize(802, 500)
    win.show()    
    
    sys.exit(app.exec())                

введите сюда описание изображения

→ Ссылка