import tkinter as tk
from tkinter import *
from tkinter import ttk
import requests
from bs4 import BeautifulSoup
import urllib.request
from tkinter import messagebox


data = {
    'id': '姓名',
    'idi': 'jiqie',
    'id1': '样式编号',
    'id2': '#FFFFFE',
    'id4': '#000000',
    'id6': '#000000'
}

baseUrl = 'http://www.yishuzi.com'
headers = {
    'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36',
    'Host' : 'www.yishuzi.com',
    'Origin' : 'http://www.yishuzi.com',
}
#获取所以的样式 以及对应的ID  ID需要作为post参数
styleNameAll = []  #所有样式
styleNumAll = []   #所有ID
res = requests.get(baseUrl, headers=headers)
res.encoding = 'utf-8'
soup = BeautifulSoup(res.text, 'lxml')
style = soup.find_all('option')
for styleName in style:
    styleNameAll.append(styleName.string)
    styleNumAll.append(styleName.get('value'))

# 窗体
window = tk.Tk()
window.title('艺术字体生成器v1.0')
window.geometry('420x380+560+200')


# 说明文字
def click():
    tk.messagebox._show('关于&说明', '    本程序仅支持在联网的环境下使用\n    颜色选项可以使用下拉框里的预设\n    也可以输入十六进制RGB数值（不带#）\n    或者不输入任何使用默认值\n    如：FF0000 ---- 红色\n    注意部分字体不支持更改颜色\n    如 花鸟艺术签\n    同时注意可生成字数存在上限问题\n    不要在意UI 就这样 have fun！\n\n                                    by  小白的路\n            Mail:zxy3953@gmail.com')
# 获取图片 保存 命名等
def getImg():
    #指定想要的 样式ID
    flag = sty.get().split('.')[:1]
    nameFlag = ''.join(sty.get().split('.')[1:])

    intFlag = int(''.join(flag)) - 1
    data['id1'] = styleNumAll[intFlag]
    #指定生成的文字内容
    data['id'] = Entry1.get()

    #指定背景颜色

    if combo2.get() == '':
        data['id2'] = '#FFFFFE'
    elif combo2.get() == '粉红':
        data['id2'] = '#FFC0CB'
    elif combo2.get() == '猩红':
        data['id2'] = '#DC143C'
    elif combo2.get() == '深粉色':
        data['id2'] = '#FF1493'
    elif combo2.get() == '紫罗兰':
        data['id2'] = '#EE82EE'
    elif combo2.get() == '紫色':
        data['id2'] = '#800080'
    elif combo2.get() == '靛青':
        data['id2'] = '#4B0082'
    elif combo2.get() == '幽灵白':
        data['id2'] = '#F8F8FF'
    elif combo2.get() == '石板灰':
        data['id2'] = '#708090'
    elif combo2.get() == '天蓝色':
        data['id2'] = '#87CEEB '
    elif combo2.get() == '水鸭色':
        data['id2'] = '#008080'
    elif combo2.get() == '海洋绿':
        data['id2'] = '#2E8B57'
    elif combo2.get() == '象牙白':
        data['id2'] = '#FFFFF0'
    elif combo2.get() == '卡其布':
        data['id2'] = '# F0E68C '
    elif combo2.get() == '巧克力':
        data['id2'] = '#D2691E'
    elif combo2.get() == '番茄':
        data['id2'] = '#FF6347'
    elif combo2.get() == '栗色':
        data['id2'] = '#800000'
    elif combo2.get() == '金色':
        data['id2'] = '#FFD700'
    else:
        data['id2'] = '#' + combo2.get()

    #指定文字颜色
    if combo1.get() == '':
        data['id6'] = '#000000'
    elif combo1.get() == '粉红':
        data['id6'] = '#FFC0CB'
    elif combo1.get() == '猩红':
        data['id6'] = '#DC143C'
    elif combo1.get() == '深粉色':
        data['id6'] = '#FF1493'
    elif combo1.get() == '紫罗兰':
        data['id6'] = '#EE82EE'
    elif combo1.get() == '紫色':
        data['id6'] = '#800080'
    elif combo1.get() == '靛青':
        data['id6'] = '#4B0082'
    elif combo1.get() == '幽灵白':
        data['id6'] = '#F8F8FF'
    elif combo1.get() == '石板灰':
        data['id6'] = '#708090'
    elif combo1.get() == '天蓝色':
        data['id6'] = '#87CEEB '
    elif combo1.get() == '水鸭色':
        data['id6'] = '#008080'
    elif combo1.get() == '海洋绿':
        data['id6'] = '#2E8B57'
    elif combo1.get() == '象牙白':
        data['id6'] = '#FFFFF0'
    elif combo1.get() == '卡其布':
        data['id6'] = '# F0E68C '
    elif combo1.get() == '巧克力':
        data['id6'] = '#D2691E'
    elif combo1.get() == '番茄':
        data['id6'] = '#FF6347'
    elif combo1.get() == '栗色':
        data['id6'] = '#800000'
    elif combo1.get() == '金色':
        data['id6'] = '#FFD700'
    else:
        data['id6'] = '#' + combo3.get()
    #指定背景颜色
    if combo3.get() == '':
        data['id4'] = '#000000'
    elif combo3.get() == '粉红':
        data['id4'] = '#FFC0CB'
    elif combo3.get() == '猩红':
        data['id4'] = '#DC143C'
    elif combo3.get() == '深粉色':
        data['id4'] = '#FF1493'
    elif combo3.get() == '紫罗兰':
        data['id4'] = '#EE82EE'
    elif combo3.get() == '紫色':
        data['id4'] = '#800080'
    elif combo3.get() == '靛青':
        data['id4'] = '#4B0082'
    elif combo3.get() == '幽灵白':
        data['id4'] = '#F8F8FF'
    elif combo3.get() == '石板灰':
        data['id4'] = '#708090'
    elif combo3.get() == '天蓝色':
        data['id4'] = '#87CEEB '
    elif combo3.get() == '水鸭色':
        data['id4'] = '#008080'
    elif combo3.get() == '海洋绿':
        data['id4'] = '#2E8B57'
    elif combo3.get() == '象牙白':
        data['id4'] = '#FFFFF0'
    elif combo3.get() == '卡其布':
        data['id4'] = '# F0E68C '
    elif combo3.get() == '巧克力':
        data['id4'] = '#D2691E'
    elif combo3.get() == '番茄':
        data['id4'] = '#FF6347'
    elif combo3.get() == '栗色':
        data['id4'] = '#800000'
    elif combo3.get() == '金色':
        data['id4'] = '#FFD700'
    else:
        data['id4'] = '#' + combo3.get()



    name = data['id']
    request = '/a/re.php'
    res = requests.post(baseUrl+request,headers = headers,data=data)

    imgUrl = res.text.split(r'"')[1:2]
    try:
        if urllib.request.urlretrieve(imgUrl[0],filename='【'+name+'】'+'____'+nameFlag+'.png'):
            tk.messagebox._show('OK', '签名制作完成，请查看同文件夹下生成的图片文件！(✿◡‿◡)')
    except:
        tk.messagebox.showerror('Error', '出错了，请重新输入参数或重启软件⊙﹏⊙∥')

# 所有需要使用的控件

label1 = tk.Label(window,text="字体样式：")
label1.place(x=60, y=60)

sty = tk.StringVar()
styChosen = ttk.Combobox(window, width=20, textvariable=sty)

styChosen['values'] = styleNameAll     # 设置下拉列表的值
styChosen.place(x = 200, y = 60)
# styChosen.grid(column=1, row=4)      # 设置其在界面中出现的位置 column代表列 row 代表行
styChosen.current(22)    # 设置下拉列表默认显示的值，0为 styChosen['values'] 的下标值



label2 = tk.Label(window,text="字体颜色：")
label2.place(x=60, y=110)
combo1 = ttk.Combobox(window, textvariable='')

combo1.place(x = 200, y = 110)
combo1['value'] = ('粉红','猩红','深粉色','紫罗兰','紫色','靛青','幽灵白','石板灰','天蓝色','水鸭色','海洋绿','象牙白','卡其布','巧克力','番茄色','栗色','金色')


label3 = tk.Label(window,text="背景颜色：")
label3.place(x=60, y=160)

combo2 = ttk.Combobox(window, textvariable='')
combo2.place(x = 200, y = 160)
combo2['value'] = ('粉红','猩红','深粉色','紫罗兰','紫色','靛青','幽灵白','石板灰','天蓝色','水鸭色','海洋绿','象牙白','卡其布','巧克力','番茄色','栗色','金色')


label5 = tk.Label(window,text="描边颜色：")
label5.place(x=60, y=210)

combo3 = ttk.Combobox(window, textvariable='')
combo3.place(x = 200, y = 210)
combo3['value'] = ('粉红','猩红','深粉色','紫罗兰','紫色','靛青','幽灵白','石板灰','天蓝色','水鸭色','海洋绿','象牙白','卡其布','巧克力','番茄色','栗色','金色')


label4 = Label(window, text="生成的文字：")
label4.place(x = 60, y = 260)

Entry1 = Entry(window, width = 23,bd=1)
Entry1.place(x = 200, y = 260)

button1 = tk.Button(window, text='生 成', command=getImg,width=10,bd=0, font=('微雅软黑', 12 ),fg='green')
button1.place(x=170, y=310)

button2 = tk.Button(window, text='说明', command=click,bd=0,width=15,font=('微雅软黑', 12 ),fg='green')
button2.place(x=320, y=15)



window.mainloop()
