清風 发表于 2024-2-25 07:45:06

MessageBox问题

        有如下代码:

# 创建一个消息框
      msg_box = QMessageBox()

      ###
      # 设置标题的字体和颜色
      title_font = QFont('Arial', 12, QFont.Weight.Bold)
      title_color = QColor(255, 0, 0)# 红色
      ###
      msg_box.setWindowTitle('123')
      msg_box.setStyleSheet(f"QLabel#qt_msgbox_label{{ color: {title_color.name()}; }}")
      msg_box.setText(f'<span style="color:{title_color.name()}">{msg_box.text()}</span>')
      msg_box.setFont(title_font)

      # 设置文本的字体和颜色
      text_font = QFont('Arial', 10)
      text_color = QColor(0, 0, 255)# 蓝色
      msg_box.setText('234~~')
      msg_box.setStyleSheet(f"QLabel#qt_msgbox_label{{color: {text_color.name()};}}")
      msg_box.setText(f'<span style="color:{text_color.name()}">{msg_box.text()}</span>')
      msg_box.setFont(text_font)


      # 获取对应的按钮
      yes_button = msg_box.addButton('是', QMessageBox.ButtonRole.YesRole)
      no_button = msg_box.addButton('否', QMessageBox.ButtonRole.NoRole)

      # 设置按钮文本为中文
      yes_button.setText('是')
      no_button.setText('不是')

      # 显示消息框,并获取用户点击的按钮
      clicked_button = msg_box.exec()

      # 做出相应的操作
      if clicked_button == QMessageBox.StandardButton.Yes:

            print('用户点击了是按钮')
      else:
            #
            print('用户点击了不是按钮')
            sys.exit()


请问如何禁用ALT+ F4和右上角的X关闭?

FishC 发表于 2024-2-25 07:45:29

要禁用ALT+F4和右上角的X关闭,可以通过重写消息框的closeEvent()方法来实现。在该方法中,我们可以检测到关闭事件,并取消默认的关闭行为。

下面是修改后的代码示例:

from PyQt5.QtWidgets import QMessageBox, QApplication
from PyQt5.QtGui import QFont, QColor
import sys

class MyMessageBox(QMessageBox):
    def __init__(self):
      super().__init__()

    def closeEvent(self, event):
      # 拦截关闭事件
      event.ignore()

if __name__ == '__main__':
    app = QApplication(sys.argv)

    msg_box = MyMessageBox()

    # 设置标题的字体和颜色
    title_font = QFont('Arial', 12, QFont.Weight.Bold)
    title_color = QColor(255, 0, 0)# 红色
    msg_box.setWindowTitle('123')
    msg_box.setStyleSheet(f"QLabel#qt_msgbox_label{{ color: {title_color.name()}; }}")
    msg_box.setText(f'{msg_box.text()}')
    msg_box.setFont(title_font)

    # 设置文本的字体和颜色
    text_font = QFont('Arial', 10)
    text_color = QColor(0, 0, 255)# 蓝色
    msg_box.setText('234~~')
    msg_box.setStyleSheet(f"QLabel#qt_msgbox_label{{color: {text_color.name()};}}")
    msg_box.setText(f'{msg_box.text()}')
    msg_box.setFont(text_font)

    # 获取对应的按钮
    yes_button = msg_box.addButton('是', QMessageBox.ButtonRole.YesRole)
    no_button = msg_box.addButton('否', QMessageBox.ButtonRole.NoRole)

    # 设置按钮文本为中文
    yes_button.setText('是')
    no_button.setText('不是')

    # 显示消息框,并获取用户点击的按钮
    clicked_button = msg_box.exec()

    # 做出相应的操作
    if clicked_button == QMessageBox.StandardButton.Yes:
      print('用户点击了是按钮')
    else:
      print('用户点击了不是按钮')
      sys.exit()

    sys.exit(app.exec_())

在这个示例中,我们创建了一个名为MyMessageBox的自定义消息框类,继承自QMessageBox。然后,重写了closeEvent()方法,在其中拦截关闭事件并忽略它。

请注意,在示例代码的最后,我们调用了sys.exit(app.exec_())来确保程序正常退出。

这样,ALT+F4和右上角的X关闭按钮将不再起作用。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

清風 发表于 2024-2-25 07:47:19

FishC 发表于 2024-2-25 07:45
要禁用ALT+F4和右上角的X关闭,可以通过重写消息框的closeEvent()方法来实现。在该方法中,我们可以检测到 ...

我是pyQT6

回复请注意python格式

FishC 发表于 2024-2-25 07:47:36

清風 发表于 2024-02-25 07:47
我是pyQT6

回复请注意python格式

非常抱歉,以下是使用PyQt6重写的示例代码:

from PyQt6.QtWidgets import QMessageBox, QApplication
from PyQt6.QtGui import QFont, QColor
import sys

class MyMessageBox(QMessageBox):
    def __init__(self):
      super().__init__()

    def closeEvent(self, event):
      # 拦截关闭事件
      event.ignore()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    msg_box = MyMessageBox()
   
    # 设置标题的字体和颜色
    title_font = QFont('Arial', 12, QFont.Weight.Bold)
    title_color = QColor(255, 0, 0)# 红色
    msg_box.setWindowTitle('123')
    msg_box.setStyleSheet(f"QLabel#qt_msgbox_label{{ color: {title_color.name()}; }}")
    msg_box.setText(f'{msg_box.text()}')
    msg_box.setFont(title_font)

    # 设置文本的字体和颜色
    text_font = QFont('Arial', 10)
    text_color = QColor(0, 0, 255)# 蓝色
    msg_box.setText('234~~')
    msg_box.setStyleSheet(f"QLabel#qt_msgbox_label{{color: {text_color.name()};}}")
    msg_box.setText(f'{msg_box.text()}')
    msg_box.setFont(text_font)

    # 获取对应的按钮
    yes_button = msg_box.addButton('是', QMessageBox.ButtonRole.YesRole)
    no_button = msg_box.addButton('否', QMessageBox.ButtonRole.NoRole)

    # 设置按钮文本为中文
    yes_button.setText('是')
    no_button.setText('不是')

    # 显示消息框,并获取用户点击的按钮
    clicked_button = msg_box.exec()

    # 做出相应的操作
    if clicked_button == QMessageBox.StandardButton.Yes:
      print('用户点击了是按钮')
    else:
      print('用户点击了不是按钮')
      sys.exit()
   
    sys.exit(app.exec())

请注意,在示例代码的最后,我们使用`sys.exit(app.exec())`来确保程序正常退出。这样,ALT+F4和右上角的X关闭按钮将不再起作用。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页: [1]
查看完整版本: MessageBox问题