Study notes-Lab Building project Class (Linux desktop dictionary)

Source: Internet
Author: User
Tags gtk

Import osimport reimport timeimport fcntlimport loggingimport pygtkpygtk.require (' 2.0 ') import gtkimport gobjectimport webkitimport requestsimport json   Home = os.getenv ("HOME")  +  '/.youdao-dict/' log = home +  '/pyoudao.log ' lock = home +   '/pyoudao.lock ' query_url =  ' http://fanyi.youdao.com/ Openapi.do?keyfrom=tinxing&key=1312427901&type=data&doctype=json&version=1.1&q= '  if  not os.path.exists (Home):     os.mkdir (Home)  logging.basicconfig (filename= Log, level=logging. DEBUG)  class dict:    def __init__ (self):         self.mouse_in = False         self.popuptime = 0        self.last_selection =  "          #  Initialization Window         self.window  = gtk. Window (GTK. Window_popup)         self.window.set_title ("Pyoudao")          self.window.set_border_width (3)          self.window.connect ("Destroy",  lambda w: gtk.main_quit ())          self.window.resize (360, 200)          #   Initialize vertical container         vbox = gtk. VBox (false, 0)         vbox.show ()           #  Create an event container,  and register selection_recevied event functions          eventbox = gtk. Eventbox ()         eventbox.connect ("selection_received", self._on_selection_received)         eventbox.connect (' Enter-notify-event ',  self._on_mouse_enter)          Eventbox.connect (' leave-notify-event ',  self._on_mouse_leave)           #  Registration cycle function _on_timer, executed once every 500 milliseconds         gobject.timeout_ Add (500, self._on_timer, eventbox)         eventbox.show ()          #  Create a webview         self.view = webkit. WebView ()         def title_changed (widget, frame, title ):             logging.debug (' Title_changed to  %s, will open webbrowser  '  % title              import webbrowser             webbrowser.open (' http://dict.youdao.com/search?le=eng&q= '  + title )          self.view.connect (' title-changed ',  title_changed)          self.view.show ()          #  Packaging various controls         self.window.add (VBox)          vbox.pack_start (Eventbox)         eventbox.add ( Self.view)      def _on_timer (self, widget):          #  Start Check selection event         widget.selection_ Convert ("PRIMARY",  "STRING")          if self.window.get _property (' Visible ')  and not self.mouse_in:            x, y =  self.window.get_position ()             px,  py, mods = self.window.get_screen (). Get_root_window (). Get_pointer ()              if  (px-x) * (px-x)  +  (py-y) * (py-y)   > 400:                 Logging.debug (' Distance big enough, hide window ')                  self.window.hide ();             if (Time.time ()  - self.popuptime > 3):                 logging.debug (' Time  long enough, hIde window ')                  self.window.hide ();         return true      #  If a string is selected, execute the function     def _on_selection_received (self,  Widget, selection_data, data):         if str (Selection_ Data.type)  ==  "STRING":             text  = selection_data.get_text ()              if not text:                 return false            text  = text.decode (' Raw-unicode-escape ')              if (len (text) &NBSP;&GT;&NBSP;20):                 return false             if  (not  text)  or  (text == self.last_selection):                 return False              logging.info ("======== selected string : %s"  % text)             self.last_selection  = text             m =  Re.search (R ' [a-za-z-]+ ',  text.encode (' UTF8 '))              if not m:                 logging.inFo ("query nothing")                  return False              Word = m.group (0). Lower ()             if  self.ignore (word):                 logging.info (' ignore word:  '  + word)                  return False              logging.info (' queryword:  '  + word)              self.query_word (word)           return False     #  Query Word     def  query_word (self, Word):        query_url = query_url + word         #  using the requests module to get the JSON string          js= json.loads (Requests.get (query_url). Text)          if  ' Basic '  not in js:             logging.info (' ignoreword:  '  + word)              return         x, y, mods =  self.window.get_screen (). Get_root_window (). Get_pointer ()          Self.window.move (X+15,&NBSP;Y+10)          self.window.present ()          translation =  ' <br/> '. Join (js[' Translation '])         if  ' phonetic '  in js[' basic ']:             phonetic = js[' basic ' [' Phonetic ']         else:            phonetic =   '         explains =  ' <br/> ' Join (js[' basic ' [') Explains '])         web =  ' <br/> ' Join ( [' <a  href= "javascript:void (0);" >%s</a>: %s '% (i[' key '],  '   '. Join (i[' value '))  for i in js[' web '] [ :3] ] )         html =  ' <style>.add_to_ Wordbook {    background: url (http://bs.baidu.com/yanglin/add.png)   no-repeat;    vertical-align: middle;    overflow: hidden;    display: inline-block;    vertical-align: top;     width: 24px;    padding-top: 26px;     Height: 0;    margin-left: .5em;} </style>          


Study notes-Lab Building project Class (Linux desktop dictionary)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.