鱼C论坛

 找回密码
 立即注册
查看: 327|回复: 9

QMessageBox问题!

[复制链接]
发表于 2024-2-24 20:33:29 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
有如下代码:

# 创建一个消息框
        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命令,显示用户选择了不是按钮?  并且程序也不退出。。。

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-2-24 20:33:55 | 显示全部楼层
问题出在你设置按钮文本时的代码上。在调用`yes_button.setText('是')`和`no_button.setText('不是')`之前,你需要先调用`msg_box.setStandardButtons(QMessageBox.Yes | QMessageBox.No)`来设置消息框的标准按钮。

修改后的代码如下:

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

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

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

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

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

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

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

  29. # 做出相应的操作
  30. if clicked_button == QMessageBox.Yes:
  31.     print('用户点击了是按钮')
  32. else:
  33.     print('用户点击了不是按钮')
  34.     sys.exit()
复制代码


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

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

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 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'
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-24 20:42:05 | 显示全部楼层
清風 发表于 2024-02-24 20:41
哈哈,报错:
msg_box.setStandardButtons(QMessageB ...


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

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

  3. # 设置标题的字体和颜色
  4. title_font = QFont('Arial', 12, QFont.Weight.Bold)
  5. title_color = QColor(255, 0, 0)  # 红色

  6. msg_box.setWindowTitle('123')
  7. msg_box.setStyleSheet(f"QLabel#qt_msgbox_label{{ color: {title_color.name()}; }}")
  8. msg_box.setText(f'{msg_box.text()}')
  9. msg_box.setFont(title_font)

  10. # 设置文本的字体和颜色
  11. text_font = QFont('Arial', 10)
  12. text_color = QColor(0, 0, 255)  # 蓝色

  13. msg_box.setText('234~~')
  14. msg_box.setStyleSheet(f"QLabel#qt_msgbox_label{{color: {text_color.name()};}}")
  15. msg_box.setText(f'{msg_box.text()}')
  16. msg_box.setFont(text_font)

  17. # 添加自定义按钮
  18. yes_button = msg_box.addButton('是', QMessageBox.YesRole)
  19. no_button = msg_box.addButton('不是', QMessageBox.NoRole)

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

  22. # 做出相应的操作
  23. if clicked_button == yes_button:
  24.     print('用户点击了是按钮')
  25. else:
  26.     print('用户点击了不是按钮')
  27.     sys.exit()
复制代码


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

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

不行,还是只执行else语句....
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-24 20:44:46 | 显示全部楼层
清風 发表于 2024-02-24 20:44
不行,还是只执行else语句....


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

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

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

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

  17. # 添加自定义按钮
  18. yes_button = msg_box.addButton('是', QMessageBox.YesRole)
  19. no_button = msg_box.addButton('不是', QMessageBox.NoRole)

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

  22. # 做出相应的操作
  23. if msg_box.clickedButton() == yes_button:
  24.     print('用户点击了是按钮')
  25. else:
  26.     print('用户点击了不是按钮')
  27.     sys.exit()
复制代码


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

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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


再请问一下:如何禁止此BOX不能点右上角的X或者点ALT+ F4关闭?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 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 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

回复请注意python格式
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-24 21:03:27 | 显示全部楼层
清風 发表于 2024-02-24 21:03
回复请注意python格式


非常抱歉,以下是使用Python语法的修正后代码示例:
  1. # 创建一个消息框
  2. msg_box = QMessageBox()

  3. # 设置标题的字体和颜色
  4. title_font = QFont('Arial', 12, QFont.Weight.Bold)
  5. title_color = QColor(255, 0, 0)  # 红色

  6. msg_box.setWindowTitle('123')
  7. msg_box.setStyleSheet(f"QLabel#qt_msgbox_label{{ color: {title_color.name()}; }}")
  8. msg_box.setText(f'{msg_box.text()}')
  9. msg_box.setFont(title_font)

  10. # 设置文本的字体和颜色
  11. text_font = QFont('Arial', 10)
  12. text_color = QColor(0, 0, 255)  # 蓝色

  13. msg_box.setText('234~~')
  14. msg_box.setStyleSheet(f"QLabel#qt_msgbox_label{{color: {text_color.name()};}}")
  15. msg_box.setText(f'{msg_box.text()}')
  16. msg_box.setFont(text_font)

  17. # 添加自定义按钮
  18. yes_button = msg_box.addButton('是', QMessageBox.YesRole)
  19. no_button = msg_box.addButton('不是', QMessageBox.NoRole)

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

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

  24. # 做出相应的操作
  25. if msg_box.clickedButton() == yes_button:
  26.     print('用户点击了是按钮')
  27. else:
  28.     print('用户点击了不是按钮')
  29.     sys.exit()
复制代码

这样修改之后,用户将无法点击右上角的关闭按钮来关闭消息框。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 19:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表