70行代码- 桌面翻译组件
本帖最后由 HZX0229 于 2021-2-23 11:10 编辑代码如下
import tkinter as tk
from fanyi import get_result
root = tk.Tk()
root.geometry("200x80+1680+910")
root.overrideredirect(True)
root.attributes('-alpha', 0.4)
image = tk.PhotoImage(file = 'sucai/8.png')
thelabel = tk.Label(root, image = image, justify = 'left', compound = 'center')
thelabel.place(relx = 0, rely = 0)
entry = tk.Entry(root, bg = 'white', bd = 5, fg = 'purple',
relief = 'sunken', highlightcolor = 'red', width = 22)
entry.place(relx = 0, rely = 0)
text = tk.Text(root, width = 22, height = 5, bd = 5, relief = 'sunken',
fg = 'purple')
text.place(relx = 0, rely = 0.38)
def callback(event):
text.insert('end', get_result(entry.get()))
root.bind('<Return>', callback)
def quit(event):
root.quit()
root.bind('<Escape>', quit)
def delete(event):
entry.delete(0, 'end')
text.delete(1.0, 'end')
root.bind('<Tab>', delete)
root.mainloop()
核心代码:
import urllib.request
import urllib.parse
import json
import random
def get_result(word):
url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
head = {}
head['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'
date = {}
date['i'] = str(word)
date['from'] = 'AUTO'
date['to'] = 'AUTO'
date['smartresult'] = 'dict'
date['client'] = 'fanyideskweb'
date['salt'] = '16098548777597'
date['sign'] = 'b5005a98f5487935fa6aa11c2fed0760'
date['lts'] = '1609854877759'
date['bv'] = 'b396e111b686137a6ec711ea651ad37c'
date['doctype'] = 'json'
date['version'] = '2.1'
date['keyfrom'] = 'fanyi.web'
date['action'] = 'FY_BY_CLICKBUTTION'
iplist = ['120.28.218.28:3128','110.78.175.145:8080','171.35.167.172:9999','175.42.68.37:9999','182.163.120.84:55443']
proxy_support = urllib.request.ProxyHandler({'https':random.choice(iplist)})
opener = urllib.request.build_opener(proxy_support)
urllib.request.install_opener(opener)
date = urllib.parse.urlencode(date).encode('utf-8')
req = urllib.request.Request(url,date,head)
response = urllib.request.urlopen(req)
html = response.read().decode('utf-8')
target = json.loads(html)
result = target['translateResult']['tgt']
return result
if __name__ == '__main__':
date = 'love'
result = get_result(date)
print(result)
打包文件
效果如图右下角
如果有alpha浮动字符提示就更好了 为什么下载后windows提示有病毒??{:10_243:} Minecraft程序猿 发表于 2021-2-23 13:57
如果有alpha浮动字符提示就更好了
添加一个text文件,保存参数,运行之前读取就行 allearn_Python 发表于 2021-2-23 14:30
为什么下载后windows提示有病毒??
是360的事吧... HZX0229 发表于 2021-2-23 14:56
添加一个text文件,保存参数,运行之前读取就行
我是说在上面加一个半透明的提示,比如"输入语句以翻译..." Minecraft程序猿 发表于 2021-2-23 16:34
我是说在上面加一个半透明的提示,比如"输入语句以翻译..."
在上面加个label ,就很难看了,在entry这里插入又不方便操作
还是简洁好点, tkinter在这方面不太行 HZX0229 发表于 2021-2-23 16:58
在上面加个label ,就很难看了,在entry这里插入又不方便操作
还是简洁好点, tkinter在这方面不太行
你可以用半透明图片的 Minecraft程序猿 发表于 2021-2-23 16:34
是360的事吧...
不是360啊,win10自带的防火墙隔离了 allearn_Python 发表于 2021-2-25 09:22
不是360啊,win10自带的防火墙隔离了
让它运行就好了,不必担心它,把防火墙设置允许运行
页:
[1]