| View previous topic :: View next topic |
| Author |
Message |
chriswadams
Joined: Tue Dec 01, 2009 12:54 am Posts: 26
|
Posted: Sat Nov 27, 2010 12:37 am Post subject: PyGTK / Google Translate tutorial -- nothing happens! |
|
|
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: |
# 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 |
|
| Back to top |
|
 |
larcky
Joined: Sun Nov 21, 2010 6:28 pm Posts: 19 Location: England
|
Posted: Fri Dec 03, 2010 1:06 pm Post subject: |
|
|
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 Can't see that anywhere in your example.
Regards, larcky |
|
| Back to top |
|
 |
chriswadams
Joined: Tue Dec 01, 2009 12:54 am Posts: 26
|
Posted: Fri Dec 10, 2010 6:13 am Post subject: |
|
|
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. |
|
| Back to top |
|
 |
larcky
Joined: Sun Nov 21, 2010 6:28 pm Posts: 19 Location: England
|
Posted: Fri Dec 10, 2010 5:28 pm Post subject: |
|
|
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
Here's a screenshot:
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 |
|
| Back to top |
|
 |
towy71 Moderator

Joined: Wed Apr 06, 2005 3:11 pm Posts: 4176 Location: wild West Wales
|
Posted: Fri Dec 10, 2010 6:22 pm Post subject: |
|
|
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 |
|
| Back to top |
|
 |
larcky
Joined: Sun Nov 21, 2010 6:28 pm Posts: 19 Location: England
|
Posted: Fri Dec 10, 2010 7:14 pm Post subject: |
|
|
| Sorry: fixed! |
|
| Back to top |
|
 |
chriswadams
Joined: Tue Dec 01, 2009 12:54 am Posts: 26
|
Posted: Fri Dec 10, 2010 8:45 pm Post subject: |
|
|
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(' ')
Thanks for your help larcky!
Chris Adams. |
|
| Back to top |
|
 |
larcky
Joined: Sun Nov 21, 2010 6:28 pm Posts: 19 Location: England
|
Posted: Sat Dec 11, 2010 7:20 am Post subject: |
|
|
"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
Welcome to the maddening world of programming, Chris! |
|
| Back to top |
|
 |
chriswadams
Joined: Tue Dec 01, 2009 12:54 am Posts: 26
|
Posted: Mon Dec 13, 2010 2:35 am Post subject: |
|
|
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(' ')
And thanks again for your help, larcky.
Chris Adams. |
|
| Back to top |
|
 |
| View previous topic :: View next topic |
|