关闭QMessageBox的X按钮
时间: 2023-10-29 07:12:30 浏览: 72
如果您使用的是 PyQt5,您可以通过以下方式禁用 QMessageBox 的 X 按钮:
```python
msg_box = QMessageBox()
msg_box.setStandardButtons(QMessageBox.Ok)
msg_box.button(QMessageBox.Ok).setEnabled(True)
msg_box.setWindowFlags(QtCore.Qt.CustomizeWindowHint | QtCore.Qt.WindowTitleHint | QtCore.Qt.WindowCloseButtonHint)
msg_box.exec_()
```
在上面的代码中,我们使用 `setWindowFlags()` 设置了 QMessageBox 的窗口标志,其中包括了 `QtCore.Qt.WindowCloseButtonHint` 表示显示关闭按钮,`QtCore.Qt.CustomizeWindowHint` 表示自定义窗口,`QtCore.Qt.WindowTitleHint` 表示显示窗口标题。
然后,我们使用 `setEnabled(True)` 方法启用了 QMessageBox 的“确定”按钮。
如果您使用的是 PySide2,则可以使用类似的方法:
```python
msg_box = QtWidgets.QMessageBox()
msg_box.setStandardButtons(QtWidgets.QMessageBox.Ok)
msg_box.button(QtWidgets.QMessageBox.Ok).setEnabled(True)
msg_box.setWindowFlags(QtCore.Qt.CustomizeWindowHint | QtCore.Qt.WindowTitleHint | QtCore.Qt.WindowCloseButtonHint)
msg_box.exec_()
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)