python GUI 实践

作者: tww844475003 分类: Python 发布时间: 2021-05-22 16:47

GUI 实现艺术签名程序

from tkinter import *
from tkinter import messagebox
import requests
import re
from PIL import Image, ImageTk


# 艺术签名
def sign():
    # 获取用户输入
    name = input.get()
    # 去掉空格
    name = name.strip()
    # print(name)

    if name == '':
        messagebox.showinfo('提示', message='请输入姓名!')
    else:
        headers = {
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36'
        }
        postData = {
            'word': name,
            'sizes': '60',
            'fonts': '1.ttf',
            'fontcolor': '#000000'
        }
        host = 'http://www.uustv.com/'

        result = requests.post(host, postData, headers=headers)
        result.encoding = 'utf-8'
        html = result.text
        # print(html)
        reg = r'<div class="tu"><img src="(.*?)"/></div>'
        imgPath = re.findall(reg, html)
        imgUrl = host + imgPath[0]
        # print(imgUrl)
        # 保存图片
        response = requests.get(imgUrl).content
        # print(response)
        with open('{}.gif'.format(name), 'wb') as f:
            f.write(response)
        # 显示图片
        bm = ImageTk.PhotoImage(file='{}.gif'.format(name))
        label2 = Label(root, image=bm)
        label2.bm = bm
        label2.grid(row=2, columnspan=2)


# 创建窗口
root = Tk()

# 设置窗口大小 位置
root.geometry('540x310+760+360')

# 设置标题
root.title('艺术签名')

# 标签控件
label = Label(root, text='签名:', font=('微软雅黑', 20))
label.grid(row=0, column=0)

# 输入框控件
input = Entry(root, font=('微软雅黑', 20))
input.grid(row=0, column=1)

# 点击按钮
button = Button(root, text='设计签名', font=('微软雅黑', 20), command=sign)
button.grid(row=1, column=1)

# 显示窗口
root.mainloop()

GUI 利用有道翻译实现中英互译

from tkinter import *
from tkinter import messagebox
import requests


def translation():
    inputContent = entry.get()
    inputContent = inputContent.strip()
    if inputContent == '':
        messagebox.showinfo('提示', message='请输入要翻译的内容')
    else:
        url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
        data = {
            'i': inputContent,
            'from': 'AUTO',
            'to': 'AUTO',
            'smartresult': 'dict',
            'client': 'fanyideskweb',
            # 'salt': '16071526513636',
            # 'sign': '3abe023c21fc85564f56c8cdd15ea398',
            # 'lts': '1607152651363',
            # 'bv': '0785986963146aebf8c240a24088d066',
            'doctype': 'json',
            'version': '2.1',
            'keyfrom': 'fanyi.web',
            'action': 'FY_BY_CLICKBUTTION'
        }
        res = requests.post(url, data=data)
        result = res.json()

        tgt = result['translateResult'][0][0]['tgt']
        # print(tran)
        translateResult.set(tgt)
        return tgt


# 创建窗口
window = Tk()
# 窗口大小坐标
window.geometry('370x100+760+360')
# 窗口标题
window.title('中英互译')

# 控件
label = Label(window, text='输入要翻译的文字:')
label.grid(row=0, column=0)

entry = Entry(window, font=('微软雅黑', 16))
entry.grid(row=0, column=1)

label2 = Label(window, text='翻译之后的结果:')
label2.grid(row=1, column=0)

translateResult = StringVar()
entry2 = Entry(window, font=('微软雅黑', 16), textvariable=translateResult)
entry2.grid(row=1, column=1)

button = Button(window, text='翻译', width=10, command=translation)
button.grid(row=2, column=0, sticky=W)

button2 = Button(window, text='退出', width=10, command=window.quit)
button2.grid(row=2, column=1, sticky=E)

# 显示窗口
window.mainloop()

前端开发那点事
微信公众号搜索“前端开发那点事”

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注