qt如何创建桌面快捷方式_如何用Qt Python创建简单的桌面条形码应用
生活随笔
收集整理的這篇文章主要介紹了
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配置规
- 下一篇: php cli输出百分比,CLI模式。能