問題描述
我正在嘗試將我的腳本從 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模板網!