之所以选择PyQt或PySide来开发图形界面,主要是因为Python和Qt的跨平台特性,Qt5甚至支持iOS和Android,并且开发相同的软件,Python的效率是极高的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/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.exec_())