FreeTstar

loving Freedom + + loving Linux

  • 首页
  • Archive
  • 关于
  • 杂七杂八
  • 留言板
Twitter Sina RSS

pygtk+glade简单实现google translate桌面版

发布于 2011年03月13日 由 freetstar
26 条评论发表评论

打算毕业设计做个pyRssReader,用pygtk+glade+xml解析完成,先简单熟悉一下,参照这网上的一些文章和例子简单写了一个pyGoogleTranslator

截图

源码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pygtk
import gtk
import gtk.glade
import urllib
import urllib2
import threading
import re
import sys

class translation(threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self,name='translation')
        self.url="http://www.google.com/dictionary?langpair=%s|%s&q=%s&hl=en&aq=f"
        self.text=None
        self.trans_in=''
        self.trans_out=''
        self.text_in=None
        self.text_out=None

    def getText(self,widget):
        return widget.get_text()

    def setText(self,widget,text=''):
        widget.set_text(text)
        return True

    def run(self):
        self.setText(self.text_out,'正在翻译')
        self.data=(self.url % (self.trans_in,self.trans_out,self.getText(self.text_in)))
        request=urllib2.Request(self.data)
        conn=urllib2.urlopen(request)
        res=conn.read()
        res=re.findall('<meta name="description".*>',res)
        res=''.join(res)
        tmp1=re.compile('[<[^>].*:').sub("",res)
        tmp2=re.compile('-[^>].*>').sub("",tmp1)
        self.setText(self.text_out,tmp2)

class googleTrans():

    def __init__(self):
        self.flags=True
        self.ui_file='./pyGT.glade'
        self.wTree=gtk.glade.XML(self.ui_file,'window1')
        self.window = self.wTree.get_widget ("window1")
        self.button =self.wTree.get_widget("button1")
        self.window.set_default_size(400,400)
        self.window.set_position(gtk.WIN_POS_CENTER)
        dic={'on_window1_destroy':gtk.main_quit,\
             'on_zh2en_toggled':self.zh2en,\
             'on_en2zh_toggled':self.en2zh,\
             'on_button1_clicked':self.tran
            }
        self.wTree.signal_autoconnect(dic)
        self.window.show_all()

    def zh2en(self,widget):
        self.flags=False
    def en2zh(self,widget):
        self.flags=True

    def tran(self,widget):
        t=translation()
        t.text_in=self.wTree.get_widget('entry1')
        t.text_out=self.wTree.get_widget('entry2')
        if self.flags:
            t.trans_in='en'
            t.trans_out='zh-CN'
        else:
            t.trans_in='zh-CN'
            t.trans_out='en'
        t.setDaemon(True)
        t.start()
    def main(self):
        gtk.main()

if __name__=='__main__':
    gtk.gdk.threads_init()
    app=googleTrans()
    app.main()

为什么wordpress总爱把下划线给吃掉=_=
源码文件:here,glade文件:here

分类:PROGRAM
正则表达式资源收集
在centos5.5上配置nagios详细步骤

26 则回应给 “pygtk+glade简单实现google translate桌面版”

  1. 小叶 CHINA Google Chrome Windows 说道:
    2011年05月17日 10:15 上午

    布局很糟糕哦 :-? ,呵呵

    [回复]

    freetstar CHINA Google Chrome Linux 回复:
    五月 17th, 2011 at 11:53 上午

    我没有没有美感阿

    [回复]

  2. 夏天 CHINA Mozilla Firefox Windows 说道:
    2011年05月14日 12:58 上午

    pygtk支持自定义窗口界面吗?

    [回复]

    freetstar CHINA Google Chrome Linux 回复:
    五月 14th, 2011 at 1:05 上午

    哪种类型的窗口见面?

    [回复]

    freetstar CHINA Google Chrome Linux 回复:
    五月 14th, 2011 at 1:06 上午

    和普通的图形库一样,^_^自由度比较高的

    [回复]

  3. OSMSG CHINA Mozilla Firefox Linux 说道:
    2011年03月18日 5:11 下午

    c++纠结中……..

    [回复]

    freetstar CHINA Google Chrome Linux 回复:
    三月 18th, 2011 at 5:26 下午

    @OSMSG, 学好c++不容易。

    [回复]

  4. ubuntusoft Google Chrome Windows 说道:
    2011年03月14日 12:27 上午

    很牛叉啊 :roll:

    [回复]

    freetstar CHINA Google Chrome Linux 回复:
    三月 14th, 2011 at 8:59 上午

    @ubuntusoft, 一般,怎么开始用win了

    [回复]

    ubuntusoft Google Chrome Ubuntu Linux 回复:
    三月 14th, 2011 at 10:34 上午

    @freetstar, 不是我的电脑

    [回复]

  5. dorole Internet Explorer Windows 说道:
    2011年03月13日 11:53 下午

    折腾java的路过。。 :twisted:

    [回复]

    freetstar CHINA Google Chrome Linux 回复:
    三月 14th, 2011 at 9:02 上午

    @dorole, 共同折腾啊

    [回复]

  6. 婉秋 CHINA Mozilla Firefox Windows 说道:
    2011年03月13日 10:05 下午

    py的啊,呃,遗憾的是我只能看懂纯C写的GTK+程序 :?:

    [回复]

    freetstar CHINA Google Chrome Linux 回复:
    三月 14th, 2011 at 9:03 上午

    @婉秋, c写gtk貌似更复杂啊,难度大

    [回复]

    婉秋 AUSTRALIA Mozilla Firefox Ubuntu Linux 回复:
    三月 14th, 2011 at 9:25 上午

    @freetstar, 听说是更大,只是不懂py~~~

    [回复]

  7. SU ROOT AUSTRALIA Google Chrome Mac OS 说道:
    2011年03月13日 10:05 下午

    连长V5,不过界面“汗”这个改一下。。。考虑用JS,CSS,XML开发应用程序了吗?

    [回复]

    freetstar CHINA Google Chrome Linux 回复:
    三月 14th, 2011 at 9:05 上午

    @SU ROOT, apple?有钱人啊

    [回复]

    SU ROOT AUSTRALIA Google Chrome Mac OS 回复:
    三月 14th, 2011 at 8:43 下午

    @freetstar, 什么乱七八糟的,我说webos程序。

    [回复]

    freetstar CHINA Google Chrome Linux 回复:
    三月 14th, 2011 at 9:27 下午

    @SU ROOT, 什么乱78糟的,我说的是你啥时候有apple了

    [回复]

    SU ROOT AUSTRALIA Google Chrome Mac OS 回复:
    三月 15th, 2011 at 7:50 下午

    @freetstar, 不是我的,我妈的,不过我们家的电脑我用哪个都成,基本上都是我选的,哈哈。

    [回复]

    freetstar CHINA Google Chrome Linux 回复:
    三月 15th, 2011 at 7:51 下午

    @SU ROOT, 你个有钱人阿

    [回复]

    SU ROOT AUSTRALIA Google Chrome Mac OS 回复:
    三月 15th, 2011 at 7:53 下午

    @freetstar, 木钱,有毛线。。。

    [回复]

    freetstar CHINA Google Chrome Linux 回复:
    三月 15th, 2011 at 7:56 下午

    @SU ROOT, 哥鄙视有钱还说自己没钱的 :oops:

    [回复]

    SU ROOT AUSTRALIA Google Chrome Mac OS 回复:
    三月 15th, 2011 at 8:12 下午

    @freetstar, 哥鄙视有老婆不拿出照片的,哼~ :!:

    freetstar CHINA Google Chrome Linux 回复:
    三月 16th, 2011 at 12:49 下午

    回来让你见真人

  8. freetstar CHINA Google Chrome Ubuntu Linux 说道:
    2011年03月13日 9:59 下午

    若源码文件http://www.freetstar.com/uploads/pyGT.py无法取得
    请访问http://www.freetstar.com/uploads/pyGT即可

    sf不留

    [回复]

发表评论 取消回复

电子邮件地址不会被公开。 必填项已用 * 标注

*

*


疑问 开心 悲伤 邪恶 惊叹 微笑 脸红 笑 惊讶 惊奇 迷惑 酷 憨笑 生气 阴险 转眼球 眨眼 主意 箭头 中立 哭 大笑

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image

点击取消回复
第 1 页,共 1 页1
  • 欢迎订阅

  • Github

  • 近期评论

    • freetstar CHINA Mozilla Firefox Linux 发表在《留言板》
    • _Fly CHINA Google Chrome Linux 发表在《使用e4rat加速你的linux启动速度(ext4文件系统专用)》
    • walfred Mozilla Firefox Windows 发表在《留言板》
    • freetstar CHINA Mozilla Firefox Windows 发表在《用landslide创建html5 slide》
  • Spread Firefox Affiliate Button
  • 我的豆瓣

  • 链接表

    • 51开源社区
    • Adam's
    • delectate's blog
    • Don's
    • fl
    • ghostunix
    • Jasey Wang
    • Jerry's Blog
    • Moolight@Ericyue
    • OSMSG
    • panda
    • Pity is the Bomp
    • PT ubuntu
    • Tao Zhu
    • ubuntuhome
    • WOW!ubuntu
    • zhangzhfang
    • 丕字的blog
    • 傻东の学习笔记
    • 奶牛的blog
    • 孤风颠影
    • 弯柚博客
    • 熊猫党军委主席
    • 靖 轩
    • 马力哥
    • 黑孩儿
  • 我的支付宝

  • 功能

    • 登录
    • 文章 RSS
    • 评论 RSS
    • WordPress.org
© FreeTstar. WordPress 强力驱动 | Nest Theme by YChong