使用python制作翻译简易的翻译软件

  首先,我们需要向百度翻译申请api。百度翻译是给了每月五万字符的免费额度的,足够大家个人使用。

第一步 在百度翻译开发平台注册http://api.fanyi.baidu.com/注册后选择管理控制平台

  开通“通用翻译服务”,然后在总览的底端找到<申请信息>

第二步

如果没有安装相关库的,在终端分别键入

pip install tkinter
pip install urllib

第三步

特别注意要保护好自己的APP ID以及密钥,防止他人盗用。

然后吧APP ID和密钥复制粘贴到下面的代码中:

备注:APP ID 放在appid中,密钥放在appkey中

然后拜拜赛博佛主,点击启动就完成啦

# 制作一个翻译软件
# 百度翻译API
# 第一版
# 23.9.13更新了不选中问题
import hashlib
import tkinter as tk
import json
from urllib import request
from urllib import parse
from hashlib import md5
import random

def translate_word(language, en_str, cho):
    url = 'https://fanyi-api.baidu.com/api/trans/vip/translate'
    # 编译appid与密钥
    appid = ''
    appkey = ''
    salt = random.randint(100000,114515)
    # appid+en+salt+密钥
    m = appid + en_str + str(salt) + appkey
    # 对m做md5
    m_md5 = hashlib.md5(m.encode('utf-8'))
    sign = m_md5.hexdigest()

    # 集合成字典
    Data = {}
    Data['from'] = str(cho)
    Data['to'] = language
    Data['q'] = en_str
    Data['appid'] = appid
    Data['salt'] = str(salt)
    Data['sign'] = sign
    # 用urlencode方法转换成标准格式
    data = parse.urlencode(Data).encode('utf-8')
    # 传递request对象与data信息
    response = request.urlopen(url, data)
    # 获取信息并解码
    html = response.read().decode('utf-8')

    # 用json获取数据
    translate = json.loads(html)
    print(translate)
    # 找到翻译数据
    translate = translate['trans_result'][0]['dst']
    return translate
# 设计按钮
def clean():
    namet.delete(0,tk.END)
    passwordt.delete(0,tk.END)

def click():
    language = str(goal)
    en_str = str(namet.get())
    zh_str = translate_word(language, en_str, cho)
    passwordt.delete(0, tk.END)
    passwordt.insert(0, zh_str)

def click2(event):
    language = str(goal)
    en_str = str(namet.get())
    zh_str = translate_word(language, en_str, cho)
    passwordt.delete(0, tk.END)
    passwordt.insert(0, zh_str)

def check():
    global goal
    goal = r.get()
    print(goal)

def français():
    global cho
    cho = f.get()
    print(cho)

win = tk.Tk()
win.title('翻译')
win.geometry("500x300+280+280")
win.minsize(100,100)
win.maxsize(500,500)

name = tk.Label(win, text='翻译内容')
name.place(x=1, y=1)
namet = tk.Entry(win, width=50)
namet.place(x=55, y=1)

password = tk.Label(win, text='结果')
password.place(x=1, y=25)
passwordt = tk.Entry(win, width=50)
passwordt.place(x=55, y=25)

# 设计翻译语言(language)

lan = tk.Label(win, text='选择要生成的语言')
lan.place(x=1, y=80)
r = tk.StringVar()
r.set('zh')
goal = 'zh'
radio = tk.Radiobutton(win, variable=r, value='zh', text='中文', command=check)
radio.place(x=1, y=100)
radio = tk.Radiobutton(win, variable=r, value='en', text='英语', command=check)
radio.place(x=1, y=120)
radio = tk.Radiobutton(win, variable=r, value='fra', text='法语', command=check)
radio.place(x=1, y=140)

f = tk.StringVar()
f.set('auto')
cho = 'auto'
radiof = tk.Checkbutton(win, variable=f, text='输入法语', offvalue='auto', onvalue='fra', command=français)
radiof.place(x=420, y=1)

b1 = tk.Button(win, text='确定', width=8, command=click)
b1.place(x=95, y=50)
b2 = tk.Button(win, text='取消', width=8, command=clean)
b2.place(x=200, y=50)

# 设计enter键实现确认功能
win.bind('<Return>', click2)
win.mainloop()

PS 将要改名为syy_tt

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值