網(wǎng)站首頁 編程語言 正文
前言
現(xiàn)如今我們生活在一個二維碼的時代,生活中處處都是二維碼,支付要使用二維碼,微信加好友要使用二維碼,乘坐公共交通要使用二維碼,那么,如果要你使用python語言生成指定內(nèi)容的二維碼,你該怎么做呢?下面我就來帶你走進(jìn)qrcode的世界吧!
1.下載所需的模塊
qrcode是二維碼生成的Python開源庫,而kinter模塊(“Tk 接口”)是Python的標(biāo)準(zhǔn)Tk GUI工具包的接口。
pip install qrcode
pip install tkinter
下載完畢后在相應(yīng)的項(xiàng)目中導(dǎo)入相應(yīng)的模塊即可
import qrcode
import tkinter
2.生成指定內(nèi)容的二維碼
函數(shù) | 描述 |
---|---|
qrcode.QRCode() | 生成一個QRCode實(shí)例 |
add_data(content) | 將content內(nèi)容添加到QRCode實(shí)例中 |
make() | 生成指定內(nèi)容的二維碼 |
make_image() | 生成二維碼圖片 |
save(path) | 將指定圖片保存到path路徑下 |
def mk_qrcode(content):
q = qrcode.QRCode()#創(chuàng)建一個QRCode實(shí)例
q.add_data(content)# 給二維碼添加內(nèi)容
q.make() #生成二維碼
#生成圖片
img = q.make_image()
# 保存圖片
img.save(filename)
3.加載二維碼到GUI窗口上顯示
函數(shù) | 描述 |
---|---|
tkinter.Tk() | 生成一個窗體對象 |
tkinter.Canvas() | 生成一個畫布對象 |
tkinter.PhotoImage() | 創(chuàng)建一個圖片對象 |
create_image() | 將圖片加載到畫布上 |
pack() | 顯示畫布 |
mainloop() | 使窗體一直運(yùn)行 |
具體代碼如下所示:
def show():
tk = tkinter.Tk() # 創(chuàng)建一個窗體對象
canvas = tkinter.Canvas(tk, width=400, height=400, bg='pink') #創(chuàng)建一個畫布
img = tkinter.PhotoImage(file=filename) # 創(chuàng)建一個圖片對象
canvas.create_image(200, 200, image=img) #將此圖片加載到畫布上
canvas.pack() #顯示畫布
tk.mainloop() # 使窗體一直運(yùn)行
4.全部代碼展示
import qrcode
import tkinter
filename = '二維碼.png'
def mk_qrcode(content):
q = qrcode.QRCode()
q.add_data(content)# 給二維碼添加內(nèi)容
q.make() #生成二維碼
#生成圖片
img = q.make_image()
# 保存圖片
img.save(filename)
def show():
tk = tkinter.Tk() # 創(chuàng)建一個窗體對象
canvas = tkinter.Canvas(tk, width=400, height=400, bg='pink') #創(chuàng)建一個畫布
img = tkinter.PhotoImage(file=filename) # 創(chuàng)建一個圖片對象
canvas.create_image(200, 200, image=img) #將此圖片加載到畫布上
canvas.pack() #顯示畫布
tk.mainloop() # 使窗體一直運(yùn)行
if __name__ == '__main__':
result = input('請輸入指定內(nèi)容:').strip()
mk_qrcode(result)
show()
5.運(yùn)行結(jié)果展示
使用微信掃描對應(yīng)的二維碼后的結(jié)果為:
總結(jié)?
原文鏈接:https://blog.csdn.net/qq_51447436/article/details/126734845
相關(guān)推薦
- 2022-12-24 C++中的模板類繼承和成員訪問問題_C 語言
- 2022-11-17 Python中的優(yōu)先隊(duì)列(priority?queue)和堆(heap)_python
- 2024-04-08 啟動spring-boot出現(xiàn)Error creating bean with name ‘conf
- 2022-05-14 Python函數(shù)中的作用域規(guī)則詳解_python
- 2022-05-02 numpy中np.dstack()、np.hstack()、np.vstack()用法_python
- 2022-08-05 RedisConfig 配置文件
- 2022-05-20 解決cnpm : 無法加載文件 C:\Users\Administrator\AppData\Roa
- 2022-11-17 python中?OpenCV和Pillow處理圖像操作及時間對比_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支