我用PyQt GUI编写了一个python程序,它使用imaplib检查我是否有新邮件,下面是代码的一部分:
def getAccountStatus(self, accountIndex):
# some not interesting code
mail = imaplib.IMAP4_SSL(currentHost)
# some not interesting code
mail.login('user', 'pass')
mailCount = int(mail.select("INBOX", True)[1][0])
# some not interesting code
serverResponse, data = mail.uid('search', None, 'UNSEEN')
# some not interesting code
unseenUidList = data[0].split()
# some not interesting code
self.emailAccountsWidget.setText("<BR>".join(self.accountStatusString))
return [mailCount, len(unseenUidList)]
问题是,在从imap服务器检索数据的过程中,GUI冻结了,甚至连TextEdit都冻结了(self.emailAccountsWidget)如果不显式调用窗口的重绘事件,则不会更新其文本。有什么方法可以避免这种情况?在
这是Qt中预期和记录的行为。解决方案是将工作移出GUI线程,例如使用
QThread
子类。在如果你想知道更多,你应该读:Qt Threading Basics
相关问题 更多 >
编程相关推荐