setWindowModality()设置窗口模态。取值如下:

Qt.NonModal - 非模态
        Qt.WindowModal - 窗口模态
        Qt.ApplicationModal - 应用程序模态

   
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import *
import sys
 
'''本程序显示模态对话框使用,分别使用QWidget和QDialog实现'''
 
class Main(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("主窗口")
        button = QPushButton("弹出子窗", self)
        button.clicked.connect(self.show_child2)
        self.child_window = Child()
        self.child_window2 = Child2()
 
    def show_child(self):
        self.child_window.setWindowModality(Qt.ApplicationModal)
        self.child_window.show()
    def show_child2(self):
        self.child_window2.setWindowModality(Qt.ApplicationModal)
        self.child_window2.exec_()
 
 
class Child(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("我是子窗口")
 
class Child2(QDialog):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("我是子窗口")
 
 
# 运行主窗口
if __name__ == "__main__":
    app = QApplication(sys.argv)
 
    window = Main()
    window.show()
 
    sys.exit(app.exec_())