我应该如何解决 Ubuntu 20.04 中的这个问题?
Invalid MIT-MAGIC-COOKIE-1 keyqt.qpa.xcb: could not connect to display :0
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, webgl, xcb.
Aborted (core dumped)
Invalid MIT-MAGIC-COOKIE-1 keyqt.qpa.xcb: could not connect to display :0
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, webgl, xcb.
Aborted (core dumped)
2224/31772MB(vision)
和
$ echo $DISPLAY
:0
和
$ python
Python 3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import inspect
>>> from PyQt5 import Qt
>>> vers = ['%s = %s' % (k,v) for k,v in vars(Qt).items() if k.lower().find('version') >= 0 and not inspect.isbuiltin(v)]
>>> print('\n'.join(sorted(vers)))
PYQT_VERSION = 331522
PYQT_VERSION_STR = 5.15.2
QOpenGLVersionProfile = <class 'PyQt5.QtGui.QOpenGLVersionProfile'>
QOperatingSystemVersion = <class 'PyQt5.QtCore.QOperatingSystemVersion'>
QT_VERSION = 331522
QT_VERSION_STR = 5.15.2
QVersionNumber = <class 'PyQt5.QtCore.QVersionNumber'>
作为重现此错误的最小代码,您可以运行以下命令:
$ cat test_qt.py
'''
====================
3D plots as subplots
====================
Demonstrate including 3D plots as subplots.
'''
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.axes3d import Axes3D, get_test_data
from matplotlib import cm
import numpy as np
# set up a figure twice as wide as it is tall
fig = plt.figure(figsize=plt.figaspect(0.5))
#===============
# First subplot
#===============
# set up the axes for the first plot
ax = fig.add_subplot(1, 2, 1, projection='3d')
# plot a 3D surface like in the example mplot3d/surface3d_demo
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
ax.set_zlim(-1.01, 1.01)
fig.colorbar(surf, shrink=0.5, aspect=10)
#===============
# Second subplot
#===============
# set up the axes for the second plot
ax = fig.add_subplot(1, 2, 2, projection='3d')
# plot a 3D wireframe like in the example mplot3d/wire3d_demo
X, Y, Z = get_test_data(0.05)
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)
plt.show()
$ python test_qt.py
Invalid MIT-MAGIC-COOKIE-1 keyqt.qpa.xcb: could not connect to display :0
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, webgl, xcb.
Aborted (core dumped)
还,
$ python
Python 3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from PyQt5 import QtCore, QtGui, QtWidgets
>>>
答案1
首先这样做:
$ export DISPLAY=:1.0
我安装的最新版本的 opencv-python 导致 pyqt5 出现问题。因此我卸载了它并安装了以下版本:
$ pip install opencv-python==4.3.0.36
添加以下信息以防对未来的读者有所帮助。
这是我的 pyqt 版本:
$ pip list|grep Qt
PyQt5 5.15.2
PyQt5-sip 12.8.1
$ python
Python 3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0] on linux
$ pip --version
pip 21.0 from /home/mona/venv/vision/lib/python3.8/site-packages/pip (python 3.8)
$ lsb_release -a
LSB Version: core-11.1.0ubuntu2-noarch:security-11.1.0ubuntu2-noarch
Distributor ID: Ubuntu
Description: Ubuntu 20.04.1 LTS
Release: 20.04
Codename: focal