Skip to content

PyQt

PyQt is a Python binding of the cross-platform GUI toolkit Qt, implemented as a Python plug-in. PyQt is free software developed by the British firm Riverbank Computing. It is available under similar terms to Qt versions older than 4.5; this means a variety of licenses including GNU General Public License (GPL) and commercial license, but not the GNU Lesser General Public License (LGPL). PyQt supports Microsoft Windows as well as various flavours of UNIX, including Linux and MacOS (or Darwin).

Festures

  • a substantial set of GUI widgets
  • classes for accessing SQL databases (ODBC, MySQL, PostgreSQL, Oracle, SQLite)
  • QScintilla, Scintilla-based rich text editor widget
  • data aware widgets that are automatically populated from a database
  • an XML parser
  • SVG support
  • classes for embedding ActiveX controls on Windows (only in commercial version)

How to install

$ pip install pyqt5

Hello World example

The below code shows a small window on the screen.

PyQt4

#! /usr/bin/env python3
# Character Encoding: UTF-8
#
# Here we provide the necessary imports.
# The basic GUI widgets are located in QtGui module. 
import sys
from PyQt4.QtGui import QApplication, QWidget

# Every PyQt4 application must create an application object.
# The application object is located in the QtGui module.
app = QApplication(sys.argv)

# The QWidget widget is the base class of all user interface objects in PyQt4.
# We provide the default constructor for QWidget. The default constructor has no parent.
# A widget with no parent is called a window. 
root = QWidget()

root.resize(320, 240)  # The resize() method resizes the widget.
root.setWindowTitle("Hello, World!")  # Here we set the title for our window.
root.show()  # The show() method displays the widget on the screen.

sys.exit(app.exec_())  # Finally, we enter the mainloop of the application.

PyQt5

#! /usr/bin/env python3
# Character Encoding: UTF-8
#
# Here we provide the necessary imports.
# The basic GUI widgets are located in QtWidgets module. 
import sys
from PyQt5.QtWidgets import QApplication, QWidget

# Every PyQt5 application must create an application object.
# The application object is located in the QtWidgets module.
app = QApplication(sys.argv)

# The QWidget widget is the base class of all user interface objects in PyQt5.
# We provide the default constructor for QWidget. The default constructor has no parent.
# A widget with no parent is called a window. 
root = QWidget()

root.resize(320, 240)  # The resize() method resizes the widget.
root.setWindowTitle("Hello, World!")  # Here we set the title for our window.
root.show()  # The show() method displays the widget on the screen.

sys.exit(app.exec_())  # Finally, we enter the mainloop of the application.

Notable applications that use PyQt

  • Anki, a spaced repetition flashcard program
  • Calibre, an E-book management application
  • Dropbox, a file hosting service
  • Eric Python IDE
  • fman, cross-platform file manager
  • Kodos, Python Regular Expression Debugger
  • Leo, an outliner and literate programming editor
  • Ninja-IDE, an extensible open-source Python IDE
  • OpenLP, an open-source lyrics projection program
  • OpenShot, a video editing program
  • Orange, a data mining and visualization framework
  • Puddletag, an open-source, cross-platform ID3 tag editor
  • QGIS, a free software desktop Geographic Information Systems (GIS) application
  • qt-recordMyDesktop, Qt4 frontend for recordMyDesktop
  • Spyder, a Python data science IDE
  • TortoiseHg, a graphical interface for the Mercurial source management program (Hg)
  • Veusz, a scientific plotting application

Troubleshooting

Could not find or load the Qt platform plugin

다음과 같은 에러가 발생할 수 있다.

This application failed to start because it could not find or load the Qt platform plugin "windows".

QT_QPA_PLATFORM_PLUGIN_PATH 환경 변수에 (파이썬3이 설치되어있는 경로)\Lib\site-packages\PyQt5\Qt\plugins\platforms 값을 입력한다.

no Qt platform plugin could be initialized

다음과 같은 에러가 발생할 수 있다.

2020-07-28 15:22:48.910 Python[54605:609794] ApplePersistenceIgnoreState: Existing state will not be touched. New state will be written to (null)
objc[54605]: Class QMacAutoReleasePoolTracker is implemented in both /Users/yourname/.pyenv/versions/3.7.7/envs/opy-yourname-3.7.7/lib/python3.7/site-packages/PyQt5/Qt/lib/QtCore.framework/Versions/5/QtCore (0x101af60f8) and /Users/yourname/.pyenv/versions/3.7.7/envs/opy-yourname-3.7.7/lib/python3.7/site-packages/cv2/.dylibs/QtCore (0x115474700). One of the two will be used. Which one is undefined.
objc[54605]: Class QT_ROOT_LEVEL_POOL__THESE_OBJECTS_WILL_BE_RELEASED_WHEN_QAPP_GOES_OUT_OF_SCOPE is implemented in both /Users/yourname/.pyenv/versions/3.7.7/envs/opy-yourname-3.7.7/lib/python3.7/site-packages/PyQt5/Qt/lib/QtCore.framework/Versions/5/QtCore (0x101af6170) and /Users/yourname/.pyenv/versions/3.7.7/envs/opy-yourname-3.7.7/lib/python3.7/site-packages/cv2/.dylibs/QtCore (0x115474778). One of the two will be used. Which one is undefined.
objc[54605]: Class KeyValueObserver is implemented in both /Users/yourname/.pyenv/versions/3.7.7/envs/opy-yourname-3.7.7/lib/python3.7/site-packages/PyQt5/Qt/lib/QtCore.framework/Versions/5/QtCore (0x101af6198) and /Users/yourname/.pyenv/versions/3.7.7/envs/opy-yourname-3.7.7/lib/python3.7/site-packages/cv2/.dylibs/QtCore (0x1154747a0). One of the two will be used. Which one is undefined.
objc[54605]: Class RunLoopModeTracker is implemented in both /Users/yourname/.pyenv/versions/3.7.7/envs/opy-yourname-3.7.7/lib/python3.7/site-packages/PyQt5/Qt/lib/QtCore.framework/Versions/5/QtCore (0x101af61e8) and /Users/yourname/.pyenv/versions/3.7.7/envs/opy-yourname-3.7.7/lib/python3.7/site-packages/cv2/.dylibs/QtCore (0x1154747f0). One of the two will be used. Which one is undefined.
[INFO   ] __init__:get_config:71 - Loading config file from: /Users/yourname/.labelmerc
[WARNING] __init__:update_dict:17 - Skipping unexpected key in config: instance_label_auto_increment
[WARNING] __init__:update_dict:17 - Skipping unexpected key in config: add_point
[WARNING] __init__:update_dict:17 - Skipping unexpected key in config: edit_line_color
[WARNING] __init__:update_dict:17 - Skipping unexpected key in config: edit_fill_color
QObject::moveToThread: Current thread (0x7f82bfc81990) is not the object's thread (0x7f82c24c8660).
Cannot move to target thread (0x7f82bfc81990)

You might be loading two sets of Qt binaries into the same process. Check that all plugins are compiled against the right Qt binaries. Export DYLD_PRINT_LIBRARIES=1 and check that only one set of binaries are being loaded.
qt.qpa.plugin: Could not load the Qt platform plugin "cocoa" in "/Users/yourname/.pyenv/versions/3.7.7/envs/opy-yourname-3.7.7/lib/python3.7/site-packages/cv2/qt/plugins" 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: cocoa, minimal, offscreen, webgl.

Abort trap: 6

플러그인 경로에 cv2가 있는걸로 봐서, 해당 라이브러리와 충돌하는듯 하다. 해당 라이브러리를 제거하면 된다.

$ pip uninstall opencv-python

Could not load the Qt platform plugin "xcb"

qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/usr/local/lib/python3.9/dist-packages/opencv_python-4.5.5.64-py3.9-linux-x86_64.egg/cv2/qt/plugins" 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: xcb, eglfs, linuxfb, minimal, minimalegl, offscreen, vnc.

opencv 쪽에서 관련 에러가 발생했다면 기존 패키지를 제거하고 headless 버전을 설치하자:

pip uninstall opencv-python
pip install opencv-python-headless

See also

Favorite site