I've been working on Paul Hudson's "PyGTK / Google Translate" tutorial over at tuxradar.com, and I wanted to turn the translator application into a script. (Paul only uses the command line in the tutorial.) After I ran the program, and worked out a couple of glitches, I got -- nothing! No window, no error message, nothing. Since the translator app worked fine from the command line, I know it's not a dependency issue. Is there something I need to add to the script to get it working, or am I missing something here?
Here is the code:
- Code: Select all
# Mega Translate - Google translation engine
import urllib
import simplejson
import gtk
def translate_clicked(btn):
# get two language codes & two texts, then send them to Google Translate
url = "http://ajax.googleapis.com/ajax/services/language/translate"
source = combofrom.get_active_text()
dest = comboto.get_active_text()
if source == "??":
source = ""
params['langpair'] = "%s|%s" % (source, dest)
params['q'] = textfrom.get_text()
data = urllib.urlencode(params)
response = urllib.urlopen(url, data)
json = simplejson.load(response)
textto.set_text(json['responseData']['translatedText'])
# create window
window = gtk.Window()
vbox = gtk.VBox()
# create two text entry boxes
hboxtop = gtk.HBox()
hboxbottom = gtk.HBox()
vbox.pack_start(hboxtop)
vbox.pack_start(hboxbottom)
window.add(vbox)
# create a 'Translate' button
translate = gtk.Button("Translate")
vbox.pack_start(translate)
# create two comboboxes
combofrom = gtk.combo_box_new_text()
comboto = gtk.combo_box_new_text()
textfrom = gtk.Entry()
textto = gtk.Entry()
textto.set_editable(False)
hboxtop.pack_start(combofrom)
hboxtop.pack_start(textfrom)
hboxbottom.pack_start(comboto)
hboxtop.pack_start(textto)
# add language codes to comboboxes
combofrom.append_text("??")
combofrom.append_text("en")
combofrom.append_text("fr")
combofrom.append_text("es")
combofrom.append_text("de")
comboto.append_text("en")
comboto.append_text("fr")
comboto.append_text("es")
comboto.append_text("de")
# connect 'Translate' button to 'translate_clicked' function
translate.connect('clicked', translate_clicked)
# display window
window.set_title("Mega Translate")
window.show_all()
Any help or advice would be appreciated. Thanks,
Chris Adams