[PySide]在简易在线词典中使用多线程

刚开始写这个小工具的时候遇到了查询过程软件界面假死的情况。

后来在华蟒讨论组里得到指点,使用多线程将查询动作另开线程,同ui线程分开。

使用python的threading类创建新线程后问题果断解决。

这里使用QThread创建新线程(PySide):

#!/usr/bin/python
# -*- coding: utf-8 -*-
 
import sys
from PySide.QtCore import *
from PySide.QtGui import *
import urllib2
import re
#import threading
from BeautifulSoup import BeautifulSoup

class 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_())
posted @ 2011-06-20 15:13  catmelo  阅读(1871)  评论(3编辑  收藏  举报