1. <legend id='ixBcV'><style id='ixBcV'><dir id='ixBcV'><q id='ixBcV'></q></dir></style></legend>

    <i id='ixBcV'><tr id='ixBcV'><dt id='ixBcV'><q id='ixBcV'><span id='ixBcV'><b id='ixBcV'><form id='ixBcV'><ins id='ixBcV'></ins><ul id='ixBcV'></ul><sub id='ixBcV'></sub></form><legend id='ixBcV'></legend><bdo id='ixBcV'><pre id='ixBcV'><center id='ixBcV'></center></pre></bdo></b><th id='ixBcV'></th></span></q></dt></tr></i><div id='ixBcV'><tfoot id='ixBcV'></tfoot><dl id='ixBcV'><fieldset id='ixBcV'></fieldset></dl></div>

    <small id='ixBcV'></small><noframes id='ixBcV'>

      <tfoot id='ixBcV'></tfoot>
      • <bdo id='ixBcV'></bdo><ul id='ixBcV'></ul>

      “QMessageBox.Yes"的替代方案对于 PyQt6

      时间:2023-08-05
        <tbody id='aR0XC'></tbody>
      <legend id='aR0XC'><style id='aR0XC'><dir id='aR0XC'><q id='aR0XC'></q></dir></style></legend>

          <small id='aR0XC'></small><noframes id='aR0XC'>

          <i id='aR0XC'><tr id='aR0XC'><dt id='aR0XC'><q id='aR0XC'><span id='aR0XC'><b id='aR0XC'><form id='aR0XC'><ins id='aR0XC'></ins><ul id='aR0XC'></ul><sub id='aR0XC'></sub></form><legend id='aR0XC'></legend><bdo id='aR0XC'><pre id='aR0XC'><center id='aR0XC'></center></pre></bdo></b><th id='aR0XC'></th></span></q></dt></tr></i><div id='aR0XC'><tfoot id='aR0XC'></tfoot><dl id='aR0XC'><fieldset id='aR0XC'></fieldset></dl></div>

              <tfoot id='aR0XC'></tfoot>
                <bdo id='aR0XC'></bdo><ul id='aR0XC'></ul>

              • 本文介绍了“QMessageBox.Yes"的替代方案对于 PyQt6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在尝试将我的脚本从 PyQt5 移植到 PyQt6.感谢this answer,我已经弄清楚了如何移植大部分内容,但是,我遇到了一个问题.

                I am trying to port over a script of mine from PyQt5 to PyQt6. I have figured out how to port most of the things thanks to this answer, however, I have run into an issue.

                我发现 PyQt6 使用 QtWidgets.QMessageBox.StandardButtons.Yes 而不是 PyQt5 的 QtWidgets.QMessageBox.Yes.

                I have figured out that PyQt6 uses QtWidgets.QMessageBox.StandardButtons.Yes instead of PyQt5's QtWidgets.QMessageBox.Yes.

                但是,当检查用户是否按下是"时打开 QMessageBox 后,将 QtWidgets.QMessageBox.Yes 替换为 QtWidgets.QMessageBox.StandardButtons.Yes 不起作用(请查看下面的示例).

                However, when checking if the user pressed "Yes" after a QMessageBox opens, replacing QtWidgets.QMessageBox.Yes with QtWidgets.QMessageBox.StandardButtons.Yes doesn't work (check the examples below).

                例子:

                PyQt5:

                reply = QtWidgets.QMessageBox()
                reply.setText("Some random text.")
                reply.setStandardButtons(QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
                
                x = reply.exec_()
                
                if x == QtWidgets.QMessageBox.Yes:
                    print("Hello!")
                

                打印你好!"这里工作正常.(16384 == 16384)

                Printing "Hello!" here works normally. (16384 == 16384)

                PyQt6:

                reply = QtWidgets.QMessageBox()
                reply.setText("Some random text.")
                reply.setStandardButtons(QtWidgets.QMessageBox.StandardButtons.Yes | 
                                         QtWidgets.QMessageBox.StandardButtons.No)
                
                x = reply.exec()
                
                if x == QtWidgets.QMessageBox.StandardButtons.Yes:
                    print("Hello!")
                

                你好!"这里根本不打印.(16384 != StandardButtons.yes)

                "Hello!" here doesn't print at all. (16384 != StandardButtons.yes)

                我知道我可以做到:

                x = reply.exec()
                
                if x == 16384:
                    print("Hello!")
                

                因为,在按下是"后,QMessageBox 等于 16384 (看这个),但我想使用这种方法,而是使用类似 PyQt5 示例的方法.

                because, after pressing "Yes", the QMessageBox equals to 16384 (see this), but I'd like to not use that approach, and rather use something like the PyQt5 example.

                推荐答案

                这有点奇怪.根据 QMessageBox.exec 的文档:

                This is kind of strange. According to the documentation for QMessageBox.exec:

                当使用带有标准按钮的 QMessageBox 时,此函数返回一个 StandardButton 值,指示之前的标准按钮点击.

                When using a QMessageBox with standard buttons, this function returns a StandardButton value indicating the standard button that was clicked.

                您使用的是标准按钮,所以这应该返回一个 QMessageBox.StandardButtons 枚举.

                You are using standard buttons, so this should return a QMessageBox.StandardButtons enum.

                值得一提的是,在 PyQt5 中比较整数和枚举不是问题,因为枚举是用 enum.IntEnum 实现的.现在,它们是用 enum.Enum 实现的.来自 Riverbank Computing 网站:

                It's also worth mentioning that comparing integers with enums was not a problem in PyQt5, because enums were implemented with enum.IntEnum. Now, they're implemented with enum.Enum. From the Riverbank Computing website:

                现在所有枚举都实现为 enum.Enum(PyQt5 使用 enum.IntEnum范围枚举和传统命名枚举的自定义类型).PyQt5每当需要枚举时都允许使用 int 但 PyQt6 需要正确的类型.

                All enums are now implemented as enum.Enum (PyQt5 used enum.IntEnum for scoped enums and a custom type for traditional named enums). PyQt5 allowed an int whenever an enum was expected but PyQt6 requires the correct type.

                但是,出于某种原因,QMessageBox.exec 返回一个整数(我只是用 PyQt6==6.0.0 尝试过)!

                However, for some reason, QMessageBox.exec returns an integer (I just tried it with PyQt6==6.0.0)!

                现在,您可以通过故意从返回的整数构造一个枚举对象来解决这个问题:

                For now, you can get around this by deliberately constructing an enum object from the returned integer:

                if QtWidgets.QMessageBox.StandardButtons(x) == QtWidgets.QMessageBox.StandardButtons.Yes:
                            print("Hello!")
                

                而且,由于您是在比较枚举,我建议您使用 is 而不是 ==.

                And, since you're comparing enums, I would suggest using is rather than ==.

                这篇关于“QMessageBox.Yes"的替代方案对于 PyQt6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Python PyQt5:如何使用 PyQt5 显示错误消息 下一篇:如何在没有 QProcess 的情况下将终端嵌入 PyQt5 应用

                相关文章

                最新文章

                  <bdo id='klHxt'></bdo><ul id='klHxt'></ul>

                <small id='klHxt'></small><noframes id='klHxt'>

                <tfoot id='klHxt'></tfoot><legend id='klHxt'><style id='klHxt'><dir id='klHxt'><q id='klHxt'></q></dir></style></legend>
              • <i id='klHxt'><tr id='klHxt'><dt id='klHxt'><q id='klHxt'><span id='klHxt'><b id='klHxt'><form id='klHxt'><ins id='klHxt'></ins><ul id='klHxt'></ul><sub id='klHxt'></sub></form><legend id='klHxt'></legend><bdo id='klHxt'><pre id='klHxt'><center id='klHxt'></center></pre></bdo></b><th id='klHxt'></th></span></q></dt></tr></i><div id='klHxt'><tfoot id='klHxt'></tfoot><dl id='klHxt'><fieldset id='klHxt'></fieldset></dl></div>