2024-08-12 16:33:09
浏览数 (3)
QMessageBox的创建以及按钮
示例代码
代码语言:python
代码运行次数:0
复制def new_message_box():
message_box = QMessageBox()
message_box.setWindowTitle('这是一个 QMessageBox 实例')
# 设置文本
message_box.setText(f'{get_time_str()}')
# 设置按钮
message_buttons = QMessageBox.StandardButton.Ok
for i in QMessageBox.StandardButton:
message_buttons = message_buttons | i
message_box.setStandardButtons(message_buttons)
# 对话框运行
ret = message_box.exec()
# 获取按钮值
print('message box clicked: ', QMessageBox.StandardButton(ret).name)
运行效果
QMessageBox常见的标准窗口
示例代码
代码语言:python
代码运行次数:0
复制from __future__ import annotations
import sys
from datetime import datetime
from PySide6.QtWidgets import QApplication, QMainWindow, QMessageBox, QPushButton, QVBoxLayout, QWidget
def get_time_str() -> str:
return datetime.now().isoformat(sep = ' ')
def new_message_box():
message_box = QMessageBox()
message_box.setWindowTitle('这是一个 QMessageBox 实例')
# 设置文本
message_box.setText(f'{get_time_str()}')
# 设置按钮
message_buttons = QMessageBox.StandardButton.Ok
for i in QMessageBox.StandardButton:
message_buttons = message_buttons | i
message_box.setStandardButtons(message_buttons)
# 对话框运行
ret = message_box.exec()
# 获取按钮值
print('message box clicked: ', QMessageBox.StandardButton(ret).name)
class MyMainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('QMessageBox类型')
self.button = QPushButton('生成QMessageBox')
self.button.clicked.connect(new_message_box)
self.about_button = QPushButton('生成QMessageBox.about')
self.about_button.clicked.connect(self.new_about_box)
self.critical_button = QPushButton('生成QMessageBox.critical')
self.critical_button.clicked.connect(self.new_critical_box)
self.information_button = QPushButton('生成QMessageBox.information')
self.information_button.clicked.connect(self.new_information_box)
self.question_button = QPushButton('生成 QMessageBox.question')
self.question_button.clicked.connect(self.new_question_box)
self.warning_button = QPushButton('生成 QMessageBox.warning')
self.warning_button.clicked.connect(self.new_warning_box)
v_layout = QVBoxLayout()
v_layout.addWidget(self.button)
v_layout.addWidget(self.about_button)
v_layout.addWidget(self.critical_button)
v_layout.addWidget(self.information_button)
v_layout.addWidget(self.question_button)
v_layout.addWidget(self.warning_button)
container = QWidget()
container.setLayout(v_layout)
self.setCentralWidget(container)
def new_about_box(self):
QMessageBox.about(self, '这是一个 About 窗口', f'About 窗口: {get_time_str()}')
def new_critical_box(self):
ret = QMessageBox.critical(self, '这是一个 Critical 窗口', f'Critical 窗口: {get_time_str()}')
print('critical box clicked:', QMessageBox.StandardButton(ret).name)
def new_information_box(self):
QMessageBox.information(self, '这是一个 Information 窗口', f'Information 窗口: {get_time_str()}')
def new_question_box(self):
ret = QMessageBox.question(self, '这是一个 Question 窗口', f'Question 窗口: {get_time_str()}')
print('question box clicked:', QMessageBox.StandardButton(ret).name)
def new_warning_box(self):
ret = QMessageBox.warning(self, '这是一个 Warning 窗口', f'Warning 窗口: {get_time_str()}')
print('warning box clicked:', QMessageBox.StandardButton(ret).name)
if __name__ == "__main__":
app = QApplication(sys.argv)
ins = MyMainWindow()
ins.show()
sys.exit(app.exec())
运行效果
触发按钮列表
about窗口
critical窗口
information窗口
question窗口
warning窗口