[PySide]在简易在线词典中使用多线程
来源:互联网 发布:查看游戏fps软件 编辑:程序博客网 时间:2024/05/04 11:04
刚开始写这个小工具的时候遇到了查询过程软件界面假死的情况。
后来在华蟒讨论组里得到指点,使用多线程将查询动作另开线程,同ui线程分开。
使用python的threading类创建新线程后问题果断解决。
这里使用QThread创建新线程(PySide):
#!/usr/bin/python# -*- coding: utf-8 -*- import sysfrom PySide.QtCore import *from PySide.QtGui import *import urllib2import re#import threadingfrom BeautifulSoup import BeautifulSoupclass Dialog(QDialog): def __init__(self,parent=None,): super(Dialog,self).__init__(parent) self.setWindowTitle(u"词典") self.browser=QTextBrowser() self.edit=QLineEdit(u"输入单词") self.browser.setText("QtSharp's Toy") quitbutton=QPushButton(u'退出') quitbutton.clicked.connect(app.quit) searchbutton=QPushButton(u'查询') searchbutton.clicked.connect(self.sendword) layout=QVBoxLayout() layout.addWidget(searchbutton) layout.addWidget(self.edit) layout.addWidget(quitbutton) layout.addWidget(self.browser) self.setLayout(layout) def sendword(self): word=self.edit.text() self.sendsignal.emit(word) sendsignal=Signal(str) @Slot(str) def getexp(self,exp): self.browser.setText(exp) class Search(QThread): def __init__(self,parent=None,): super(Search,self).__init__(parent) def run(self): wordweb=urllib2.urlopen("http://dict.baidu.com/s?wd=%s"% self.word) charset = wordweb.headers['Content-Type'].split(' charset=')[0].lower() soup=BeautifulSoup(wordweb,fromEncoding=charset) exp=soup.findAll(text=re.compile(u'译典通'))[0].parent.parent.parent.parent exp=str(exp).decode('utf8') self.exped.emit(exp) exped=Signal(str) @Slot(str) def getword(self,word): self.word=word self.run() if __name__=='__main__': app=QApplication(sys.argv) dialog=Dialog() search=Search() dialog.sendsignal.connect(search.getword) search.exped.connect(dialog.getexp) dialog.show() sys.exit(app.exec_())
- [PySide]在简易在线词典中使用多线程
- 在PySide中使用多进程与多线程(multiprocess,threading)
- 在PySide中嵌入VTK
- PySide 简易教程<一>-------Hello PySide
- 简易词典
- 使用PHP实现简易词典查询功能
- 在线词典
- 使用AJAX开发简单在线小词典
- 如何在FineReader 12破解版中使用用户词典
- 如何在FineReader 12破解版中使用用户词典
- PySide 路在何方?
- PySide+QWebView使用
- 第十五周项目3-简易中英词典
- 有道词典简易客户端
- 在Eclipse中使用在线java帮助
- 在IOS中使用多线程
- Map在多线程中使用
- 队列在多线程中使用
- Java线程模型缺陷
- 线性表
- IOS推送
- VM增加磁盘空间
- service_name的一点理解
- [PySide]在简易在线词典中使用多线程
- 关于BeautifulSoup编码的一些问题
- 《Visual C++ 2008入门经典》学习笔记(1)——数据类型
- 《Visual C++ 2008入门经典》学习笔记(2)——typedef与枚举
- 三菱PLC——Dx Developer的使用
- win下硬盘安装ubuntu(无需光驱、U盘)
- 超简单的Ghost安装双系统方法
- 简明 Python 编程规范
- 通过隐藏iframe实现文件下载的js方法