pyqt和pyside开发图形化界面

yipeiwu_com6年前Python基础

复制代码 代码如下:

#!/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_())

相关文章

python文档字符串(函数使用说明)使用详解

python文档字符串(函数使用说明)使用详解

1.效果图: 2.代码: # 文档字符串( doc str) 是 函数使用说明 # 用法: 在函数第一行写一个字符串 def fn(*nums): ''' 函数的作用: 计...

解决pycharm py文件运行后停止按钮变成了灰色的问题

解决pycharm py文件运行后停止按钮变成了灰色的问题

这两天被这个问题折磨得要死,把pycharm卸载了还是没解决,后来终于在一篇博客中看见,然后终于解决了 问题界面如下: 1. 每次运行后都会跳出一个 python console,并且...

python的socket编程入门

Flask或者其他框架都是封装的比较完善,我们可以不去关注路由、SESSION等到底是怎么实现的,现在我们使用socket来实现一个带有注册、登录功能的To do网站,这样能对后端框架了...

Python3中urlencode和urldecode的用法详解

在Python3中,将中文进行urlencode编码使用函数 urllib.parse.quote(string, safe='/', encoding=None, errors=N...

python正则表达式匹配[]中间为任意字符的实例

如下所示: result = re.search('^\[[\S\s]*\]$',str) print(result) print(result.group()) <_sre....