usr bin python import pygtk pygtk require import gtk import gobject im

 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/python
import pygtk
pygtk.require("2.0")
import gtk
import gobject
import sys, os, re
import pango
import urllib2
import urllib
user_agent='Mozilla/5.0 (X11; U; Linux x86_64; ru; rv:1.9.0.4) Gecko/2008120916 Gentoo Firefox/3.0.4'
def g_trans(f,t,text):
url = 'http://translate.google.com/translate_a/t?client=t&text=%s&sl=%s&tl=%s' % ( urllib.quote(text),f,t )
req = urllib2.Request(url)
req.add_header('User-Agent',user_agent)
res = urllib2.urlopen(req).read()
try:
res=eval(res)
except:
pass
if isinstance(res,list):
return res[0]+'\n'+'\n'.join( [ i for i in res[1][0] if i!='noun'] )
return res
class App:
def __init__(self):
# - UI --------------------
self.win = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.win.resize(400,200)
self.win.move(200,100)
self.win.set_deletable(False)
self.win.set_title('Google Translate')
self.text_buf = gtk.TextBuffer()
self.text = gtk.TextView(self.text_buf)
self.text.modify_font( pango.FontDescription('sans normal 12' ) )
self.text.connect('key_press_event',self.text_key_press)
self.text.set_wrap_mode(gtk.WRAP_WORD)
self.win.add(self.text)
self.win.set_border_width(3)
# - menu ------------------
self.menu = gtk.Menu()
self.menu_quit = gtk.ImageMenuItem(gtk.STOCK_QUIT)
self.menu_quit.connect('activate', self.quit)
self.menu.append(self.menu_quit)
# - tray ------------------
self.tray = gtk.StatusIcon()
self.tray.set_from_stock(gtk.STOCK_REFRESH)
self.tray.set_tooltip('Google Translate')
self.tray.connect('activate',self.show_win)
self.tray.connect('popup-menu', self.popup, self.menu)
# - init ------------------
self.desktop = gtk.gdk.get_default_root_window()
def popup(self, widget, button, time, data = None):
if button==3 and data:
data.show_all()
data.popup(None, None, None, 3, time)
def quit(self,widget):
gtk.main_quit()
def show_win(self,widget):
if self.win.get_property('visible'):
self.win.hide()
else:
self.win.show_all()
def text_key_press(self,w,p):
if p.string=='\r':
text=self.text_buf.get_text(self.text_buf.get_start_iter(),self.text_buf.get_end_iter())
self.text_buf.set_text('')
self.text_buf.set_text(unicode( g_trans('en','ru',text)))
if __name__ == "__main__":
app = App()
gtk.main()