PyQt5使用QMessageBox,可以設置在幾秒后關閉 (作者使用的python3)
info_box = QMessageBox()
# 因為沒使用這種方式 QMessageBox.information(self, '復制', '復制成功', QMessageBox.Yes) 寫彈出框,
# 則主窗口的樣式不能應用在QMessageBox中,因此重新寫了彈出框的部件樣式
info_box.setStyleSheet('QPushButton{font-weight: bold; background: skyblue; border-radius: 14px;'
'width: 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定時器定時關閉彈框的,但是沒能成功。如果哪位老哥實現了定時器關閉彈框,請教教我,感激不盡。