Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PySide2:TypeError:';字节';对象不能解释为整数_Python_Pyqt_Pyside_Pyside2 - Fatal编程技术网

Python PySide2:TypeError:';字节';对象不能解释为整数

Python PySide2:TypeError:';字节';对象不能解释为整数,python,pyqt,pyside,pyside2,Python,Pyqt,Pyside,Pyside2,我有一个PySide2代码,它在Linux和Python2.7上工作 from PySide2.QtCore import QBuffer, QByteArray ... image = self.clipboard.image() ba = QByteArray() buffer = QBuffer(ba) buffer.open(QIODevice.WriteOnly) image.save(buffer, "PNG") return bytes(buffer.data()) 但在使用

我有一个PySide2代码,它在Linux和Python2.7上工作

from PySide2.QtCore import QBuffer, QByteArray

...

image = self.clipboard.image()
ba = QByteArray()
buffer = QBuffer(ba)
buffer.open(QIODevice.WriteOnly)
image.save(buffer, "PNG")
return bytes(buffer.data())
但在使用Python 3.6的windows上,它在这里失败:

  File "C:\....foo.py", line 93, in image_to_png
    return bytes(buffer.data())
例外情况:

TypeError: 'bytes' object cannot be interpreted as an integer
以字节形式获取PNG最简单的方法是什么

我希望避免创建临时文件,因为此上下文不需要它


(我使用PySide2,但如果您认为它更有意义,我可以切换到不同的python QT绑定。如果您这样认为,请留下评论)

这是一个解决方法

def image_to_png(image):
    temp_png = tempfile.mktemp('.png')
    image.save(temp_png)
    with io.open(temp_png, 'rb') as fd:
        content = fd.read()
    os.unlink(temp_png)
    return content

如果可能的话,我想避免临时文件。欢迎使用其他解决方案。

看起来像
QBuffer.data()
返回类型是
QByteArray
(可能无法由
bytes()
构造函数处理),但是
QByteArray.data()
返回类型是
bytes
。因此,我认为您应该尝试
返回buffer.data().data()
而不是
返回字节(buffer.data())

您只是将旧代码转换为在Python3上运行,还是需要同时在2.7和3.6上运行?在Python3.6上,buffer.data()的类型是什么?@PM2Ring到目前为止,相同的代码库在2.7和3.6上都可以运行。如果能保持这种状态(至少几个月),那就太好了。我认为在线文档在这种情况下是不正确的,因为它将
str
指示为
QByteArray.data()的返回类型: