Google Translate using pygtk + glade

Source: Internet
Author: User

 

 

Python is very fun .. It took 5 to 6 hours. Based on some examples on the internet, I wrote a Google Translate desktop version. The interface and functions are simple and can only be translated between Chinese and English. Mainly to learn Python

 

It is very convenient to use Glade on the interface. After painting, you only need to link the signal, and glade with pygtk is very convenient.

The source code of 100 lines

 

#! /Usr/bin/ENV python <br/> #-*-coding: UTF-8-*-<br/> Import pygtk <br/> Import GTK. glade <br/> Import urllib <br/> Import urllib2 <br/> Import threading <br/> Import Re <br/> Import sys <br/> class trans (threading. thread): <br/> def _ init _ (Self): <br/> threading. thread. _ init _ (self, name = 'Trans ') <br/> self. url = 'HTTP: // fanyi.cn.yahoo.com/translate_txt' <br/> self. TEXT = none <br/> self. trans_in = ''<br/> self. trans_out = ''<br/> self. text_out = none <br/> self. text_in = none <br/> def gettext (self, widget): <br/> Buf = widget. get_buffer () <br/> B, E = Buf. get_bounds () <br/> return Buf. get_text (B, E) <br/> def settext (self, widget, text = ''): <br/> Buf = widget. get_buffer () <br/> Buf. set_text (text) <br/> widget. set_buffer (BUF) <br/> def run (Self): <br/> self. settext (self. text_out, 'Translating ') <br/> values = {'ei': 'utf-8',/<br/> 'F ':'', /<br/> 'lp ':' % S _ % s' % (self. trans_in, self. trans_out),/<br/> 'trtext': '% s' % (self. gettext (self. text_in ). replace ('/N','/R/n/R'),)} <br/> DATA = urllib. urlencode (values) <br/> request = urllib2.request (self. URL, data) <br/> conn = urllib2.urlopen (request) <br/> res = Conn. read () <br/> res = Re. findall ('<Div id = "PD" class = "PD"> (. {1 ,}) </div> ', Res) <br/> Print res <br/> res = res [0]. replace ('<br/>', '/N') <br/> Print res <br/> res = res. replace ('<DNT> </DNT>', '/N') <br/> Print res <br/> self. settext (self. text_out, Res) <br/> class yahootrans (): <br/> def _ init _ (Self): <br/> self. ui_file = sys. path [0] + '/UI. glade '<br/> self. widgettree = GTK. glade. XML (self. ui_file, 'window1') <br/> DIC = {"on_exit_clicked": GTK. main_quit,/<br/> "on_window1_destroy": GTK. main_quit,/<br/> "on_to_zh_clicked": Self. tozh,/<br/> "on_to_en_clicked": Self. tozh} <br/> self. widgettree. signal_autoconnect (DIC) <br/> def tozh (self, widget): <br/> Print widget. get_name () <br/> T = trans () <br/> T. text_in = self. widgettree. get_widget ('text _ in') <br/> T. text_out = self. widgettree. get_widget ('text _ out') <br/> If widget. get_name () = 'to _ ZH ': <br/> T. trans_in = 'en' <br/> T. trans_out = 'zh '<br/> else: <br/> T. trans_in = 'zh '<br/> T. trans_out = 'en' <br/> T. setdaemon (true) <br/> T. start () <br/> def main (Self): <br/> GTK. main () <br/> If _ name __= = '_ main _': <br/> GTK. gdk. threads_init () <br/> APP = yahootrans () <br/> app. main () 

 

 

Yes, it seems that the file cannot be uploaded. Another Glade cannot be uploaded.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.