python GUI图形界面tkinter:实现有道翻译小案例

 

# 实现模拟有道翻译
# coding:utf-8
from tkinter import *
import requests

# 有道翻译
class YouDao:
    def __init__(self):
        pass

    def fanyi(self, content):
        # translate_o改成translate,可防止返回:{'errorCode': 50}
        # url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
        url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
        headers = {
            'referer': 'http://fanyi.youdao.com/',
            'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36 OPR/65.0.3467.78 (Edition Baidu)'
        }
        data = {
            'i': content,
            'from': 'AUTO',
            'to': 'AUTO',
            'smartresult': 'dict',
            'client': 'fanyideskweb',
            'salt': '15791434983359',
            'sign': '4c3ba7ac1aefe25a7e0f6780c3f08512',
            'ts': '1579143498335',
            'bv': '120ea3adb00787750fbc44dd1af54b88',
            'doctype': 'json',
            'version': '2.1',
            'keyfrom': 'fanyi.web',
            'action': 'FY_BY_REALTlME',
        }
        res = requests.post(url, headers=headers, data=data)
        if res.status_code == 200:
            result = res.json()
            if 'translateResult' in result:
                return result['translateResult'][0][0]['tgt']
            else:
                return result
        else:
            return '请求失败'

# 界面
class Application:
    def __init__(self):
        self.youdao = YouDao()

        # 创建窗口对象
        self.window = Tk()

        # 窗口对象的标题
        self.window.title(u'我的程序')

        # 设置窗口的大小和位置(宽:310,高:370,坐标:500,300)
        self.window.geometry("310x370+500+300")
        self.window.minsize(310,370)
        self.window.maxsize(310,370)

        # 创建一个文本框
        # self.text1 = Text(self.window, background='azure')
        self.text1 = Text(self.window, background='#ccc', foreground='black')
        self.text1.place(x=10, y=5, width=290, height=165) # 设置位置坐标和大小
        self.text1.bind('<Key-Return>', self.text1_event) # 绑定键盘回车键事件

        # 创建提示标题
        self.label1 = Label(self.window, text=u'翻译结果:')
        self.label1.place(x=10, y=175)

        # 创建按钮
        self.btn1 = Button(self.window, text='翻译', command=self.btn1_event)
        self.btn1.place(x=220, y=175, width=35, height=25)
        self.btn2 = Button(self.window, text='清空', command=self.btn2_event)
        self.btn2.place(x=263, y=175, width=35, height=25)

        # 创建结果文本框
        self.text2 = Text(self.window, background='black', foreground='white')
        self.text2.place(x=10, y=205, width=290, height=165)

    # text1 监听回车事件
    def text1_event(self, event):
        # 获取用户输入的值
        content = self.text1.get(0.0, END).strip().replace('\n', ' ')
        print(content)
        # 进行翻译
        result = self.youdao.fanyi(content)
        # 填写结果到结果输入框
        self.text2.delete(0.0, END)
        self.text2.insert(END, result)

    # btn1 翻译事件
    def btn1_event(self):
        # 获取用户输入的值
        content = self.text1.get(0.0, END).strip().replace('\n', ' ')
        print(content)

        # 进行翻译
        result = self.youdao.fanyi(content)

        # 填写结果到结果输入框
        self.text2.delete(0.0, END)
        self.text2.insert(END, result)

    # btn2 清空事件
    def btn2_event(self):
        # 清空文本框的内容
        self.text1.delete(0.0, END)
        self.text2.delete(0.0, END)

    def run(self):
        # 进入循环
        self.window.mainloop()

if __name__ == '__main__':
    app = Application()
    app.run()

 

运行结果:

遇到的问题:

1、解决有道翻译返回 {'errorCode': 50}的问题:

1)更改请求 url,把translate_o修改成translate

  参考:https://blog.csdn.net/qq_36697196/article/details/90696845

2)参考:https://blog.csdn.net/suixinlun/article/details/93976400

2、tkinter的基本操作:

   https://www.runoob.com/python/python-gui-tkinter.html

   https://www.cnblogs.com/happy-xiaoxiao/p/10494508.html

3.图形界面打包

1)使用pyinstaller来进行打包,工具安装命令:

pip install pyinstaller

2)打包命令:(只需编译入口文件即可,即:pyinstaller 选项 Python 源文件)

pyinstaller -F -w main.py

参考:https://www.jianshu.com/p/924a130a3d7e 

posted @ 2020-01-17 09:47  KeenLeung  阅读(846)  评论(0编辑  收藏  举报