当前位置:
首页 >
qt如何创建桌面快捷方式_如何用Qt Python创建简单的桌面条形码应用
发布时间:2025/3/15
52
豆豆
生活随笔
收集整理的这篇文章主要介绍了
qt如何创建桌面快捷方式_如何用Qt Python创建简单的桌面条形码应用
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
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源码,然后编译安装:
简单的Windows桌面扫码应用
Qt安装之后可以用Qt designer来设计界面并生成Python代码。
运行Python37Libsite-packagesPySide2designer.exe。
设计好界面之后保存到.ui文件。
用Python37Scriptspyside2-uic.exe把.ui文件转换成Python文件:
pyside2-uic -x *.ui -o ui.py之后可以直接运行这个Python文件。
应用界面很简单。Button用于加载图片,Label用于显示图片,Text用于显示结果。
import sys from PySide2.QtGui import QPixmapfrom PySide2.QtWidgets import QApplication, QLabel, QPushButton, QVBoxLayout, QWidget, QFileDialog, QTextEdit, QSizePolicy from PySide2.QtCore import Slot, Qt, QStringListModel, QSizeimport dbr import osclass UI_Window(QWidget):def __init__(self):QWidget.__init__(self)# The default barcode image.dir_path = os.path.dirname(os.path.realpath(__file__))filename = os.path.join(dir_path, 'image.tif')# Create a layout.layout = QVBoxLayout()# Add a buttonself.btn = QPushButton("Load an image")self.btn.clicked.connect(self.pickFile)layout.addWidget(self.btn)# Add a labelself.label = QLabel()self.label.setFixedSize(640, 640)pixmap = self.resizeImage(filename)self.label.setPixmap(pixmap)layout.addWidget(self.label)# Add a text areaself.results = QTextEdit()self.readBarcode(filename)layout.addWidget(self.results)# Set the layoutself.setLayout(layout)self.setWindowTitle("Dynamsoft Barcode Reader")self.setFixedSize(800, 800)点击按钮之后调用系统对话框来加载文件:
def pickFile(self):# Load an image file.filename = QFileDialog.getOpenFileName(self, 'Open file','E:Program Files (x86)DynamsoftBarcode Reader 6.4.1Images', "Barcode images (*)")# Show barcode imagespixmap = self.resizeImage(filename[0])self.label.setPixmap(pixmap)# Read barcodesself.readBarcode(filename[0])调节图片尺寸,让图片显示在固定区域内:
def resizeImage(self, filename):pixmap = QPixmap(filename)lwidth = self.label.maximumWidth()pwidth = pixmap.width()lheight = self.label.maximumHeight()pheight = pixmap.height()wratio = pwidth * 1.0 / lwidthhratio = pheight * 1.0 / lheightif pwidth > lwidth or pheight > lheight:if wratio > hratio:lheight = pheight / wratioelse:lwidth = pwidth / hratioscaled_pixmap = pixmap.scaled(lwidth, lheight)return scaled_pixmapelse:return pixmap调用barcode接口识别条形码:
def readBarcode(self, filename):dbr.initLicense("<Your License>")results = dbr.decodeFile(filename, 0x3FF | 0x2000000 | 0x4000000 | 0x8000000 | 0x10000000)out = ''index = 0for result in results:out += "Index: " + str(index) + "n"out += "Barcode format: " + result[0] + 'n'out += "Barcode value: " + result[1] + 'n'out += '-----------------------------------n'index += 1self.results.setText(out)运行程序:
python barcode-reader.py源码
https://github.com/dynamsoft-dbr/python/tree/master/examples/qt
总结
以上是生活随笔为你收集整理的qt如何创建桌面快捷方式_如何用Qt Python创建简单的桌面条形码应用的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: php 规则配置,模块Config配置规
- 下一篇: Python实现HTTP服务器(四)单进