Skip to content

Commit dc2094c

Browse files
committed
Add 03-12-05-01
QMessageBox-简介与创建
1 parent 6864b43 commit dc2094c

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import sys
2+
3+
from PySide6 import QtWidgets
4+
5+
"""
6+
QMessageBox 信息提示框
7+
8+
信息提示框是一种模态对话框,用于向用户提示信息,或向用户提出一个问题并接收回答
9+
官方文档:https://doc.qt.io/qtforpython/PySide6/QtWidgets/QMessageBox.html
10+
继承自QDialog
11+
12+
13+
有两种构造函数,简易版可选传入父控件,完整版则可以在创建时设置几乎所有属性:
14+
15+
.__init__(self, parent: Optional[QWidget] = None)
16+
17+
.__init__(self, icon: QMessageBox.Icon, title: str, text: str, \
18+
buttons: QMessageBox.StandardButtons = QMessageBox.StandardButton.NoButton, parent: Optional[QWidget] = None, \
19+
flags: Qt.WindowFlags = Instance(Qt.Dialog | Qt.MSWindowsFixedSizeDialogHint))
20+
21+
若只需临时使用一次对话框,显式创建实例并调用.open()的操作,可以用静态方法替代,详情见本目录下对应小节
22+
23+
"""
24+
25+
26+
class MyWidget(QtWidgets.QWidget):
27+
def __init__(self, *args, **kwargs):
28+
super().__init__(*args, **kwargs)
29+
self.setWindowTitle("QMessageBox-信息提示框")
30+
self.resize(800, 600)
31+
self.setup_ui()
32+
33+
def setup_ui(self) -> None:
34+
"""设置界面"""
35+
36+
# 第一种构造函数
37+
# message_box = QtWidgets.QMessageBox(self)
38+
# message_box.setIcon(QtWidgets.QMessageBox.Warning)
39+
# message_box.setWindowTitle("这是一个消息提示框")
40+
# message_box.setText("您不能在关闭模态对话框前操作其他窗口!")
41+
# message_box.setStandardButtons(QtWidgets.QMessageBox.Ok | QtWidgets.QMessageBox.Cancel)
42+
43+
# 以上设置等价于第二种构造函数:
44+
message_box = QtWidgets.QMessageBox(
45+
QtWidgets.QMessageBox.Warning,
46+
"这是一个消息提示框",
47+
"您不能在关闭模态对话框前操作其他窗口!",
48+
QtWidgets.QMessageBox.Ok | QtWidgets.QMessageBox.Cancel,
49+
self,
50+
)
51+
52+
pop_btn = QtWidgets.QPushButton("弹出对话框", self)
53+
pop_btn.move(200, 200)
54+
pop_btn.clicked.connect(message_box.open) # type: ignore
55+
56+
57+
if __name__ == "__main__":
58+
app = QtWidgets.QApplication(sys.argv)
59+
window = MyWidget()
60+
window.show()
61+
sys.exit(app.exec())

0 commit comments

Comments
 (0)