PyQt5弹框定时关闭(python)

PyQt5使用QMessageBox,可以设置在几秒后关闭  (作者使用的python3)

info_box = QMessageBox()
# 因为没使用这种方式 QMessageBox.information(self, '复制', '复制成功', QMessageBox.Yes) 写弹出框,
# 则主窗口的样式不能应用在QMessageBox中,因此重新写了弹出框的部件样式

info_box.setStyleSheet('QPushButton{font-weight: bold; background: skyblue; border-radius: 14px;'
' 64px; height: 28px; font-size: 20px; text-align: center;}'
'QLabel{font-weight: bold; font-size: 20px; color: orange}'
)
info_box.setIconPixmap(QPixmap(pic_path))        # 自定义QMessageBox中间的图片
info_box.setWindowIcon(QIcon('pics/icon1.gif'))    # 自定义QMessageBox左上角的小图标
info_box.setWindowTitle(title)     # QMessageBox标题
info_box.setText(text)     #  QMessageBox的提示文字
info_box.setStandardButtons(QMessageBox.Ok)      # QMessageBox显示的按钮
info_box.button(QMessageBox.Ok).animateClick(t)    # t时间后自动关闭(t单位为毫秒)
info_box.exec_()    # 如果使用.show(),会导致QMessageBox框一闪而逝

样式如下,1s弹框会自动关闭,:

     

如果使用QMessageBox.information(self, '复制', '复制成功', QMessageBox.Yes) ,弹框的部件会使用主窗口的通用样式,但是不支持上面的这种定时关闭办法;

后来寻思着使用Qtimer定时器定时关闭弹框的,但是没能成功。如果哪位老哥实现了定时器关闭弹框,请教教我,感激不尽。

原文地址:https://www.cnblogs.com/lipx9527/p/14007001.html