Copy codeThe Code is as follows:
#! /Usr/bin/env python
Import sys
From PyQt4 import QtGui, QtCore
Import httplib
From urllib import urlencode
Import re
Def out (text ):
P = re. compile (R '","')
M = p. split (text)
Result = unicode (m [0] [4:]. decode ('utf-8 '))
DS_Widget.setDS_TextEdit_text (result)
Def dic ():
Word = DS_Widget.getDS_LineEdit_text ()
Text = urlencode ({'text': word })
H = httplib. HTTP ('translate .google.cn ')
H. putrequest ('get', '/translate_a/t? Client = t & hl = zh-CN & sl = en & tl = zh-CN & ie = UTF-8 & oe = UTF-8 & '+ text)
H. endheaders ()
H. getreply ()
F = h. getfile ()
Lines = f. readlines ()
Out (lines [0])
F. close ()
Class DS_QWidget (QtGui. QWidget ):
Def _ init _ (self ):
QtGui. QWidget. _ init _ (self)
Self. DS_LineEdit = QtGui. QLineEdit (self)
DS_SearchButton = QtGui. QPushButton ('search', self)
Self. DS_TextEdit = QtGui. QTextEdit (self)
Hbox = QtGui. QHBoxLayout ()
Hbox. addWidget (self. DS_LineEdit)
Hbox. addWidget (DS_SearchButton)
Vbox = QtGui. QVBoxLayout (self)
Vbox. addLayout (hbox)
Vbox. addWidget (self. DS_TextEdit)
Self. resize (500,300)
Self. setWindowTitle ('dictionary ')
Self. connect (DS_SearchButton, QtCore. SIGNAL ('clicked () '), dic)
Self. setLayout (vbox)
Def getDS_LineEdit_text (self ):
Return self. DS_LineEdit.text ()
Def setDS_TextEdit_text (self, text ):
Self. DS_TextEdit.setText (text)
If _ name __= = "_ main __":
DS_APP = QtGui. QApplication (sys. argv)
DS_Widget = DS_QWidget ()
DS_Widget.show ()
Sys.exit(DS_APP.exe c _())