一个设计签名的小程序
一个制作设计签名的小程序,话不多说,效果图如下:
运行界面:
输入姓名:
一键设计:
下面是程序的代码: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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102from tkinter import Tk, Label, Entry, Button, PhotoImage
import tkinter.messagebox as tkMessageBox
import requests
from urllib.parse import urljoin
import io
from PIL import Image, ImageTk
import sys
from threading import Thread
def show_img(url, row, column):
"""显示图片"""
image_bytes = requests.get(url).content
data_stream = io.BytesIO(image_bytes)
pil_image = Image.open(data_stream)
pi = ImageTk.PhotoImage(image=pil_image, width=500, height=180)
l2 = Label(image=pi)
l2.image = pi
l2.grid(row=row, column=column)
def design(url, data, text, row, column):
"""访问接口"""
data["id"] = text
try:
rs = requests.post(url, data)
except Exception as e:
print(e)
sys.exit(1)
if rs.status_code == 200:
gif_src = rs.text.split("=")[1].lstrip("\"").rstrip("\">")
gif_all = urljoin(url, gif_src)
show_img(gif_all, row, column)
def button_thing(url, data, entry, label_row, label_column):
"""按钮事件"""
entry_text = entry.get()
if entry_text == "":
tkMessageBox.showinfo("出错了", "姓名为空!")
else:
design(url, data, entry_text, label_row, label_column)
class MyTK:
def __init__(self, title):
self.tk = None
self.title = title
def init(self, add=None, mult=None, width=None, height=None, s_width=None, s_height=None, resizable=(False, False)):
self.tk = Tk()
if add:
assert s_width or s_height, "Add parameter error"
self.tk.geometry("+{}+{}".format(s_width, s_height))
if mult:
assert height or width, "Mult parameter error"
self.tk.geometry("{}x{}".format(width, height))
self.tk.title(self.title)
self.tk.resizable(*resizable)
def add_label(self, row, column, text=None, *args, **kwargs):
self.label = Label(self.tk, text=text, *args, **kwargs)
self.label.grid(row=row, column=column)
return self.label
def add_entry(self, row, column, *args, **kwargs):
self.entry = Entry(self.tk, *args, **kwargs)
self.entry.grid(row=row, column=column)
return self.entry
def add_button(self, row, column, *args, **kwargs):
self.button = Button(self.tk, *args, **kwargs)
self.button.grid(row=row, column=column)
return self.button
def loop(self):
assert self.tk
self.tk.mainloop()
if __name__ == '__main__':
url = "http://www.jiqie.com/a/re22.php"
data = {
"id": "",
"id1": "30",
"id2": "901",
"id3": "#0000FF",
"id4": "#0000DD",
"id5": "#0000AA",
"id6": "#000077",
"idi": "jiqie"
}
t = MyTK("个人签名")
t.init(add=True, s_width=450, s_height=250)
t.add_label(0, 0, "姓名:")
entry = t.add_entry(0, 1)
label_row = 0
label_column = 10
t.add_button(2, 1, text="一件设计签名", command=lambda: button_thing(url, data, entry, label_row, label_column))
# t.add_button(2, 1, text="一件设计签名", command=lambda: Thread(target=button_thing, args=(url, data, entry, label_row, label_column)).start())
t.loop()
简单的玩一玩,高手勿喷,有指点的可以留言或者发邮箱,谢谢。