zoukankan      html  css  js  c++  java
  • [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_())
  • 相关阅读:
    C/C++字符串转换函数;
    MFC CTreeCtrl 递归遍历算法
    汉字转拼音
    Windows之权限讲解
    Ubuntu 保存文件时报E212
    ON_WM_MOUSEWHEEL无响应
    sln、db、opendb、vcxproj、filters、user文件跟踪说明
    iOS 9: UIStackView入门
    Swift语言Storyboard教程:第一部分
    springboot启动项目报错:ERROR:o.s.b.d.LoggingFailureAnalysisReporter解决办法
  • 原文地址:https://www.cnblogs.com/catmelo/p/2085254.html
Copyright © 2011-2022 走看看