我正在尝试将我的脚本从 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模板网!