# -*- coding: utf-8 -*-

from Tkinter import *
import difflib
import urllib2
import urllib # python2.7才需要两个urllib
import json # ----------------------主框架部分---------------------- root = Tk()
root.title('翻译GUI&beta1')
root.geometry()
Label_root=Label(root) #-----------------------定义规则------------------------ def translate(content): url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null"
data = {} # 构造data,里面构造参数传入
data['type'] = 'AUTO'
data['i']=content
data['doctype'] = 'json'
data['xmlVersion'] = '1.8'
data['keyfrom'] = 'fanyi.web'
data['ue'] = 'UTF-8'
data['action'] = 'FY_BY_ENTER'
data['typoResult'] = 'true' data = urllib.urlencode(data).encode('utf-8') # 将构造的data编码
req = urllib2.Request(url) # 向浏览器发出请求
response = urllib2.urlopen(req, data) # 带参请求,返回执行结果
html = response.read().decode('utf-8')
# print(html) # 可以取消print的注释,查看其中效果,这边获取的结果是进行解析 target = json.loads(html) # 以json形式载入获取到的html字符串 #print u"翻译的内容是:"+target['translateResult'][0][0]['tgt']
return target['translateResult'][0][0]['tgt'].encode('utf-8') #还可以继续增加规则函数,只要是两输入的参数都可以
#----------------------触发函数----------------------- def Answ():# 规则函数 Ans.insert(END,"翻译 %s: "%var_first.get().encode('utf-8') + translate(var_first.get().encode('utf-8'))) def Clea():#清空函数
input_num_first.delete(0,END)#这里entry的delect用0
Ans.delete(0,END)#text中的用0.0 #----------------------输入选择框架--------------------
frame_input = Frame(root)
Label_input=Label(frame_input, text='请输入需要翻译的内容', font=('',15))
var_first = StringVar()
input_num_first = Entry(frame_input, textvariable=var_first) #---------------------计算结果框架---------------------
frame_output = Frame(root)
Label_output=Label(frame_output, font=('',15))
Ans = Listbox(frame_output, height=5,width=30) #text也可以,Listbox好处在于换行 #-----------------------Button----------------------- calc = Button(frame_output,text='翻译', command=Answ)
cle = Button(frame_output,text='清空', command=Clea) Label_root.pack(side=TOP)
frame_input.pack(side=TOP)
Label_input.pack(side=LEFT) input_num_first.pack(side=LEFT) frame_output.pack(side=TOP)
Label_output.pack(side=LEFT)
calc.pack(side=LEFT)
cle.pack(side=LEFT)
Ans.pack(side=LEFT) #-------------------root.mainloop()------------------ root.mainloop()

python写的翻译代码的更多相关文章

  1. python写一个翻译的小脚本

    起因: 想着上学看不懂English的PDF感慨万分........ 然后就有了翻译的脚本. 截图: 代码: #-*- coding:'utf-8' -*- import requests impor ...

  2. 使用python写的一个代码统计程序

    # encoding="utf-8" """ 统计代码行数 """ import sys import os def c ...

  3. Python初学者随笔(一)_ 用Python写的第一个游戏“猜数字”

    如标题所写,这篇随笔主要记录下学习Python过程中用Python写的第一个游戏--"猜数字"_跟着"小甲鱼"学Python,链接: https://b23.t ...

  4. 用python写一个百度翻译

    运行环境: python 3.6.0 今天处于练习的目的,就用 python 写了一个百度翻译,是如何做到的呢,其实呢就是拿到接口,通过这个接口去访问,不过中间确实是出现了点问题,不过都解决掉了 先晾 ...

  5. 用Python写一个将Python2代码转换成Python3代码的批处理工具

    之前写过一篇如何在windows操作系统上给.py文件添加一个快速处理的右键功能的文章:<一键将Python2代码自动转化为Python3>,作用就是为了将Python2的文件升级转换成P ...

  6. 十行代码--用python写一个USB病毒 (知乎 DeepWeaver)

    昨天在上厕所的时候突发奇想,当你把usb插进去的时候,能不能自动执行usb上的程序.查了一下,发现只有windows上可以,具体的大家也可以搜索(搜索关键词usb autorun)到.但是,如果我想, ...

  7. Python之美[从菜鸟到高手]--一步一步动手给Python写扩展(异常处理和引用计数)

    我们将继续一步一步动手给Python写扩展,通过上一篇我们学习了如何写扩展,本篇将介绍一些高级话题,如异常,引用计数问题等.强烈建议先看上一篇,Python之美[从菜鸟到高手]--一步一步动手给Pyt ...

  8. Python写爬虫-爬甘农大学校新闻

    Python写网络爬虫(一) 关于Python: 学过C. 学过C++. 最后还是学Java来吃饭. 一直在Java的小世界里混迹. 有句话说: "Life is short, you ne ...

  9. 用Python写Verilog(非HLS)

    https://blog.csdn.net/qq_32010099/article/details/81197171 前段时间玩Python的时候好奇, 既然Python这么强大, 那么能不能用Pyt ...

随机推荐

  1. ajax执行成功不进入success方法

    当dataType的值为json时,传入的值和返回的值符合json格式的时候,执行成功才会进入success方法,否则进入error方法.

  2. unity 动态更新模型透明度

    RaycastHit[] hits; Vector3 normal = transform.position - target.position; hits = Physics.RaycastAll( ...

  3. java对象之----(PO,VO,DAO,BO,POJO)

    转自http://www.cnblogs.com/bluestorm/archive/2012/09/26/2703234.html 一.PO :(persistant object ),持久对象 可 ...

  4. PowerDesigner反向生成PDM和name与注释互换

    Option Explicit ValidationMode = True InteractiveMode = im_Batch Dim mdl 'the current model 'get the ...

  5. yarn 常用命令

    1.安装 yarn npm install yarn -g 2.卸载yarn npm uninstall yarn -g

  6. delphi 7里怎么隐藏PageControl控件的tabsheet标签

    Tabsheet1.tabvisible := False;

  7. java添加菜单项目

  8. SpringSecurity入门demo

    配置依赖: <properties> <spring.version>4.2.4.RELEASE</spring.version> </properties& ...

  9. java之路 把1到100之间的数的偶数相加

    /** *把1到100之间的数的偶数相加 */ class Demo{ public static void main(String[] args){ int i =1; int sum = 0; d ...

  10. 如何创建并初始化程序集里List类型的反射

    参考网址:http://stackoverflow.com/questions/315231/using-reflection-to-set-a-property-with-a-type-of-lis ...