- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 PyQt4 编写了一个小型 GUI,它显示图像并获取用户单击的点坐标。我需要将 2D numpy 数组显示为灰度,因此我从该数组创建一个 QImage,然后从该数组创建一个 QPixmap。在 Python 2 中它工作得很好。
但是,当我迁移到 Python 3 时,它无法决定 QImage 的构造函数 - 它给了我以下错误:
TypeError: arguments did not match any overloaded call:
QImage(): too many arguments
QImage(QSize, QImage.Format): argument 1 has unexpected type 'numpy.ndarray'
QImage(int, int, QImage.Format): argument 1 has unexpected type 'numpy.ndarray'
QImage(str, int, int, QImage.Format): argument 1 has unexpected type 'numpy.ndarray'
QImage(sip.voidptr, int, int, QImage.Format): argument 1 has unexpected type 'numpy.ndarray'
QImage(str, int, int, int, QImage.Format): argument 1 has unexpected type 'numpy.ndarray'
QImage(sip.voidptr, int, int, int, QImage.Format): argument 1 has unexpected type 'numpy.ndarray'
QImage(list-of-str): argument 1 has unexpected type 'numpy.ndarray'
QImage(str, str format=None): argument 1 has unexpected type 'numpy.ndarray'
QImage(QImage): argument 1 has unexpected type 'numpy.ndarray'
QImage(object): too many arguments
据我所知,我之前调用的 QImage 构造函数是其中之一:
QImage(str, int, int, QImage.Format)
QImage(sip.voidptr, int, int, QImage.Format)
我假设 numpy 数组适合其中之一所需的协议(protocol)之一。我认为这可能与数组和 View 有关,但我尝试过的所有变体要么产生上述错误,要么只是使 GUI 退出而不执行任何操作。 如何在 Python 3 中重现 Python 2 的行为?
以下是一个小示例,其中相同的代码在 Python 2 下运行良好,但在 Python 3 下运行不佳:
from __future__ import (print_function, division)
from PyQt4 import QtGui, QtCore
import numpy as np
class MouseView(QtGui.QGraphicsView):
mouseclick = QtCore.pyqtSignal(tuple)
def __init__(self, scene, parent=None):
super(MouseView, self).__init__(scene, parent=parent)
def mousePressEvent(self, event):
self.mouseclick.emit((event.x(),
self.scene().sceneRect().height() - event.y()))
class SimplePicker(QtGui.QDialog):
def __init__(self, data, parent=None):
super(SimplePicker, self).__init__(parent)
mind = data.min()
maxd = data.max()
bdata = ((data - mind) / (maxd - mind) * 255.).astype(np.uint8)
wdims = data.shape
wid = wdims[0]
hgt = wdims[1]
# This is the line of interest - it works fine under Python 2, but not Python 3
img = QtGui.QImage(bdata.T, wid, hgt,
QtGui.QImage.Format_Indexed8)
self.scene = QtGui.QGraphicsScene(0, 0, wid, hgt)
self.px = self.scene.addPixmap(QtGui.QPixmap.fromImage(img))
view = MouseView(self.scene)
view.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
view.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
view.setSizePolicy(QtGui.QSizePolicy.Fixed,
QtGui.QSizePolicy.Fixed)
view.setMinimumSize(wid, hgt)
view.mouseclick.connect(self.click_point)
quitb = QtGui.QPushButton("Done")
quitb.clicked.connect(self.quit)
lay = QtGui.QVBoxLayout()
lay.addWidget(view)
lay.addWidget(quitb)
self.setLayout(lay)
self.points = []
def click_point(self, xy):
self.points.append(xy)
def quit(self):
self.accept()
def test_picker():
x, y = np.mgrid[0:100, 0:100]
img = x * y
app = QtGui.QApplication.instance()
if app is None:
app = QtGui.QApplication(['python'])
picker = SimplePicker(img)
picker.show()
app.exec_()
print(picker.points)
if __name__ == "__main__":
test_picker()
我在 Windows 7 64 位、Qt 4.8.7、PyQt 4.10.4、numpy 1.9.2 上使用 Anaconda 安装。
最佳答案
在上面的 PyQt 构造函数中,从名为 bdata
的 Numpy 数组中观察到以下行为:
bdata
在 Python 2 和 Python 3 上都能正常工作bdata.T
适用于 2,不适用于 3(构造函数错误)bdata.T.copy()
适用于两者bdata[::-1,:]
不适用于 2 或 3(相同的错误)bdata[::-1,:].copy()
适用于两者bdata[::-1,:].base
对两者都有效,但会丢失反向操作的结果正如 @ekhumoro 在评论中提到的,您需要支持 Python buffer protocol 的东西。这里实际感兴趣的 Qt 构造函数是 this QImage 构造函数,或其 const 版本:
QImage(uchar * data, int width, int height, Format format)
来自 PyQt 4.10.4 文档保留 here ,PyQt 对 unsigned char *
的期望在 Python 2 和 3 中是不同的:
Python 2:
If Qt expects a
char *
,signed char *
or anunsigned char *
(or a const version) then PyQt4 will accept a unicode or QString that contains only ASCII characters, a str, a QByteArray, or a Python object that implements the buffer protocol.
Python 3:
If Qt expects a
signed char *
or anunsigned char *
(or a const version) then PyQt4 will accept a bytes.
Numpy 数组 满足这两个要求,但显然 Numpy View 也不满足。实际上令人费解的是 bdata.T
在 Python 2 中竟然能工作,因为它据称返回一个 View :
>>> a = np.ones((2,3))
>>> b = a.T
>>> b.base is a
True
最终答案:如果您需要进行导致 View 的转换,则可以通过将结果copy()
到新数组来避免错误用于传递到构造函数中。这可能不是最好的答案,但它会起作用。
关于python - 在 Python 3 中重现 Python 2 PyQt4 QImage 构造函数行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33445109/
我现在已经用 PyQt 做了几个项目,而且我越来越熟悉 Qt 采用的模型/ View 思想流派。我已经将它用于列表和表格 View 之类的东西,它们背后有一个自定义模型来显示和操作数据。我使用委托(d
import sys from PyQt4.QtCore import * from PyQt4.QtGui import * class MainWindow(QMainWindow):
使用下面的示例代码(受 here 的严重影响),右键单击上下文菜单并没有真正正确对齐。 从屏幕截图中可以看出,生成的菜单在鼠标光标上方相当多的位置。我希望菜单的左上角与鼠标指针完全对齐。 有没有办法对
所以我创建了一个自定义上下文菜单,但我想根据某些值将树小部件的某些行中的某些项目灰显。如何禁用菜单上的项目? myUI.setContextMenuPolicy( Qt.CustomContextMe
是否可以禁用 QComboBox在 pyqt 中,就像我们可以在 Win Forms(C#) 中那样做,因为我在 QComboBox 中找不到任何选项手动的。我想启用 QcomboBox仅当管理员登录
我想将 QComboBox 与元组中的“键”和“值”一起使用,该元组类似于 django 模型中使用的元组。例如,我对一个人的性别有以下结构。 SEX_CHOICES = (('M', 'Male')
是否可以让 Altair 或 Vega(-Lite) 渲染到 PyQt 小部件,类似于支持多个后端的 Matplotlib?我知道我可以使用 Qt WebView 小部件来呈现带有 Vega 嵌入的网
在下面的示例代码中(受 here 的影响很大),我希望选择单击单元格的整行而不是单个单元格。如何更改代码以合并它? import re import operator import os import
我正在尝试禁用关闭“x”按钮,并且我认为通过将 DockWidgetFeature 设置为仅可移动和可 float 即可工作。 def CreateDockWidget (self): Pan
我已经按照 Yasin Uludag 的一些有用的在线教程来尝试使用 PyQt(或者更确切地说是 PySide)来创建一个简单的 TreeView ,但是我在使用工具提示时遇到了问题。在以下代码中,工
我正在尝试创建一个场景,我需要从 mousePressEvent 位置画线到最新的鼠标 moveposition 这意味着我需要调用 paintEvent 来自 mousePressEvent ,这可
是Python 3的组合和 PyQt 4受到推崇的?有没有其他选择? 最佳答案 我不明白为什么不,有一个 version available对于正常工作的 Python 3,如果你真的需要 Qt,唯一
我正在尝试显示从二进制文件中读取的图像数据(我编写了用于从文件中检索此数据并将其存储为图像以供 QImage() 使用的代码)。我想做的是将 slider 连接到图形 View 小部件,以便当您移动
我已经准备了很多关于如何在 python 和 pyqt 中将多个信号连接到同一个事件处理程序的帖子。例如,将多个按钮或组合框连接到同一功能。 许多示例展示了如何使用 QSignalMapper 执行此
我有一个 PyQt 主窗口,当用户按下某个按钮时,我需要从中获取一串用户输入。 这是我的用户输入窗口代码: class InputDialog(QtGui.QDialog): ''' t
编辑: 以下 buildout.cfg 用于构建 Qt、PyQt 和 SIP [buildout] parts = pyqt [pyqt] recipe = zc.recipe.cmmi ur
我目前正在开发一个应用程序,该应用程序可以使用 PyQt 访问 sqlalchemy 数据库并将其内容显示到 TableView 或其他一些小部件中。现在为了简单起见,我们只说这是一个电话簿,上面有姓
使用我现在拥有的代码,我可以成功地播放文件中的 .mp3 数据。但是我需要使用 QtCore.QBuffer(不是来自文件)播放相同的数据。当我使用文档的示例时,它会出现意外类型的 QBuffer 错
安装 sip 后,我在尝试安装 PyQt-x11-gpl-4.11 时不断收到这个可爱的错误消息。 mycommandline$ python configure-ng.py --verbose Qu
我正在为一个项目使用 PyQt。但并非突然间我收到一个错误: QPixmap: It is not safe to use pixmaps outside the GUI thread in PyQt
我是一名优秀的程序员,十分优秀!