清風 发表于 2024-2-24 20:33:29

QMessageBox问题!

有如下代码:

# 创建一个消息框
      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()


怎么运行下来都是在执行else命令,显示用户选择了不是按钮?并且程序也不退出。。。

FishC 发表于 2024-2-24 20:33:55

问题出在你设置按钮文本时的代码上。在调用`yes_button.setText('是')`和`no_button.setText('不是')`之前,你需要先调用`msg_box.setStandardButtons(QMessageBox.Yes | QMessageBox.No)`来设置消息框的标准按钮。

修改后的代码如下:

# 创建一个消息框
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'{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)

# 设置标准按钮
msg_box.setStandardButtons(QMessageBox.Yes | QMessageBox.No)

# 获取对应的按钮
yes_button = msg_box.button(QMessageBox.Yes)
no_button = msg_box.button(QMessageBox.No)

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

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

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

通过调用`msg_box.setStandardButtons(QMessageBox.Yes | QMessageBox.No)`来设置消息框的标准按钮,然后使用`msg_box.button(QMessageBox.Yes)`和`msg_box.button(QMessageBox.No)`获取对应的按钮对象。接着可以使用`setText`方法来设置按钮文本为中文。

这样修改之后,当用户点击了"是"按钮时,会输出"用户点击了是按钮";当用户点击了"否"按钮时,会输出"用户点击了不是按钮"并退出程序。

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

清風 发表于 2024-2-24 20:41:41

FishC 发表于 2024-2-24 20:33
问题出在你设置按钮文本时的代码上。在调用`yes_button.setText('是')`和`no_button.setText('不是')`之前 ...

哈哈,报错:
msg_box.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
                               ^^^^^^^^^^^^^^^
AttributeError: type object 'QMessageBox' has no attribute 'Yes'

FishC 发表于 2024-2-24 20:42:05

清風 发表于 2024-02-24 20:41
哈哈,报错:
msg_box.setStandardButtons(QMessageB ...

非常抱歉,我的错误。在PyQt中,标准按钮没有直接的`Yes`和`No`属性,它们是通过`QMessageBox`类的静态方法来定义的。以下是修正后的代码:

# 创建一个消息框
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'{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.YesRole)
no_button = msg_box.addButton('不是', QMessageBox.NoRole)

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

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

现在你可以使用`QMessageBox.YesRole`和`QMessageBox.NoRole`来添加自定义按钮,然后通过比较`clicked_button`和`yes_button`来确定用户点击了是还是不是按钮,并打印相应的消息。当用户点击了不是按钮时,程序将退出。请注意,这里使用了`sys.exit()`来退出程序,确保你已经导入了`sys`模块。

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

清風 发表于 2024-2-24 20:44:22

FishC 发表于 2024-2-24 20:42
非常抱歉,我的错误。在PyQt中,标准按钮没有直接的`Yes`和`No`属性,它们是通过`QMessageBox`类的静态 ...

不行,还是只执行else语句....

FishC 发表于 2024-2-24 20:44:46

清風 发表于 2024-02-24 20:44
不行,还是只执行else语句....

非常抱歉,我之前的回答有误。在PyQt中,判断用户点击的按钮需要使用按钮的标准按钮角色,而不是直接比较按钮对象。以下是修正后的代码:

# 创建一个消息框
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'{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.YesRole)
no_button = msg_box.addButton('不是', QMessageBox.NoRole)

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

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

现在,我们通过`msg_box.clickedButton()`来获取用户点击的按钮对象,并与`yes_button`进行比较以确定用户点击的是哪个按钮。当用户点击"是"按钮时,会输出"用户点击了是按钮";当用户点击"不是"按钮时,会输出"用户点击了不是按钮"并退出程序。请确保你已经导入了`sys`模块。如果仍然无法正常工作,请提供完整的代码和报错信息,以便更好地帮助你解决问题。

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

清風 发表于 2024-2-24 21:02:00

FishC 发表于 2024-2-24 20:44
非常抱歉,我之前的回答有误。在PyQt中,判断用户点击的按钮需要使用按钮的标准按钮角色,而不是直接比 ...

再请问一下:如何禁止此BOX不能点右上角的X或者点ALT+ F4关闭?

FishC 发表于 2024-2-24 21:02:22

清風 发表于 2024-02-24 21:02
再请问一下:如何禁止此BOX不能点右上角的X关闭?

要禁止消息框的右上角关闭按钮(X按钮)的点击事件,可以使用`setWindowFlag`方法来设置`Qt.WindowCloseButtonHint`标志位。以下是相应的代码示例:# 创建一个消息框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'{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.YesRole)no_button = msg_box.addButton('不是', QMessageBox.NoRole)# 禁止关闭按钮msg_box.setWindowFlag(Qt.WindowCloseButtonHint, False)# 显示消息框,并获取用户点击的按钮msg_box.exec()# 做出相应的操作if msg_box.clickedButton() == yes_button:    print('用户点击了是按钮')else:    print('用户点击了不是按钮')    sys.exit()通过调用`setWindowFlag`方法并传递`Qt.WindowCloseButtonHint`常量和`False`来禁止关闭按钮。这样修改之后,用户将无法点击右上角的关闭按钮来关闭消息框。

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

清風 发表于 2024-2-24 21:03:12

FishC 发表于 2024-2-24 21:02
要禁止消息框的右上角关闭按钮(X按钮)的点击事件,可以使用`setWindowFlag`方法来设置`Qt.WindowClos ...

回复请注意python格式

FishC 发表于 2024-2-24 21:03:27

清風 发表于 2024-02-24 21:03
回复请注意python格式

非常抱歉,以下是使用Python语法的修正后代码示例:
# 创建一个消息框
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'{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.YesRole)
no_button = msg_box.addButton('不是', QMessageBox.NoRole)

# 禁止关闭按钮
msg_box.setWindowFlag(Qt.WindowCloseButtonHint, False)

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

# 做出相应的操作
if msg_box.clickedButton() == yes_button:
    print('用户点击了是按钮')
else:
    print('用户点击了不是按钮')
    sys.exit()
这样修改之后,用户将无法点击右上角的关闭按钮来关闭消息框。

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