欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

pyqt和pyside开发图形化界面

程序员文章站 2022-08-14 08:12:20
复制代码 代码如下:#!/usr/bin/env pythonimport sysfrom pyqt4 import qtgui,qtcoreimport httplibf...

复制代码 代码如下:

#!/usr/bin/env python
import sys
from pyqt4 import qtgui,qtcore
import httplib
from urllib import urlencode
import re

def out(text):
    p = re.compile(r'","')
    m = p.split(text)
    result=unicode(m[0][4:].decode('utf-8'))
    ds_widget.setds_textedit_text(result)

def dic():
    word=ds_widget.getds_lineedit_text()
    text=urlencode({'text':word})
    h=httplib.http('translate.google.cn')
    h.putrequest('get', '/translate_a/t?client=t&hl=zh-cn&sl=en&tl=zh-cn&ie=utf-8&oe=utf-8&'+text)
    h.endheaders()
    h.getreply()
    f = h.getfile()
    lines = f.readlines()
    out(lines[0])
    f.close()

class ds_qwidget(qtgui.qwidget):
    def __init__(self):
        qtgui.qwidget.__init__(self)

        self.ds_lineedit = qtgui.qlineedit(self) 
        ds_searchbutton=qtgui.qpushbutton('search',self)
        self.ds_textedit = qtgui.qtextedit(self)

        hbox = qtgui.qhboxlayout()
        hbox.addwidget(self.ds_lineedit)
        hbox.addwidget(ds_searchbutton)

        vbox = qtgui.qvboxlayout(self)
        vbox.addlayout(hbox)
        vbox.addwidget(self.ds_textedit)

        self.resize(500, 300)
        self.setwindowtitle('dictionary')
        self.connect(ds_searchbutton, qtcore.signal('clicked()'),dic)
        self.setlayout(vbox)

    def getds_lineedit_text(self):
        return self.ds_lineedit.text()
    def setds_textedit_text(self,text):
        self.ds_textedit.settext(text)

if __name__=="__main__":
    ds_app = qtgui.qapplication(sys.argv)
    ds_widget = ds_qwidget()
    ds_widget.show()
    sys.exit(ds_app.exec_())