Python 如何在pyqt中创建模态窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24697347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 05:01:47  来源:igfitidea点击:

How to create a modal window in pyqt?

pythonpython-2.7modal-dialogpyqtpyqt4

提问by user3135832

I looked into the documentation and i found 'self.setWindowModality(QtCore.Qt.WindowModal)'.
I added this function to my 'init' function, but however still was not able to create a modal dialog box.

我查看了文档,发现“self.setWindowModality(QtCore.Qt.WindowModal)”。
我将此函数添加到我的“ init”函数中,但仍然无法创建模态对话框。

Any help will be appreciated,
Thank You.

任何帮助将不胜感激,
谢谢。

采纳答案by Shadow9043

QDialoghas setModal()as found here.

QDialogsetModal()找到here

As the docs state:

正如文档所述:

By default, this property is Falseand show()pops up the dialog as modeless. Setting this property to true is equivalent to setting QWidget.windowModalityto Qt.ApplicationModal.

默认情况下,这个属性是Falseshow()弹出的对话框与非模态。将此属性设置为 true 等同于设置 QWidget.windowModalityQt.ApplicationModal

As @sebastian noted you could use exec(). However it is better to use exec_()as the one sebastian mentioned is also a python call.

正如@sebastian 指出的那样,您可以使用exec(). 但是最好使用,exec_()因为 sebastian 提到的也是一个 python 调用。

Example:

例子:

my_dialog = QDialog(self) 
my_dialog.exec_()  # blocks all other windows until this window is closed.

If this doesn't help please post your code and I will have a look.

如果这没有帮助,请发布您的代码,我会看看。