Qt for Python可以快速跨平台的GUI应用。这篇文章分享下如何结合Dynamsoft Barcode Reader SDK来创建一个简单的读码应用。
安装Qt for Python
官方站点可以下载对应的wheel文件,或者通过命令行安装:
pip install pyside2
我之前用Python 3.6无法成功加载Qt:
Error:
Traceback (most recent call last):
File "test2.py", line 3, in <module>
from PySide2.QtWidgets import (QApplication, QLabel, QPushButton, QVBoxLayout, QWidget)
ImportError: DLL load failed: The specified procedure could not be found.
换成Python 3.7之后就好了。
安装Dynamsoft Barcode Reader
-
下载barcode sdk。
-
申请一个免费试用的license。
-
获取extension源码,然后编译安装:
python setup.py build install
简单的Windows桌面扫码应用
Qt安装之后可以用Qt designer来设计界面并生成Python代码。
运行Python37\Lib\site-packages\PySide2\designer.exe
。
设计好界面之后保存到.ui文件。
用Python37\Scripts\pyside2-uic.exe
把.ui文件转换成Python文件:
pyside2-uic -x *.ui -o ui.py
之后可以直接运行这个Python文件。
应用界面很简单。Button用于加载图片,Label用于显示图片,Text用于显示结果。
import sys
from PySide2.QtGui import QPixmap
from PySide2.QtWidgets import QApplication, QLabel, QPushButton, QVBoxLayout, QWidget, QFileDialog, QTextEdit, QSizePolicy
from PySide2.QtCore import Slot, Qt, QStringListModel, QSize
import dbr
import os
class UI_Window(QWidget):
def __init__(self