-
实现简单的词典:
from urllib import request, parse
# 制作转化中文为URL格式
from urllib.parse import quote
# 分析库
from lxml import etree
# 用tkinter做图形化
from tkinter import *
# 解析json数据
import json
#pip install pillow
from PIL import ImageTk,Image
# 英文单词查询网址
ebase = 'http://dict.youdao.com/w/eng/'
# 中文单词查询网址
cbase = 'http://dict.youdao.com/w/'
# 翻译网址
trans = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
# 查询时http头
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0"}
# 翻译提交的字典
dic = {"doctype": "json"}
# 判断查询类型,1是查词,2是翻译。
def chaxun():
if v.get() == 1:
chaci()
else:
fanyi()
# 定义查词函数:
def chaci():
wd = e1.get()
# 如果是中文查询,转换中文为URL编码格式,中文unicode编码范围。
if '\u4e00' <= wd <= '\u9fff':
# 转中文为URL编码
wd = quote(wd)
url = cbase + wd
q = request.Request(url=url, headers=headers)
r = request.urlopen(q, timeout=2)
html = etree.HTML(r.read().decode('utf-8'))
# 查中文时的xpath和英文的xpath不同
result = html.xpath('/html/body/div[1]/div[2]/div[1]/div[2]/div[2]/div[1]/div/ul/p/span/a/text()')
else:
url = ebase + wd
q = request.Request(url=url, headers=headers)
r = request.urlopen(q, timeout=2)
html = etree.HTML(r.read().decode('utf-8'))
# 查英文词的xpath
result = html.xpath('/html/body/div[1]/div[2]/div[1]/div[2]/div[2]/div[1]/div/ul/li/text()')
# 清理text,重新显示
text1.delete(1.0, END)
# 如果有结果,显示结果
if len(result) != 0:
for pt in result:
text1.insert(INSERT, pt + '\n')
else:
# 如果没有结果,提示没有结果
text1.insert(INSERT, 'There is no explaination.')
# 翻译
def fanyi():
# 清除文本框中的内容
text1.delete(1.0, END)
# 获取输入框的字,复制给变量wd,装入字典
wd = e1.get()
dic['i'] = wd
data = bytes(parse.urlencode(dic), encoding='utf-8')
# 开始翻译
q = request.Request(url=trans, data=data, headers=headers, method='POST')
r = request.urlopen(q)
# 得到的结果是json数据,
result = json.loads(r.read().decode('utf-8'))
# 数据处理后放入输入框
text1.insert(INSERT, result['translateResult'][0][0]['tgt'])
if __name__ == '__main__':
# 主函数,定义一个tk对象
root = Tk()
root.title("Perigul's En-CN Dictionary 1.0 ")
#添加背景图片
image2 = Image.open(r'C:\Users\Mr.R\Pictures\22.jpg')
background_image = ImageTk.PhotoImage(image2)
w = background_image.width()
h = background_image.height()
root.geometry('%dx%d+0+0' % (w/2, h/2))
background_label = Label(root, image=background_image)
background_label.place(x=0, y=0, relwidth=0.8, relheight=0.6)
#加显示标签
L2 = Label(root, text='Hi Peridigul! Wish you happiness!', background='white')
L2.grid(row=6, column=2)
# 定义一个说明标签
l1 = Label(root, text='Input')
l1.grid(row=0, column=0)
# 定义一个输入框
e1 = Entry(root)
e1.grid(row=0, column=1, padx=5, pady=13)
# 定义一个按钮
bt1 = Button(root, text='Go', command=chaxun)
bt1.grid(row=0, column=2, padx=5, pady=1)
# 定义一个文本框
text1 = Text(root, width=50, height=13, bg='sky blue')
text1.grid(row=1, columnspan=4, padx=5, pady=37)
# 定义一个单选变量,初始值1,即默认是查词
v = IntVar()
v.set(1)
# 定义一个单选框
Radiobutton(root, text="Translate Just Phrases", variable=v, value=1).grid(row=0, column=3)
Radiobutton(root, text="Translate A Sentence", variable=v, value=2).grid(row=0, column=4)
# 循环窗体
mainloop()
-
设置背景色:
from urllib import request, parse
# 制作转化中文为URL格式
from urllib.parse import quote
# 分析库
from lxml import etree
# 用tkinter做图形化
from tkinter import *
# 解析json数据
import json
#pip install pillow
from PIL import ImageTk,Image
# 英文单词查询网址
ebase = 'http://dict.youdao.com/w/eng/'
# 中文单词查询网址
cbase = 'http://dict.youdao.com/w/'
# 翻译网址
trans = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
# 查询时http头
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0"}
# 翻译提交的字典
dic = {"doctype": "json"}
# 判断查询类型,1是查词,2是翻译。
def chaxun():
if v.get() == 1:
chaci()
else:
fanyi()
# 定义查词函数:
def chaci():
wd = e1.get()
# 如果是中文查询,转换中文为URL编码格式,中文unicode编码范围。
if '\u4e00' <= wd <= '\u9fff':
# 转中文为URL编码
wd = quote(wd)
url = cbase + wd
q = request.Request(url=url, headers=headers)
r = request.urlopen(q, timeout=2)
html = etree.HTML(r.read().decode('utf-8'))
# 查中文时的xpath和英文的xpath不同
result = html.xpath('/html/body/div[1]/div[2]/div[1]/div[2]/div[2]/div[1]/div/ul/p/span/a/text()')
else:
url = ebase + wd
q = request.Request(url=url, headers=headers)
r = request.urlopen(q, timeout=2)
html = etree.HTML(r.read().decode('utf-8'))
# 查英文词的xpath
result = html.xpath('/html/body/div[1]/div[2]/div[1]/div[2]/div[2]/div[1]/div/ul/li/text()')
# 清理text,重新显示
text1.delete(1.0, END)
# 如果有结果,显示结果
if len(result) != 0:
for pt in result:
text1.insert(INSERT, pt + '\n')
else:
# 如果没有结果,提示没有结果
text1.insert(INSERT, 'There is no explaination.')
# 翻译
def fanyi():
# 清除文本框中的内容
text1.delete(1.0, END)
# 获取输入框的字,复制给变量wd,装入字典
wd = e1.get()
dic['i'] = wd
data = bytes(parse.urlencode(dic), encoding='utf-8')
# 开始翻译
q = request.Request(url=trans, data=data, headers=headers, method='POST')
r = request.urlopen(q)
# 得到的结果是json数据,
result = json.loads(r.read().decode('utf-8'))
# 数据处理后放入输入框
text1.insert(INSERT, result['translateResult'][0][0]['tgt'])
if __name__ == '__main__':
# 主函数,定义一个tk对象
root = Tk()
root.title("Perigul's En-CN Dictionary 1.0 ")
root.configure(bg='pink')
#加显示标签
L2 = Label(root, text='Hi Peridigul! Wish you happiness!', background='white')
L2.grid(row=6, column=2)
# 定义一个说明标签
l1 = Label(root, text='Input')
l1.grid(row=0, column=0)
# 定义一个输入框
e1 = Entry(root)
e1.grid(row=0, column=1, padx=1, pady=5)
# 定义一个按钮
bt1 = Button(root, text='Go', command=chaxun)
bt1.grid(row=0, column=2, padx=5, pady=5)
# 定义一个文本框
text1 = Text(root, width=59, height=10, bg='lightblue')
text1.grid(row=1, columnspan=10, padx=5, pady=7)
# 定义一个单选变量,初始值1,即默认是查词
v = IntVar()
v.set(1)
# 定义一个单选框
Radiobutton(root, text="Translate Just Phrases", variable=v, value=1).grid(row=0, column=3)
Radiobutton(root, text="Translate A Sentence", variable=v, value=2).grid(row=0, column=4)
# 循环窗体
mainloop()
-
最终版:
from urllib import request, parse
# 制作转化中文为URL格式
from urllib.parse import quote
# 分析库
from lxml import etree
# 用tkinter做图形化
from tkinter import *
import tkinter.font as tf
# 解析json数据
import json
#pip install pillow
from PIL import ImageTk,Image
# 英文单词查询网址
ebase = 'http://dict.youdao.com/w/eng/'
# 中文单词查询网址
cbase = 'http://dict.youdao.com/w/'
# 翻译网址
trans = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
# 查询时http头
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0"}
# 翻译提交的字典
dic = {"doctype": "json"}
# 判断查询类型,1是查词,2是翻译。
def chaxun():
if v.get() == 1:
chaci()
else:
fanyi()
# 定义查词函数:
def chaci():
wd = e1.get()
# 如果是中文查询,转换中文为URL编码格式,中文unicode编码范围。
if '\u4e00' <= wd <= '\u9fff':
# 转中文为URL编码
wd = quote(wd)
url = cbase + wd
q = request.Request(url=url, headers=headers)
r = request.urlopen(q, timeout=2)
html = etree.HTML(r.read().decode('utf-8'))
# 查中文时的xpath和英文的xpath不同
result = html.xpath('/html/body/div[1]/div[2]/div[1]/div[2]/div[2]/div[1]/div/ul/p/span/a/text()')
else:
url = ebase + wd
q = request.Request(url=url, headers=headers)
r = request.urlopen(q, timeout=2)
html = etree.HTML(r.read().decode('utf-8'))
# 查英文词的xpath
result = html.xpath('/html/body/div[1]/div[2]/div[1]/div[2]/div[2]/div[1]/div/ul/li/text()')
# 清理text,重新显示
text1.delete(1.0, END)
# 如果有结果,显示结果
if len(result) != 0:
for pt in result:
text1.insert(INSERT, pt + '\n')
else:
# 如果没有结果,提示没有结果
text1.insert(INSERT, 'There is no input or no explanation for the target content.')
# 翻译
def fanyi():
# 清除文本框中的内容
text1.delete(1.0, END)
# 获取输入框的字,复制给变量wd,装入字典
wd = e1.get()
dic['i'] = wd
data = bytes(parse.urlencode(dic), encoding='utf-8')
# 开始翻译
q = request.Request(url=trans, data=data, headers=headers, method='POST')
r = request.urlopen(q)
# 得到的结果是json数据,
result = json.loads(r.read().decode('utf-8'))
# 数据处理后放入输入框
text1.insert(INSERT, result['translateResult'][0][0]['tgt'])
if __name__ == '__main__':
# 主函数,定义一个tk对象
root = Tk()
root.title("Perigul's EN-CN Dictionary 1.0 ")
root.configure(bg='pink')
#加显示标签
L2 = Label(root, text='Hi Peridigul! I Wish you happiness and good luck! O(∩_∩)O', background='pink')
L2.grid(row=6, column=2)
# 定义一个说明标签
l1 = Label(root, text='Input', bg='pink')
l1.grid(row=0, column=0)
# 定义一个输入框
e1 = Entry(root)
e1.grid(row=0, column=1, padx=5, pady=5)
# 定义一个按钮
bt1 = Button(root, text='Go', command=chaxun, bg='pink')
bt1.grid(row=0, column=2, padx=5, pady=5)
# 定义一个文本框
text1 = Text(root, width=59, height=10, bg='lightblue')
text1.grid(row=1, columnspan=10, padx=5, pady=7)
# 定义一个单选变量,初始值1,即默认是查词
v = IntVar()
v.set(1)
# 定义一个单选框
Radiobutton(root, text="Translate Just Phrases", variable=v, value=1, bg='pink').grid(row=0, column=3)
Radiobutton(root, text="Translate A Sentence", variable=v, value=2, bg='pink').grid(row=0, column=4)
# 循环窗体
mainloop()
得到: