a2005147 发表于 2017-5-6 11:24:09

也发一个python小辞典(Qt版)

  看前面兄弟贴了一个,想起自己也写过一个,贴出来大家分享吧。
  这里用Pyqt做界面,辞典主要是用dictcn提供的xml的api,方便了很多。

#!/usr/bin/env python
# -*-encoding:utf-8 -*-
##############################
# author: yanckin#gmail.com
#2009.05.09 version 0.1
##############################
import sys
import urllib
from xml.etree.ElementTree import parse
from PyQt4.QtCore import *
from PyQt4.QtGui import *
LOOKUP_URL = "http://dict.cn/ws.php?utf8=true&q=%s"
class Form(QDialog):
def __init__(self,parent=None):
super(Form,self).__init__(parent)
self.browser = QTextBrowser()
self.lineedit = QLineEdit(u"输入单词,并按回车键")
self.lineedit.selectAll()
layout = QVBoxLayout()
layout.addWidget(self.lineedit)
layout.addWidget(self.browser)
self.setLayout(layout)
self.lineedit.setFocus()
self.connect(self.lineedit,SIGNAL("returnPressed()"),self.updateUi)
self.setWindowTitle(u"小译通")
def updateUi(self):
word = unicode(self.lineedit.text())
url = LOOKUP_URL % word
dictcn = parse(urllib.urlopen(url)).getroot()
if not dictcn.find('pron'):
word = '[%s]'%word
url = LOOKUP_URL % word
dictcn = parse(urllib.urlopen(url)).getroot()
self.browser.clear()
self.browser.append(u"<h3><font color=blue>您要找的是不是:\n\n</font></h2>" )
similarwords = ', '.join()
self.browser.append(u"<font color=red>%s\n\n</font>" %similarwords )
else:
pron = dictcn.find('pron').text
define = dictcn.find('def').text
self.browser.clear()
self.browser.append(u"<h2><font color=green>%s [%s]\n\n</font></h2>"%(word,pron))
self.browser.append(u"<h3><font color=blue>含义:\n\n</font></h3>" )
self.browser.append(u"<font color=red>%s\n\n</font>" %define )
self.browser.append(u"<h3><font color=blue>例句:\n\n</font></h3>" )
for i,sent in enumerate(dictcn.findall('sent')):
orig = sent.find('orig').text
trans = sent.find('trans').text
self.browser.append(u"<font color=#ff00ff>%s. %s</font>"%(i,orig))
self.browser.append(u"%s"%trans )
if __name__ == "__main__":
app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()
页: [1]
查看完整版本: 也发一个python小辞典(Qt版)