PyQt5实现倒计时弹窗
1.PyQt5弹窗通过QMessageBox实现
2.使用PyQt5的QTimer模块实现倒计时功能
注意
QTimer 一旦开启start方法,必须使用stop停止不然会每个一端事件会出现弹窗。start方法是中为毫秒。
通过添加按钮、添加标题、实现弹窗页面内容 具体代码如下:
def waitMita(self):
self.qmsg_box = QMessageBox()
self.qmsg_box.setWindowTitle("启动MiTA")
self.qmsg_box.setText("5S后默认自动启动MiTA")
self.qmsg_box.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
self.timer = QTimer()
self.timer.timeout.connect(self.startMita)
self.timer.start(5000)
button_y = self.qmsg_box.button(QMessageBox.Yes)
button_y.setText("启动")
button_n = self.qmsg_box.button(QMessageBox.No)
button_n.setText('忽略')
self.qmsg_box.exec()
if button_y == self.qmsg_box.clickedButton():
self.startMita()
else:
self.qmsg_box.close()
self.timer.stop()
def startMita(self):
self.qmsg_box.close()
try:
self.timer.stop()
current_dir = os.path.dirname(os.path.abspath(__file__))
father_dir = os.path.dirname(current_dir)
p = subprocess.Popen(f"cd {father_dir} & setup_MiTA.bat", stderr=subprocess.STDOUT, stdout=subprocess.PIPE,
shell=True,
close_fds=True,
start_new_session=True)
try:
p.communicate(timeout=120)
except:
pass
task_list = subprocess.getstatusoutput("tasklist")
import re
mita_compile = re.compile("mita.exe (.*?) Console")
mita_pid = int(mita_compile.findall(task_list[1])[0])
subprocess.getstatusoutput(f"taskkill /pid {mita_pid} -f")
QMessageBox.about(self, "提示", "MiTA启动成功,已获取最新版本")
except:
QMessageBox.warning(self, "警告", "未找到MiTA.bat", QMessageBox.Cancel)
finally:
self.timer.stop()