欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python+Tkinter制作在線個(gè)性簽名工具

 更新時(shí)間:2022年03月05日 14:21:06   作者:宇宙超級(jí)無敵霹靂西瓜君  
這篇文章主要為大家分享如何利用Python中的Tkinter庫制作一個(gè)簡(jiǎn)易的在線個(gè)性簽名生成工具,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下

思路:先選擇在線簽名網(wǎng)站,找到接口模擬請(qǐng)求,然后將生成的簽名圖片顯示在 Tkinter 生成的 GUI 窗口上,最后保存生成的簽名圖片

選擇網(wǎng)址為:http://www.uustv.com/

首先了解爬蟲的基本步驟:

發(fā)起請(qǐng)求 :即發(fā)送一個(gè)Request,可能包含額外的headers,data等信息

獲取響應(yīng)內(nèi)容 :得到網(wǎng)頁的HTML文件內(nèi)容

解析內(nèi)容:可以使用正則表達(dá)式提取出想要的內(nèi)容

保存數(shù)據(jù):將數(shù)據(jù)存為文本,或mp3,mp4等文件或存到數(shù)據(jù)庫等

一、首先確定GUI界面:

例如如:

代碼:

root =Tk()
root.title('簽名工具')
root.resizable(0,0)

option='1.藝術(shù)簽 2.連筆簽 3.商務(wù)簽 4.楷書簽 5,瀟灑簽 6.草體簽 7.行書簽 8.個(gè)性簽 9.可愛簽'
var1=StringVar(value='')
var2=StringVar(value='')


lable1=Label(root,text=option,font=('黑體',6))
lable2=Label(root,text='輸入你的名字:',font=('黑體',13))

entry1=Entry(root,width=40,font=('黑體',13),textvariable=var1)
entry2=Entry(root,width=10,font=('黑體',13),textvariable=var2)

button=Button(root,text='確定',command=lambda:check())
lable1.grid(row=0,columnspan=3)
lable2.grid(row=0,column=0)
entry1.grid(row=1,column=1)
entry2.grid(row=1,column=2)
button.grid(row=1,column=3)
root.mainloop()

二、爬取我們需要的內(nèi)容

1. 爬取的網(wǎng)頁樣式:目的獲取紅色框的圖片

2. 請(qǐng)求網(wǎng)頁需要的參數(shù):url和表單數(shù)據(jù)data

url:

表單數(shù)據(jù): 如圖下四個(gè)參數(shù):

fonts為簽名樣式在網(wǎng)頁的真實(shí)值,我們輸入的樣式

word為輸入的名字

代碼:

 signature = ['1.ttf','zql.ttf','8.ttf','6.ttf','bzcs.ttf','lfc.ttf','2.ttf','3.ttf','yqk.ttf']
        url='http://m.uustv.com/'
        #請(qǐng)求網(wǎng)頁需要的參數(shù)
        data ={
            'word':name,
            'sizes':60,
            'fonts':signature[num-1], #實(shí)際代碼中的1表示0
            'fontcolor': '#000000'
        }
        #1.對(duì)需要爬取的網(wǎng)頁發(fā)起請(qǐng)求  2. 獲取響應(yīng)的內(nèi)容
        response=requests.post(url,data)

3.爬取圖片的步驟

       #1.對(duì)需要爬取的網(wǎng)頁發(fā)起請(qǐng)求  2. 獲取響應(yīng)的內(nèi)容
        response=requests.post(url,data)
        response.encoding='utf-8' 
        print(response.text)
        #3.解析內(nèi)容 
        imgXpath='<img src="(.*?)"/></div>' #對(duì)請(qǐng)求回來的數(shù)據(jù)進(jìn)行提取
        imgUrls=re.findall(imgXpath,response.text) #正則取下來的是一個(gè)列表
        content=requests.get(url+imgUrls[0]).content
        #4.將獲取的二進(jìn)制內(nèi)容保存為圖片
        with open('{}.gif'.format(name),'wb') as f: 
            f.write(content)
        #5.顯示圖片
        bg_img=ImageTk.PhotoImage(file='{}.gif'.format(name))
        #6.將圖片在GUI的底部顯示
        lable=Label(root,image=bg_img)
        lable.bg_img=bg_img
        lable.grid(row=2,columnspan=4)  #在第三行顯示,合并四列
    except:
        messagebox.showinfo('提示',message='生成失敗')
    pass

4. 爬取的結(jié)果:

三、完整代碼

from tkinter import * 
from tkinter import messagebox
from PIL import  Image,ImageTk  #pip install pillow顯示圖片
import re
import  requests

root =Tk()
root.title('簽名工具')
root.resizable(0,0)

option='1.藝術(shù)簽 2.連筆簽 3.商務(wù)簽 4.楷書簽 5,瀟灑簽 6.草體簽 7.行書簽 8.個(gè)性簽 9.可愛簽'
var1=StringVar(value='')
var2=StringVar(value='')

def check():  #輸入規(guī)范處理
    name = entry1.get()
    num  = entry2.get()
    flag = True
    if num.isdigit():
        num= int(num)
    if name=='':
        var1.set('不能為空')
        flag= False
    if name.isdigit():
        var1.set('不能為數(shù)字')
        flag= False
    if type(num) == type(' ') or num== '':
        var2.set('1-9')
        flag=False
    else:
        if num>9 or num <1:
            var2.set('1-9')
            flag = False
    if flag :
        get_img(name,num) #輸入的用戶名稱和簽名
    pass

def get_img(name,num,root=root):
    try:
        #這里的接口font是選擇簽名的樣式
        signature = ['1.ttf','zql.ttf','8.ttf','6.ttf','bzcs.ttf','lfc.ttf','2.ttf','3.ttf','yqk.ttf']
        url='http://m.uustv.com/'
        #請(qǐng)求網(wǎng)頁需要的參數(shù)
        data ={
            'word':name,
            'sizes':60,
            'fonts':signature[num-1], #實(shí)際代碼中的1表示0
            'fontcolor': '#000000'
        }
        #1.對(duì)需要爬取的網(wǎng)頁發(fā)起請(qǐng)求  2. 獲取響應(yīng)的內(nèi)容
        response=requests.post(url,data)
        response.encoding='utf-8' 
        print(response.text)
        #3.解析內(nèi)容
        imgXpath='<img src="(.*?)"/></div>' #對(duì)請(qǐng)求回來的數(shù)據(jù)進(jìn)行提取
        imgUrls=re.findall(imgXpath,response.text) #正則取下來的是一個(gè)列表
        content=requests.get(url+imgUrls[0]).content #獲取的圖片是二進(jìn)制文件
        #4.將獲取的二進(jìn)制內(nèi)容保存為圖片
        with open('{}.gif'.format(name),'wb') as f:
            f.write(content)
        #5.顯示圖片
        bg_img=ImageTk.PhotoImage(file='{}.gif'.format(name)) 
        #6.將圖片在GUI的底部顯示
        lable=Label(root,image=bg_img)
        lable.bg_img=bg_img
        lable.grid(row=2,columnspan=4)  #在第三行顯示,合并四列
    except:
        messagebox.showinfo('提示',message='生成失敗')
    pass


lable1=Label(root,text=option,font=('黑體',6))
lable2=Label(root,text='輸入你的名字:',font=('黑體',13))

entry1=Entry(root,width=40,font=('黑體',13),textvariable=var1)
entry2=Entry(root,width=10,font=('黑體',13),textvariable=var2)

button=Button(root,text='確定',command=lambda:check())
lable1.grid(row=0,columnspan=3)
lable2.grid(row=0,column=0)
entry1.grid(row=1,column=1)
entry2.grid(row=1,column=2)
button.grid(row=1,column=3)
root.mainloop()

以上就是Python+Tkinter制作在線個(gè)性簽名工具的詳細(xì)內(nèi)容,更多關(guān)于Python Tkinter個(gè)性簽名的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論