qt如何创建桌面快捷方式_如何用Qt Python创建简单的桌面条形码应用
Qt for Python可以快速跨平臺(tái)的GUI應(yīng)用。這篇文章分享下如何結(jié)合Dynamsoft Barcode Reader SDK來(lái)創(chuàng)建一個(gè)簡(jiǎn)單的讀碼應(yīng)用。
安裝Qt for Python
官方站點(diǎn)可以下載對(duì)應(yīng)的wheel文件,或者通過(guò)命令行安裝:
pip install pyside2我之前用Python 3.6無(wú)法成功加載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。
- 申請(qǐng)一個(gè)免費(fèi)試用的license。
- 獲取extension源碼,然后編譯安裝:
簡(jiǎn)單的Windows桌面掃碼應(yīng)用
Qt安裝之后可以用Qt designer來(lái)設(shè)計(jì)界面并生成Python代碼。
運(yùn)行Python37Libsite-packagesPySide2designer.exe。
設(shè)計(jì)好界面之后保存到.ui文件。
用Python37Scriptspyside2-uic.exe把.ui文件轉(zhuǎn)換成Python文件:
pyside2-uic -x *.ui -o ui.py之后可以直接運(yùn)行這個(gè)Python文件。
應(yīng)用界面很簡(jiǎn)單。Button用于加載圖片,Label用于顯示圖片,Text用于顯示結(jié)果。
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)點(diǎn)擊按鈕之后調(diào)用系統(tǒng)對(duì)話框來(lái)加載文件:
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])調(diào)節(jié)圖片尺寸,讓圖片顯示在固定區(qū)域內(nèi):
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調(diào)用barcode接口識(shí)別條形碼:
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)運(yùn)行程序:
python barcode-reader.py源碼
https://github.com/dynamsoft-dbr/python/tree/master/examples/qt
總結(jié)
以上是生活随笔為你收集整理的qt如何创建桌面快捷方式_如何用Qt Python创建简单的桌面条形码应用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: php 规则配置,模块Config配置规
- 下一篇: php cli输出百分比,CLI模式。能