前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PySide6 GUI 编程(25):QMessageBox的简单使用

PySide6 GUI 编程(25):QMessageBox的简单使用

原创
作者头像
bowenerchen
发布2024-08-12 16:33:09
2302
发布2024-08-12 16:33:09
举报
文章被收录于专栏:编码视界

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 的按钮
QMessageBox 的按钮

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窗口

about 窗口
about 窗口

critical窗口

critical 窗口
critical 窗口

information窗口

information窗口
information窗口

question窗口

question 窗口
question 窗口

warning窗口

warning 窗口
warning 窗口

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • QMessageBox的创建以及按钮
    • 示例代码
      • 运行效果
      • QMessageBox常见的标准窗口
        • 示例代码
          • 运行效果
            • 触发按钮列表
            • about窗口
            • critical窗口
            • information窗口
            • question窗口
            • warning窗口
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档