如何强制 PyQt5 用于 QObject 类? [英] How to force PyQt5 use for QObject class?

查看:86
本文介绍了如何强制 PyQt5 用于 QObject 类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Python 3 和 PyQt5 开发一个小型图形应用程序.在我使用的第一台计算机上,只安装了 PyQt5,我的代码中的一切都很好.但是,当我想在安装了 PyQt4 和 PyQt5 的另一台笔记本电脑上运行我的代码时,出现以下错误:

I'm developping a small graphic application using Python 3 and PyQt5. On the first computer I use, where only PyQt5 is installed, everything in my code is fine. But when I want to run my code on my other laptop, where both PyQt4 and PyQt5 are installed, I get the following error:

RuntimeError: the PyQt5.QtCore and PyQt4.QtCore modules both wrap the QObject class

Python 解释器在从主文件调用的ViewWindow.py"文件中定位错误.

Python interpreter locates the error in the file "ViewWindow.py", called from the main file.

由于我在这台笔记本电脑上同时安装了 PyQt4 和 PyQt5,而且我无法卸载 PyQt4(这太容易了...),我想知道是否可以强制使用 PyQt5.QtCore 或其他东西避免这个问题.我在这台笔记本电脑上的配置:Debian 8、Python3.4、PyQt4 和 5(没有特殊配置,从 Debian 存储库安装),IDE = Spyder.

As I have both PyQt4 and PyQt5 on this laptop, and because I can't uninstall PyQt4 (it would be too easy...), I wonder if it's possible to force use of PyQt5.QtCore, or something else to avoid this problem. My configuration on this laptop: Debian 8, Python3.4, PyQt4 and 5 (without special configuration, installed from Debian repos), IDE = Spyder.

我把我的文件 main.pyViewWindow.py 的第一行放在那里.

I put there first lines of my files main.py and ViewWindow.py.

# main.py
import sys
import sqlite3
import ViewWindow
from DataWindow import DataWindow
from PyQt5.QtCore import QObject # I tried adding this line, but nothing changed...
from PyQt5.QtWidgets import (QApplication,
                         QWidget,
                         QGridLayout,
                         QHBoxLayout,
                         QLabel,
                         QLineEdit,
                         QPushButton,
                         QTextEdit,
                         QVBoxLayout
                         )


class MainWindow(QWidget):
    # Some cool stuff


# ViewWindow.py
import sys
import sqlite3
from PyQt5.QtCore import QObject # same thing than above, adding this line doesn't change the output.
from PyQt5.QtWidgets import (QApplication,
                         QWidget,
                         QGridLayout,
                         QLabel,
                         QPushButton,
                         QVBoxLayout
                         )


class ViewWindow(QWidget):

有人知道如何运行这段代码吗?

Does someone knows how to make this code run ?

谢谢,

杰瑞 M.

我试图运行那个强制使用 Python3 的脚本,它奏效了……问题似乎来自 iPython3.感谢您的帮助.

I tried to run that script forcing use of Python3, and it worked... It seems that problem comes from iPython3. Thanks for your help.

推荐答案

A RuntimeError with message

A RuntimeError with message

PyQt5.QtCorePyQt4.QtCore 模块都封装了 QObject

the PyQt5.QtCore and PyQt4.QtCore modules both wrap the QObject class

在您尝试导入 PyQt5.QtCore 时引发,而 PyQt4.QtCore 之前已经导入.

is raised the moment you try to import PyQt5.QtCore while PyQt4.QtCore was already imported before.

此错误是在用于连接 Qt 的 SIP 中引发的.就像它所说的那样,只允许一个模块声称包装 QObject.因此,错误只是告诉您,您同时使用 PyQt4 和 PyQt5.

This error is raised within SIP, which is used to connect to Qt. Like it states, it's only allowed to have one module claiming to wrap QObject. Thus the error just tells you, that you're using PyQt4 and PyQt5 at once.

所以你需要找到加载PyQt4的模块来配置它使用PyQt5.或者,您可以尝试将 from PyQt5.QtCore import QObject 放在任何其他导入之前,并希望通常从 PyQt4 导入的模块具有适应性并且能够使用 PyQt5 作为后备.

So you need to find the module loading PyQt4 to configure it to use PyQt5 instead. Alternatively you could try to put from PyQt5.QtCore import QObject before any other import and hope, that the module, which usually imports from PyQt4, is adaptable and able to use PyQt5 as fallback.

这篇关于如何强制 PyQt5 用于 QObject 类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆