pygtk 字典

在读电子书的时候,双击又可以找出单词,这样子比较方便,就像金山自动取词一样,最近要看好多书,这个只能写的了草一点了,没怎么优化,代码都不好读

先给图然后在copy代码

还有很多的bug不过没有时间了,boss要求学习c 

 

下面附上python脚本

Dictionary
import gtk,gobject,urllib
import xml.etree.ElementTree as ET
class Dictionary:
    def getXML(self,word):
        result=""
        url="http://dict-co.iciba.com/api/dictionary.php?w="+word.lower()
        data=urllib.urlopen(url).read();
        tree=ET.fromstring(data)
        for i in tree.iter("acceptation"):
            result=result+i.text
        print result.replace("\n","")
        return result
    def get_stringtarget(self,widget):
        ret=widget.selection_convert("PRIMARY","STRING");
        return True;
    def selection_received(self,widget,selection_data,data):
        text=selection_data.get_text()
        self.label.set_text(self.getXML(text))
    def helloworld(self):
        self.button.emit("clicked")
        return True
    def __init__(self):
        window=gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.set_keep_above(1)
        window.set_decorated(False)
        window.resize(500,30)
        window.show()
        self.label=gtk.Label("GetIn")
        vbox=gtk.VBox()
        window.add(vbox)
        self.button=gtk.Button("Get String Target")
        eventbox=gtk.EventBox()
        self.button.connect_object("clicked",self.get_stringtarget,eventbox)
        eventbox.connect("selection_received",self.selection_received)
        vbox.pack_start(eventbox)
        vbox.pack_start(self.label)
        window.show_all()
        gobject.timeout_add(1000,self.helloworld)
def main():
    gtk.main()
if __name__=="__main__":
    dictionary=Dictionary()
    main()
    

 

主要没时间,写出这样的代码都对不起自己,看着难过

 

posted @ 2012-12-21 14:28  Epirus  阅读(192)  评论(0编辑  收藏  举报