我能想起来的一直都是你的背影 ————四月是您的谎言

好久没发点啥了~最近在忙着学习前端呢~这次就发点好玩哒~用python3打造GUI个性签名的生成器~

咱们先来看一下效果~

signature_shifan

看着还行吧~~
Python版本是3.5的~



首先,咱们先把界面写出来~~

1
2
3
4
5
6
7
8
9
10
11
12
from tkinter import *
MainWindows = Tk()
MainWindows.geometry("400x250")
MainWindows.maxsize("400","250")
MainWindows.minsize("400","250")
MainWindows.title("个性签名")
MainWindows.config(bg="#444")
Label(MainWindows,text="姓名: ",font=("楷体",20),bg="#444",fg="#fff").place(x=50,y=50)
names = Entry(MainWindows,font=("楷体",20),fg="#666",width=15)
names.place(x=140,y=50)
Button(MainWindows,text="一键设计",font=("楷体",15),relief="solid").place(x=280,y=200)
MainWindows.mainloop()

现在已经可以输入名字了,但是怎么把图片搞过来呢?

去看一看网页http://www.uustv.com/

打开开发者工具,先随便输入一个,看一看啥样的~~

在我点击马上给我设计之后,有两个gif,第二个是我们想要的图片~~

signature_check_img_dizhi

接着,咱们看一下第一个

signature_look_html_dizhi

最后面这个就是我们输入文字
点击view soure,显示本来的面目: word=%E6%88%91%E7%88%B1%E4%B8%AD%E5%9B%BD&sizes=60&fonts=jfcs.ttf&fontcolor=%23000000
看看英文意思就明白了,word的就是我们输入的文字,被浏览器处理过了~我们改就要改这里
图片就更好弄了,点击Preview

signature_dizhi

这就是返回的图片,拼接地址就是:http://www.uustv.com/tmp/149597716941200.gif
但是直接在浏览器上访问是打不开的~~

OK,现在怎么拿到图片也知道了,上代码

1
2
3
4
5
6
7
8
import urllib.request
import re
url = "http://www.uustv.com/"
html_Request = urllib.request.Request(url,data="word={}&sizes=60&fonts=jfcs.ttf&fontcolor=%23000000".format("我爱中国").encode( "utf-8"))
html_open = urllib.request.urlopen(html_Request).read().decode("utf-8")
res = '<div class="tu"><img src="(.*?)"/></div>'
re_html = re.findall(res, html_open)
print(re_html)

这个会返回一个list,[‘tmp/149597780094792.gif’],没问题,现在把它们放在一起就ok了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import urllib.request
import re
from tkinter import *
from tkinter import messagebox
from PIL import Image
def get_name():
name = names.get()
if not name:
messagebox.showinfo(title="提示",message="姓名不得为空")
else:
url = "http://www.uustv.com/"
html_Request = urllib.request.Request(url,data="word={}&sizes=60&fonts=jfcs.ttf&fontcolor=%23000000".format(name).encode("utf-8"))
html_open = urllib.request.urlopen(html_Request).read().decode("utf-8")
res = '<div class="tu"><img src="(.*?)"/></div>'
re_html = re.findall(res,html_open)
urllib.request.urlretrieve(url+re_html[0],"%s.gif" % name)
try:
open_img = Image.open("%s.gif" % name)
open_img.show()
open_img.close()
except :
messagebox.showinfo(title="提示",message="请自行打开!")
if __name__ == '__main__':
MainWindows = Tk()
MainWindows.geometry("400x250")
MainWindows.maxsize("400","250")
MainWindows.minsize("400","250")
MainWindows.title("个性签名")
MainWindows.config(bg="#444")
Label(MainWindows,text="姓名: ",font=("楷体",20),bg="#444",fg="#fff").place(x=50,y=50)
names = Entry(MainWindows,font=("楷体",20),fg="#666",width=15)
names.place(x=140,y=50)
Button(MainWindows,text="一键设计",font=("楷体",15),relief="solid",command=get_name).place(x=280,y=200)
MainWindows.mainloop()

2017年 05月 29日 星期一 17点
有人要求把剩下的六种字体也写上,按照他的要求,还是加上了,其实很容易的,把data数据的fonts的值改成对应的字体就行了~~直接上代码了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import urllib.request
import re
from tkinter import *
from tkinter import messagebox
from PIL import Image
def downloading(name,var):
url = "http://www.uustv.com/"
html_Request = urllib.request.Request(url,data="word={}&sizes=60&fonts={}&fontcolor=%23000000".format(name,var).encode("utf-8"))
html_open = urllib.request.urlopen(html_Request).read().decode("utf-8")
res = '<div class="tu"><img src="(.*?)"/></div>'
re_html = re.findall(res,html_open)
urllib.request.urlretrieve(url+re_html[0],"%s.gif" % name)
try:
open_img = Image.open("%s.gif" % name)
open_img.show()
open_img.close()
except :
messagebox.showinfo(title="提示",message="请自行打开!")
def get_name():
name = names.get()
select_var = var.get()
signature_ttf = ["jfcs.ttf","qmt.ttf","bzcs.ttf","lfc.ttf","haku.ttf","zql.ttf","yqk.ttf"]
if not name:
messagebox.showinfo(title="提示",message="姓名不得为空!")
elif select_var == 1:
downloading(name,signature_ttf[0])
elif select_var == 2:
downloading(name,signature_ttf[1])
elif select_var == 3:
downloading(name,signature_ttf[2])
elif select_var == 4:
downloading(name,signature_ttf[3])
elif select_var == 5:
downloading(name,signature_ttf[4])
elif select_var == 6:
downloading(name,signature_ttf[5])
elif select_var == 7:
downloading(name,signature_ttf[6])
else:
messagebox.showinfo(title="提示",message="请选择您想要的样式!")
if __name__ == '__main__':
MainWindows = Tk()
MainWindows.geometry("400x250")
MainWindows.maxsize("400","250")
MainWindows.minsize("400","250")
MainWindows.title("个性签名")
MainWindows.config(bg="#444")
Label(MainWindows,text="姓名: ",font=("楷体",20),bg="#444",fg="#fff").place(x=50,y=50)
names = Entry(MainWindows,font=("楷体",20),fg="#666",width=15)
names.place(x=140,y=50)
var = IntVar()
rb1 = Radiobutton(MainWindows,text="个性签",variable=var,value=1,bg="#444",relief="solid").place(x=50,y=115)
rb2 = Radiobutton(MainWindows,text="连笔签",variable=var,value=2,bg="#444",relief="solid").place(x=50,y=150)
rb3 = Radiobutton(MainWindows,text="潇洒签",variable=var,value=3,bg="#444",relief="solid").place(x=50,y=185)
rb4 = Radiobutton(MainWindows,text="草体签",variable=var,value=4,bg="#444",relief="solid").place(x=50,y=220)
rb5 = Radiobutton(MainWindows,text="合文签",variable=var,value=5,bg="#444",relief="solid").place(x=150,y=115)
rb6 = Radiobutton(MainWindows,text="商务签",variable=var,value=6,bg="#444",relief="solid").place(x=150,y=150)
rb7 = Radiobutton(MainWindows,text="可爱签",variable=var,value=7,bg="#444",relief="solid").place(x=150,y=185)
Button(MainWindows,text="一键设计",font=("楷体",15),relief="solid",command=get_name).place(x=280,y=200)
MainWindows.mainloop()

效果:

signarture_fonts

很简单吧~源代码
如果出现问题,请在下面评论~~




更新:源代码

修复窗口未响应的假死状态