爬有道翻译的的一个程序+tkinter界面
from tkinter import *import urllib.request
import urllib.parse
import json
url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
root = Tk()
root.title('翻译')
Label(root,text='请输入:').grid(row=0,column=0,padx=10,pady=10)
Label(root,text='翻译结果:').grid(row=3,column=0,padx=10,pady=10)
e1 =Text(root,width=40,height=5,font=("宋体", 18, "bold"))
e1.grid(row=0,column=1)
e2 =Text(root,width=40,height=5,font=("宋体", 18, "bold"))
e2.grid(row=4,column=1)
def tr():
e2.delete(1.0, END)
temp = e1.get(1.0, END)
date = {}
date['i'] = temp
date['from']='AUTO'
date['to']='AUTO'
date['smartresult']='dict'
date['client']='fanyideskweb'
date['salt']='15979913254708'
date['sign']='fe5f00b4f6da188ae071a5f15f239354'
date['lts']='1597991325470'
date['bv']='cc652a2ad669c22da983a705e3bca726'
date['doctype']='json'
date['version']='2.1'
date['keyfrom']='fanyi.web'
date['action']='FY_BY_CLICKBUTTION'
date = urllib.parse.urlencode(date).encode('utf-8')
response = urllib.request.urlopen(url,date)
html = response.read().decode('utf-8')
target = json.loads(html)
target = target['translateResult']['tgt']
e2.insert(INSERT,(target))
b2=Button(root,text='翻译',width=10,command=tr)
b2.grid(row=0,column=3,padx=10,pady=10)
b1=Button(root,text='退出',width=10,command=root.quit)
b1.grid(row=5,column=3,padx=10,pady=10)
def empty():
e1.delete(1.0, END)
b3=Button(root,text='清空',width=10,command=empty)
b3.grid(row=3,column=3,padx=10,pady=10)
mainloop() 2.0 版本
from tkinter import *
import urllib.request
import urllib.parse
import json
url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
root = Tk()
root.title('翻译')
def tr(event):
e2.delete(1.0, END)
event = e1.get(1.0, END)
date = {}
date['i'] = event
date['from']='AUTO'
date['to']='AUTO'
date['smartresult']='dict'
date['client']='fanyideskweb'
date['salt']='15979913254708'
date['sign']='fe5f00b4f6da188ae071a5f15f239354'
date['lts']='1597991325470'
date['bv']='cc652a2ad669c22da983a705e3bca726'
date['doctype']='json'
date['version']='2.1'
date['keyfrom']='fanyi.web'
date['action']='FY_BY_CLICKBUTTION'
date = urllib.parse.urlencode(date).encode('utf-8')
response = urllib.request.urlopen(url,date)
html = response.read().decode('utf-8')
target = json.loads(html)
target = target['translateResult']['tgt']
e2.insert(INSERT,(target))
e1 =Text(root,width=40,height=5,font=("宋体", 18, "bold"))
e1.pack(fill=X)
e1.bind('<KeyRelease>',tr)
e1.focus_set()
e2 =Text(root,width=40,height=5,font=("宋体", 18, "bold"))
e2.pack(fill=X)
def empty():
e1.delete(1.0, END) 2.0版本from tkinter import *
import urllib.request
import urllib.parse
import json
url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
root = Tk()
root.title('翻译')
def tr(event):
e2.delete(1.0, END)
event = e1.get(1.0, END)
date = {}
date['i'] = event
date['from']='AUTO'
date['to']='AUTO'
date['smartresult']='dict'
date['client']='fanyideskweb'
date['salt']='15979913254708'
date['sign']='fe5f00b4f6da188ae071a5f15f239354'
date['lts']='1597991325470'
date['bv']='cc652a2ad669c22da983a705e3bca726'
date['doctype']='json'
date['version']='2.1'
date['keyfrom']='fanyi.web'
date['action']='FY_BY_CLICKBUTTION'
date = urllib.parse.urlencode(date).encode('utf-8')
response = urllib.request.urlopen(url,date)
html = response.read().decode('utf-8')
target = json.loads(html)
target = target['translateResult']['tgt']
e2.insert(INSERT,(target))
e1 =Text(root,width=40,height=5,font=("宋体", 18, "bold"))
e1.pack(fill=X)
e1.bind('<KeyRelease>',tr)
e1.focus_set()
e2 =Text(root,width=40,height=5,font=("宋体", 18, "bold"))
e2.pack(fill=X)
def empty():
e1.delete(1.0, END) 支持楼主
页:
[1]