pyqt4:在線程Qthread中使用定時器Qtimer


GUI main 部分,主app類中的__init__初始化方法中添加

 

實例化線程

self.s2_thread=Worker2()

初始化一個定時器

 
        self.log_get=QtCore.QTimer()

將調用方法和信號綁定

QtCore.QObject.connect(self.s2_thread, QtCore.SIGNAL("get_log_start()"), self.qtimer_get_log) 

 

 

定義觸發開始的調用方法

 def qtimer_get_log(self):
        self.log_get.singleShot(30000,self.s2_thread.get_mem_error_logfile_content) self.log_get.singleShot(90000,self.s2_thread.get_mem_right_logfile_content)
        self.log_get.start()

 

 

線程Qthread部分

在線程的run中根據需要的地方發起激活定時器的信號

class Worker2(QtCore.QThread):
    def __init__(self, parent=None):
    
        QtCore.QThread.__init__(self, parent)

  def get_mem_error_logfile_content(self):
        filename=host_list['app75']['autologfilename']
        with open(filename,'r') as f:
            self.log_30_sec=f.read()
            print '#'*30
    def get_mem_right_logfile_content(self):
        filename=host_list['app75']['autologfilename']
        with open(filename,'r') as f:
            self.log_90_sec=f.read()
            print '*'*30
def run(self): ...... self.emit(QtCore.SIGNAL("get_log_start()")) #觸發定時器信號 ......

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM