PyGTK / Google Translate tutorial -- nothing happens!

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

PyGTK / Google Translate tutorial -- nothing happens!

Postby chriswadams » Sat Nov 27, 2010 12:37 am

Hi all --

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
User avatar
chriswadams
 
Posts: 27
Joined: Tue Dec 01, 2009 12:54 am

Postby larcky » Fri Dec 03, 2010 1:06 pm

Hi Chris
Did you solve this?
I've only programmed GTK with gtkmm (the C++ wrapper), but AFAIK you set things off in Python with a call to
Code: Select all
gtk.main()
Can't see that anywhere in your example.
Regards, larcky
larcky
 
Posts: 19
Joined: Sun Nov 21, 2010 6:28 pm
Location: England

Postby chriswadams » Fri Dec 10, 2010 6:13 am

Hi larcky --

Thanks for the suggestion. I tried it, but it didn't work. I'm pretty much at a loss to explain it.

Chris A.
User avatar
chriswadams
 
Posts: 27
Joined: Tue Dec 01, 2009 12:54 am

Postby larcky » Fri Dec 10, 2010 5:28 pm

Hi Chris
What didn't work? I've just copied and pasted your code into gedit, added gtk.main() to the end, saved it and run it and a GUI popped up :D
Here's a screenshot:
Image
You did put that extra line right at the very end? That's how you enter the GTK event loop.
See you, larcky
Last edited by larcky on Sat Dec 11, 2010 7:11 am, edited 2 times in total.
larcky
 
Posts: 19
Joined: Sun Nov 21, 2010 6:28 pm
Location: England

Postby towy71 » Fri Dec 10, 2010 6:22 pm

larcky
could you please crop the picture and reduce it, that will enable people with smaller screens appreciate it more ;)
still looking for that door into summer
User avatar
towy71
Moderator
 
Posts: 4269
Joined: Wed Apr 06, 2005 2:11 pm
Location: wild West Wales

Postby larcky » Fri Dec 10, 2010 7:14 pm

Sorry: fixed!
larcky
 
Posts: 19
Joined: Sun Nov 21, 2010 6:28 pm
Location: England

Postby chriswadams » Fri Dec 10, 2010 8:45 pm

Unbelievable. I had put the "gtk.main()" command in various places, not thinking to put it at the very end. Of course, it worked once I put it in the right place. Man, I feel stupid. javascript:emoticon(':oops:')

Thanks for your help larcky!
Chris Adams.
User avatar
chriswadams
 
Posts: 27
Joined: Tue Dec 01, 2009 12:54 am

Postby larcky » Sat Dec 11, 2010 7:20 am

"The way to succeed is to double your failure rate." - Thomas J. Watson, founder of IBM.
By that rule I should now be CEO of Sony Corporation :roll:
Welcome to the maddening world of programming, Chris!
larcky
 
Posts: 19
Joined: Sun Nov 21, 2010 6:28 pm
Location: England

Postby chriswadams » Mon Dec 13, 2010 2:35 am

Hey all --

After fussing with the code some more, I finally got Mega Translate working as a PyGTK project, and I thought everyone would be interested in seeing the finished code:


[code]
#!/usr/bin/env python
# Mega Translate - Google translation engine
# based on a video by Paul Hudson at www.tuxradar.com

import pygtk
pygtk.require('2.0')
import urllib
import simplejson
import gtk

def translate_clicked(btn):
# get two language codes & text, then send them to Google Translate
url = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0"
source = combofrom.get_active_text()
if source == "??"
source = "auto"
target = comboto.get_active_text()

params = {}
params['q'] = textfrom.get_text()
params['langpair'] = source + "|" + target

# if you want an API key, register with Google, then plug it in to the
# following line, and uncomment
# params['key'] = "YOUR-KEY-HERE"

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()
window.add(vbox)

# create two comboboxes & two text entry boxes
hboxtop = gtk.HBox()
combofrom = gtk.combo_box_new_text()
hboxtop.pack_start(combofrom)
textfrom = gtk.Entry()
hboxtop.pack_start(textfrom)
vbox.pack_start(hboxtop)

hboxbottom = gtk.HBox()
comboto = gtk.combo_box_new_text()
hboxbottom.pack_start(comboto)
textto = gtk.Entry()
textto.set_editable(False)
hboxbottom.pack_start(textto)
vbox.pack_start(hboxbottom)

# create a 'Translate' button
translate = gtk.Button("Translate")
vbox.pack_start(translate)

# 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()
gtk.main()[/code]

Note that this version of the program differs a little from the previous version I posted. I tweaked the setup of the input boxes, and I had to change the "translate_clicked" function slightly, but it works. Also, Paul Hudson's version of Mega Translate didn't need an API key, but Google encourages you to use one, so I left space to plug it in.

larcky wrote:
[quote]Welcome to the maddening world of programming, Chris![/quote]
Thanks! Glad to be here! javascript:emoticon(':D')
And thanks again for your help, larcky.
Chris Adams.
User avatar
chriswadams
 
Posts: 27
Joined: Tue Dec 01, 2009 12:54 am


Return to Programming

Who is online

Users browsing this forum: Yahoo [Bot] and 0 guests

cron