Просмотр исходного кода

Use qtpy to abstract Qt python binding

Kentaro Wada 7 лет назад
Родитель
Сommit
c59d3f4eb3
10 измененных файлов с 34 добавлено и 76 удалено
  1. 7 14
      labelme/app.py
  2. 10 16
      labelme/canvas.py
  3. 1 4
      labelme/colorDialog.py
  4. 4 10
      labelme/labelDialog.py
  5. 3 8
      labelme/lib.py
  6. 1 4
      labelme/shape.py
  7. 2 6
      labelme/toolBar.py
  8. 3 8
      labelme/zoomWidget.py
  9. 1 0
      setup.py
  10. 2 6
      tests/test_labelDialog.py

+ 7 - 14
labelme/app.py

@@ -6,18 +6,11 @@ import re
 import subprocess
 import sys
 
-try:
-    from PyQt5 import QtCore
-    from PyQt5.QtCore import Qt
-    from PyQt5 import QtGui
-    from PyQt5 import QtWidgets
-    PYQT5 = True
-except ImportError:
-    from PyQt4 import QtCore
-    from PyQt4.QtCore import Qt
-    from PyQt4 import QtGui
-    from PyQt4 import QtGui as QtWidgets
-    PYQT5 = False
+from qtpy import PYQT5
+from qtpy import QtCore
+from qtpy.QtCore import Qt
+from qtpy import QtGui
+from qtpy import QtWidgets
 
 from labelme.canvas import Canvas
 from labelme.colorDialog import ColorDialog
@@ -979,7 +972,7 @@ class MainWindow(QtWidgets.QMainWindow, WindowMixin):
         filename = QtWidgets.QFileDialog.getOpenFileName(
             self, '%s - Choose Image or Label file' % __appname__,
             path, filters)
-        if PYQT5:
+        if qtpy.PYQT5:
             filename, _ = filename
         filename = str(filename)
         if filename:
@@ -1015,7 +1008,7 @@ class MainWindow(QtWidgets.QMainWindow, WindowMixin):
         filename = dlg.getSaveFileName(
             self, 'Choose File', default_labelfile_name,
             'Label files (*%s)' % LabelFile.suffix)
-        if PYQT5:
+        if qtpy.PYQT5:
             filename, _ = filename
         filename = str(filename)
         return filename

+ 10 - 16
labelme/canvas.py

@@ -2,16 +2,10 @@ from __future__ import print_function
 
 import sys
 
-try:
-    from PyQt5 import QtCore
-    from PyQt5 import QtGui
-    from PyQt5 import QtWidgets
-    PYQT5 = True
-except ImportError:
-    from PyQt4 import QtCore
-    from PyQt4 import QtGui
-    from PyQt4 import QtGui as QtWidgets
-    PYQT5 = False
+from qtpy import PYQT5
+from qtpy import QtCore
+from qtpy import QtGui
+from qtpy import QtWidgets
 
 from labelme.lib import distance
 from labelme.shape import Shape
@@ -29,12 +23,12 @@ CURSOR_GRAB = QtCore.Qt.OpenHandCursor
 
 
 class Canvas(QtWidgets.QWidget):
-    zoomRequest = QtCore.pyqtSignal(int, QtCore.QPoint)
-    scrollRequest = QtCore.pyqtSignal(int, int)
-    newShape = QtCore.pyqtSignal()
-    selectionChanged = QtCore.pyqtSignal(bool)
-    shapeMoved = QtCore.pyqtSignal()
-    drawingPolygon = QtCore.pyqtSignal(bool)
+    zoomRequest = QtCore.Signal(int, QtCore.QPoint)
+    scrollRequest = QtCore.Signal(int, int)
+    newShape = QtCore.Signal()
+    selectionChanged = QtCore.Signal(bool)
+    shapeMoved = QtCore.Signal()
+    drawingPolygon = QtCore.Signal(bool)
 
     CREATE, EDIT = 0, 1
 

+ 1 - 4
labelme/colorDialog.py

@@ -1,7 +1,4 @@
-try:
-    from PyQt5 import QtWidgets
-except ImportError:
-    from PyQt4 import QtGui as QtWidgets
+from qtpy import QtWidgets
 
 
 class ColorDialog(QtWidgets.QColorDialog):

+ 4 - 10
labelme/labelDialog.py

@@ -1,13 +1,7 @@
-try:
-    from PyQt5 import QtCore
-    from PyQt5 import QtGui
-    from PyQt5 import QtWidgets
-    PYQT5 = True
-except ImportError:
-    from PyQt4 import QtCore
-    from PyQt4 import QtGui
-    from PyQt4 import QtGui as QtWidgets
-    PYQT5 = False
+from qtpy import PYQT5
+from qtpy import QtCore
+from qtpy import QtGui
+from qtpy import QtWidgets
 
 from .lib import labelValidator
 from .lib import newIcon

+ 3 - 8
labelme/lib.py

@@ -1,14 +1,9 @@
 from math import sqrt
 import os.path as osp
 
-try:
-    from PyQt5 import QtCore
-    from PyQt5 import QtGui
-    from PyQt5 import QtWidgets
-except ImportError:
-    from PyQt4 import QtCore
-    from PyQt4 import QtGui
-    from PyQt4 import QtGui as QtWidgets
+from qtpy import QtCore
+from qtpy import QtGui
+from qtpy import QtWidgets
 
 
 here = osp.dirname(osp.abspath(__file__))

+ 1 - 4
labelme/shape.py

@@ -1,7 +1,4 @@
-try:
-    from PyQt5 import QtGui
-except ImportError:
-    from PyQt4 import QtGui
+from qtpy import QtGui
 
 from .lib import distance
 

+ 2 - 6
labelme/toolBar.py

@@ -1,9 +1,5 @@
-try:
-    from PyQt5 import QtCore
-    from PyQt5 import QtWidgets
-except ImportError:
-    from PyQt4 import QtCore
-    from PyQt4 import QtGui as QtWidgets
+from qtpy import QtCore
+from qtpy import QtWidgets
 
 
 class ToolBar(QtWidgets.QToolBar):

+ 3 - 8
labelme/zoomWidget.py

@@ -1,11 +1,6 @@
-try:
-    from PyQt5 import QtCore
-    from PyQt5 import QtGui
-    from PyQt5 import QtWidgets
-except ImportError:
-    from PyQt4 import QtCore
-    from PyQt4 import QtGui
-    from PyQt4 import QtGui as QtWidgets
+from qtpy import QtCore
+from qtpy import QtGui
+from qtpy import QtWidgets
 
 
 class ZoomWidget(QtWidgets.QSpinBox):

+ 1 - 0
setup.py

@@ -18,6 +18,7 @@ install_requires = [
     'numpy',
     'Pillow>=2.8.0',
     'PyYAML',
+    'qtpy',
 ]
 
 

+ 2 - 6
tests/test_labelDialog.py

@@ -1,9 +1,5 @@
-try:
-    from PyQt5 import QtCore
-    from PyQt5 import QtWidgets
-except ImportError:
-    from PyQt4 import QtCore
-    from PyQt4 import QtGui as QtWidgets
+from qtpy import QtCore
+from qtpy import QtWidgets
 
 from labelme import labelDialog