线程定时器.取消程序关闭



我有一个线程定时器,每60秒运行一个函数,并打印一个字符串到文本框

def devPresent(self):
    stdout = self.deviceExists()
    exists = self.exactMatch(stdout, "device")
    if "device" in str(exists):
        self.progressBox.AppendText('TEST STILL HEREn')
    else: 
        self.progressBox.AppendText('Device connection lostn')
        self.rstBtn()
    t = threading.Timer(60, self.devPresent)
    t.start()

所以这是有效的,TEST STILL HERE被打印到progressBox,但是当我用下面的def关闭窗口时,它关闭了主窗口,但另一个弹出并冻结。

 def closeWindow(self,e):
    t = self.devPresent
    t.cancel()
    time.sleep(3)
    self.Destroy()

我是否错误地关闭线程?

t将不会在类方法之间可见,除非您将其作为self的属性。试一试:

def devPresent(self):
    stdout = self.deviceExists()
    exists = self.exactMatch(stdout, "device")
    if "device" in str(exists):
        self.progressBox.AppendText('TEST STILL HEREn')
    else: 
        self.progressBox.AppendText('Device connection lostn')
        self.rstBtn()
    self.t = threading.Timer(60, self.devPresent)
    self.t.start()

 def closeWindow(self,e):
    self.t.cancel()
    time.sleep(3)
    self.Destroy()

最新更新