pyside6设置确认谈话窗
时间: 2025-01-10 13:41:34 浏览: 60
### 如何在 PySide6 中设置确认对话框
为了创建并配置一个确认对话框,在 PySide6 中可以利用 `QMessageBox` 类来实现这一目标。下面是一个简单的实例,展示了如何构建一个带有“是”和“否”的确认对话框。
```python
from PySide6.QtWidgets import QApplication, QMessageBox, QPushButton, QWidget
class ConfirmDialogExample(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('Confirm Dialog Example')
button = QPushButton('Show Confirmation', self)
button.clicked.connect(self.show_confirmation_dialog)
def show_confirmation_dialog(self):
confirm = QMessageBox.question(
self,
'Confirmation',
'Do you want to proceed?',
buttons=QMessageBox.Yes | QMessageBox.No,
defaultButton=QMessageBox.No
)
if confirm == QMessageBox.Yes:
print("User chose Yes.")
elif confirm == QMessageBox.No:
print("User chose No.")
if __name__ == '__main__':
app = QApplication([])
window = ConfirmDialogExample()
window.resize(300, 200)
window.show()
app.exec_()
```
上述代码定义了一个名为 `ConfirmDialogExample` 的类继承自 `QWidget`, 创建按钮用于触发确认对话框的显示[^1]。当点击该按钮时会弹出询问用户是否继续的消息框;消息框提供两个选项:“Yes” 和 “No”。依据用户的响应执行不同的操作逻辑[^2]。
阅读全文
相关推荐

















