【问题标题】:Why does PyQt5 QPixmap crash python? [duplicate]为什么 PyQt5 QPixmap 会导致 python 崩溃? [复制]
【发布时间】:2019-12-19 23:20:38
【问题描述】:

当我尝试将此字符串列表转换为像素图时,python 会崩溃。 有什么建议可以解决这个问题?

openIcon = [
    '16 13 5 1',
    '. c #040404',
    '# c #808304',
    'a c None',
    'b c #f3f704',
    'c c #f3f7f3',
    'aaaaaaaaa...aaaa',
    'aaaaaaaa.aaa.a.a',
    'aaaaaaaaaaaaa..a',
    'a...aaaaaaaa...a',
    '.bcb.......aaaaa',
    '.cbcbcbcbc.aaaaa',
    '.bcbcbcbcb.aaaaa',
    '.cbcb...........',
    '.bcb.#########.a',
    '.cb.#########.aa',
    '.b.#########.aaa',
    '..#########.aaaa',
    '...........aaaaa'
    ]

if __name__ == "__main__":
    from PyQt5.QtGui import QPixmap
    openIcon_p = QPixmap(openIcon)
    openIcon_p.save("openIcon.png")

使用:

Win32 上的 Python 3.7.4(tags/v3.7.4:e09359112e,2019 年 7 月 8 日,20:34:20)[MSC v.1916 64 位 (AMD64)]

PyQt5==5.13.0

【问题讨论】:

  • 更改为from PyQt5.QtGui import QGuiApplication, QPixmap app = QGuiApplication([]) openIcon_p = QPixmap(openIcon) openIcon_p.save("openIcon.png")

标签: python crash pyqt5 qpixmap


【解决方案1】:

从控制台运行代码以查看错误消息。 在QPixmap 之前需要一个QApplication

from PyQt5 import QtWidgets, QtGui
import sys

openIcon = [
    '16 13 5 1',
    '. c #040404',
    '# c #808304',
    'a c None',
    'b c #f3f704',
    'c c #f3f7f3',
    'aaaaaaaaa...aaaa',
    'aaaaaaaa.aaa.a.a',
    'aaaaaaaaaaaaa..a',
    'a...aaaaaaaa...a',
    '.bcb.......aaaaa',
    '.cbcbcbcbc.aaaaa',
    '.bcbcbcbcb.aaaaa',
    '.cbcb...........',
    '.bcb.#########.a',
    '.cb.#########.aa',
    '.b.#########.aaa',
    '..#########.aaaa',
    '...........aaaaa'
    ]

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv) 
    openIcon_p = QtGui.QPixmap(openIcon)
    openIcon_p.save("openIcon.png")

编辑:如果没有添加的行,代码会给出以下错误消息:

QPixmap::fromImageInPlace: QPixmap cannot be created without a QGuiApplication
QPixmap: Must construct a QGuiApplication before a QPixmap 

解释见Qt-Documentation。还有一个描述什么时候使用QtWidgets.QApplication和什么时候QtGui.QGuiApplication由eyllanesc评论

【讨论】:

  • 不需要QApplication,只需要QGuiApplication,不需要去掉sys.exit(app.exec_())
  • 奇怪 - 为什么我需要 QGuiApplication?为什么 Python 会崩溃?
  • 查看我的答案编辑
  • @eyllanesc:我遵循了错误消息,但没有识别 QGuiApplication :-)
  • @a_manthey_67 QGuiApplicaton 属于 QtGui
猜你喜欢
  • 2011-01-03
  • 2021-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多