Tag:linux Project Experimental building python
Importosimportreimporttimeimportfcntlimportloggingimportpygtkpygtk.require (' 2.0 ') Importgtkimportgobjectimportwebkitimportrequestsimportjson 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) classdict: def __init__ (self): self.mouse_in =false self.popuptime =0 self.last_selection = ' # Initialize window self.Window =gtk. Window (GTK. Window_popup) self.window.set_title ("Pyoudao") self.window.set_border_width (3) self.window.connect ("Destroy", lambdaw: gtk.main_quit ()) self.window.resize (360, 200) # Initializes the 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) # The registration cycle function _on_timer, executes gobject.timeout_add (500, self) every 500 milliseconds. _on_timer,eventbox) eventbox.show () # Create a Webview self.view =webkit. WebView () def title_changed (widget, frame,title): logging.debug (' title_changedto %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 notself.mouse_in: x, y = self.window.get_position () &NBSP;&NBsp; 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 bigenough, hide window ') self.window.hide (); if ( Time.time () -self.popuptime >3): logging.debug (' Time longenough, 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) >20): return False if (Not text) or (text == self.last_selection): return False logging.info ("======== selectedstring : % S " % text) self.last_ selection =text m = re.search (R ' [a-za-z-]+ ', text.encode (' UTF8 ')) if not m: logging.info ("querynothing") return False word&nbSp;= 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 # Search for Words 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 injs: 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 ( [' <ahref= " Javascript:void (0); " >%s</a>: %s '% (i[' key '], ' '. Join (i[' value ')) for iin 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)