基于PyQT5制作一個(gè)二維碼生成器
個(gè)性化二維碼的exe桌面應(yīng)用的獲取方式我放在文章最后面了,注意查收。通過執(zhí)行打包后的exe應(yīng)用程序可以直接運(yùn)行生成個(gè)性化二維碼。
開始之前先來看一下通過二維碼生成器是如何生成個(gè)性化二維碼的。
其中使用的python包和之前的GUI應(yīng)用制作使用的模塊是一樣的。
# -*- coding:utf-8 -*- import os import sys from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import * import images
這里的images模塊是用于解決打包應(yīng)用時(shí)外部圖片的引用不能生效的問題。后面的一篇文章中將會(huì)說明如何將外部資源打包到exe的應(yīng)用中去。
做GUI的桌面應(yīng)用,首先還是使用pyqt5進(jìn)行界面的布局和界面組件的添加,雖然代碼量看起來比較多,但邏輯不多。
# -*- coding:utf-8 -*- def init_ui(self): grid = QGridLayout() self.picture_name = '' self.words_label = QLabel() self.words_label.setText('鏈接設(shè)置:') self.words_text = QLineEdit() self.words_text.setPlaceholderText('www.baidu.com') self.words_text.setAttribute(Qt.WA_InputMethodEnabled, False) self.version_label = QLabel() self.version_label.setText('邊距設(shè)置(只允許微調(diào)):') self.version_text = QSpinBox() self.version_text.setRange(1, 3) self.version_text.setValue(1) self.picture_text = QLineEdit() self.picture_text.setPlaceholderText('個(gè)性化圖片路徑') self.picture_text.setReadOnly(True) self.picture_button = QPushButton() self.picture_button.setText('個(gè)性化圖片') self.picture_button.clicked.connect(self.picture_button_click) self.colorized_label = QLabel() self.colorized_label.setText('是否顯示為彩色:') self.colorized_text = QComboBox() colorized_items = ['是', '否'] self.colorized_text.addItems(colorized_items) self.colorized_text.setCurrentIndex(1) self.brightness_label = QLabel() self.brightness_label.setText('調(diào)節(jié)圖片亮度:') self.brightness_text = QDoubleSpinBox() self.brightness_text.setRange(1, 10) self.brightness_text.setSingleStep(1.0) self.save_dir_text = QLineEdit() self.save_dir_text.setPlaceholderText('存儲(chǔ)目錄') self.save_dir_text.setReadOnly(True) self.save_dir_button = QPushButton() self.save_dir_button.setText('自定義路徑') self.save_dir_button.clicked.connect(self.save_dir_button_click) self.generate_button = QPushButton() self.generate_button.setText('快速生成二維碼') self.generate_button.clicked.connect(self.generate_button_click) self.version_current = QLabel() self.version_current.setText('默認(rèn)二維碼為作者公眾號(hào),版本聲明:本應(yīng)用由公眾號(hào) [Python 集中營(yíng)] 發(fā)布!') self.version_current.setAlignment(Qt.AlignCenter) self.version_current.setStyleSheet('color:red') self.image = QLabel() self.image.setScaledContents(True) self.image.setMaximumSize(200, 200) self.image.setPixmap(QPixmap(':/default.png')) grid.addWidget(self.words_label, 0, 0, 1, 1) grid.addWidget(self.words_text, 0, 1, 1, 2) grid.addWidget(self.version_label, 1, 0, 1, 2) grid.addWidget(self.version_text, 1, 2, 1, 1) grid.addWidget(self.picture_text, 2, 0, 1, 2) grid.addWidget(self.picture_button, 2, 2, 1, 1) grid.addWidget(self.colorized_label, 3, 0, 1, 2) grid.addWidget(self.colorized_text, 3, 2, 1, 1) grid.addWidget(self.brightness_label, 4, 0, 1, 2) grid.addWidget(self.brightness_text, 4, 2, 1, 1) grid.addWidget(self.save_dir_text, 5, 0, 1, 2) grid.addWidget(self.save_dir_button, 5, 2, 1, 1) grid.addWidget(self.generate_button, 6, 0, 1, 3) hbox = QHBoxLayout() hbox.addWidget(self.image) hbox.addSpacing(30) hbox.addLayout(grid) vbox = QVBoxLayout() vbox.addLayout(hbox) vbox.addSpacing(10) vbox.addWidget(self.version_current) self.setLayout(vbox)
用到的槽函數(shù)有三個(gè),一個(gè)是為了做選擇背景圖片、第二個(gè)是為了做選擇要存儲(chǔ)生成后的文件存放路徑可以自由選擇存放到什么地方、第三個(gè)是為了做調(diào)起生成二維碼的函數(shù)。
第一個(gè)來看一下如何通過關(guān)聯(lián)槽函數(shù)來實(shí)現(xiàn)讀取需要作為個(gè)性化二維碼的背景圖片。
def picture_button_click(self): import os self.cwd = os.getcwd() txt_file_path = QFileDialog.getOpenFileName(self, "選取文件", self.cwd, "JPG File (*.jpg);; PNG File (*.png)") self.picture_text.setText(txt_file_path[0]) if self.picture_text.text().strip() != "": self.picture_name = txt_file_path[0].split('/')[-1].split('.')[0] print(self.picture_name) else: self.picture_name = ''
第二個(gè)就是選擇存儲(chǔ)文件路徑的槽函數(shù)。
def save_dir_button_click(self): import os self.cwd = os.getcwd() directory = QFileDialog.getExistingDirectory(self, '選取文件夾', self.cwd) print(directory) self.save_dir_text.setText(directory)
是通過dialog對(duì)話框的形式獲取到自定義選擇的存儲(chǔ)文件路徑。
第三個(gè)槽函數(shù)就是為了生成個(gè)性化二維碼,其實(shí)二維碼的生成部分只有一句代碼。那就是MYQR模塊提供的run函數(shù),通過這個(gè)函數(shù)就能實(shí)現(xiàn)個(gè)性化二維碼的生成。
首先,需要導(dǎo)入MYQR這個(gè)庫。
from MyQR import myqr
為了可以看清楚后面二維碼生成函數(shù)(run函數(shù)),先來看一下這個(gè)庫提供的run函數(shù)都有什么參數(shù)。
''' myqr.run() 參數(shù)解釋 words 需要跳轉(zhuǎn)的鏈接或者文字 version 自然數(shù),數(shù)字越大邊長(zhǎng)越大 level 糾錯(cuò)等級(jí) picture 結(jié)合圖片 colorized 是否顯示彩色 contrast 對(duì)比度,默認(rèn)為1.0 brightness 亮度 float,調(diào)節(jié)圖片的亮度 save_name 輸出文件名,默認(rèn)文件名"qrcode.png" save_dir 存儲(chǔ)位置,默認(rèn)存儲(chǔ)當(dāng)前目錄 '''
下面看一下這個(gè)具體生成個(gè)性化二維碼的槽函數(shù)。除了二維碼的生成部分和需要將生成后的二維碼放到應(yīng)用的頁面上展示之外,其他主要就是一些參數(shù)的校驗(yàn)方法。
def generate_button_click(self):
from MyQR import myqr
colorized_index = self.colorized_text.currentIndex()
print(colorized_index)
colorized = None
if colorized_index == 0:
colorized = True
else:
colorized = False
print(colorized)
words_text = self.words_text.text()
words = None
if words_text.strip() != "":
words = words_text.strip()
else:
words = 'default message: Python is very beautiful'
print(words)
version_text = self.version_text.value()
print(version_text)
picture_text = self.picture_text.text()
picture = None
if picture_text.strip() != "":
picture = picture_text
print(picture)
brightness_text = self.brightness_text.value()
print(brightness_text)
save_dir_text = self.save_dir_text.text()
save_dir = None
if save_dir_text.strip() != "":
save_dir = save_dir_text.strip()
else:
save_dir = os.getcwd()
print(save_dir)
myqr.run(words=str(words), version=int(version_text), level='H', picture=picture,
colorized=colorized, contrast=1.0, brightness=float(brightness_text), save_dir=save_dir)
if self.picture_name.strip() != '':
map_dir = save_dir + '/' + self.picture_name + '_qrcode.png'
else:
map_dir = save_dir + '/' + 'qrcode.png'
print(map_dir)
self.image.setPixmap(QPixmap(map_dir))
到此這篇關(guān)于基于PyQT5制作一個(gè)二維碼生成器的文章就介紹到這了,更多相關(guān)PyQT5二維碼生成器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pycharm運(yùn)行出現(xiàn)ImportError:No module named的解決方法
今天小編就為大家分享一篇pycharm運(yùn)行出現(xiàn)ImportError:No module named的解決方法。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-10-10教你pycharm運(yùn)行Django第一個(gè)項(xiàng)目
本文主要介紹了教你pycharm運(yùn)行Django第一個(gè)項(xiàng)目的實(shí)現(xiàn),文中通過圖文示例介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-08-08OpenCV凸包檢測(cè)和凸缺陷學(xué)習(xí)示例
這篇文章主要為大家介紹了OpenCV凸包檢測(cè)和凸缺陷學(xué)習(xí)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06Python中Pickling和Unpickling的區(qū)別詳解
在本文中,我們將探討 Python 中 pickling 和 unpickling 之間的主要區(qū)別,我們將詳細(xì)討論 Python pickling 和 unpickling 的概念,包括它們的目的、語法、用法以及安全可靠的 pickling 和 unpickling 操作的注意事項(xiàng),需要的朋友可以參考下2023-09-09python使用os.listdir和os.walk獲得文件的路徑的方法
本篇文章主要介紹了python使用os.listdir和os.walk獲得文件的路徑的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12pyinstaller打包后偶爾出現(xiàn)黑窗口一閃而過的問題及解決
這篇文章主要介紹了pyinstaller打包后偶爾出現(xiàn)黑窗口一閃而過的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01python連接、操作mongodb數(shù)據(jù)庫的方法實(shí)例詳解
這篇文章主要介紹了python連接、操作mongodb數(shù)據(jù)庫的方法,結(jié)合實(shí)例形式詳細(xì)分析了Python針對(duì)MongoDB數(shù)據(jù)庫的連接、查詢、排序等相關(guān)操作技巧,需要的朋友可以參考下2019-09-09python re.sub()替換正則的匹配內(nèi)容方法
今天小編就為大家分享一篇python re.sub()替換正則的匹配內(nèi)容方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-07-07matplotlib之Pyplot模塊繪制三維散點(diǎn)圖使用顏色表示數(shù)值大小
在撰寫論文時(shí)常常會(huì)用到matplotlib來繪制三維散點(diǎn)圖,下面這篇文章主要給大家介紹了關(guān)于matplotlib之Pyplot模塊繪制三維散點(diǎn)圖使用顏色表示數(shù)值大小的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08python中分組函數(shù)groupby和分組運(yùn)算函數(shù)agg的使用
本文主要介紹了python中分組函數(shù)groupby和分組運(yùn)算函數(shù)agg的使用,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10