A simple English-Chinese Translation written in python

Source: Internet
Author: User

Note:

1. Install the pyqt Module

2. python IDLE is UTF-8 encoded by default.

3. Go to http://fanyi.youdao.com/openapi? Path = data-mode, apply for an api key

#-*-Coding: UTF-8-*-import sysfrom PyQt4 import QtGui, QtCoreimport urllibimport jsonfrom urllib import urlencodeclass LH_QWidget (QtGui. QWidget): def _ init _ (self): QtGui. QWidget. _ init _ (self) self. first = QtGui. QLineEdit (self) # Add a text box # label1 = QtGui. QLabel ('+', self) # self. second = QtGui. QLineEdit (self) # label2 = QtGui. QLabel ('=', self) self. third = QtGui. QLineEdit (self) # add another text box button = QtGui. QPushBut Ton ('Summit ', self) # Add a button grid = QtGui. QGridLayout () # layout grid. addWidget (self. first, 0, 0) # grid. addWidget (label1, 0, 1) # grid. addWidget (self. second, 0, 2) # grid. addWidget (label2, 0, 3) grid. addWidget (self. third, 1, 0) grid. addWidget (button, 2, 0) self. resize (300,150) self. setWindowTitle ("loster") self. setLayout (grid) self. connect (button, QtCore. SIGNAL ('clicked () '), chuli) # click monitoring, call the function chuli () def getfirst (self ): Return self. first. text () def getsecond (self): return self. second. text () def getthird (self, text): self. third. setText (text) def chuli (): url = 'HTTP: // fanyi.youdao.com/openapi.do? Keyfrom = 123 licheng & key = 1933182090 & type = data & doctype = json & version = 1.1 & q = 'X = LH_Widget.getfirst () # obtain the text to be translated # print x = unicode (x, 'utf-8 '). encode ('utf-8') # convert the input to UTF-8 encoded print unicode (x, 'utf-8 '). decode ('utf-8') result = urllib. urlopen (url + x ). read () # read jsonval = json. loads (result) # obtain the returned json structure print jsonval. values () print '------------- 'print jsonval. keys () print jsonval ["translation"] [0] LH_Widget.getthird (jsonval ["translation"] [0]) if _ name __= = "_ main __": app = QtGui. QApplication (sys. argv) LH_Widget = LH_QWidget () LH_Widget.show () sys.exit(app.exe c _())


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140307/20140307151324265.jpg" alt = "">


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.