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 = "">