PyQt5实现远程下载更新exe可执行文件
1、实现流程
1、获取远程http地址
2、获取需要更新的exe文件
3、点击更新
4、把exe强关闭
5、下载文件
6、更新
2、效果图
3、示例代码
conf.ini配置文件:
{"http_address_edit_value": "http://xxx.com/xxx/xxx.exe", "exe_name_edit_value": "\u83b7\u53d6IP.exe", "version_num_edit_value": "4.5.16"}
main.py文件:
# -*- coding: utf-8 -*-"""
@contact: 微信 1257309054
@file: main.py
@time: 2024/3/9 21:16
@author: LDC
"""import datetime
import json
import logging
import os
import subprocess
import sys
import timeimport psutil as psutil
import requests
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtWidgets import QMainWindow, QApplicationfrom update import Ui_MainWindowclass Window(QMainWindow, Ui_MainWindow):def __init__(self):super(QMainWindow, self).__init__()self.log_msg = None # 更新日志self.setup_ui() # 渲染画布self.update_thread = UpdateThread(self) # 开启线程循环监控是否可以下载self.connect_signals() # 绑定触发事件def setup_ui(self):self.setupUi(self) # 渲染pyqt5界面self.get_conf_ini() # 获取当前参数self.http_address_edit_value = self.conf['http_address_edit_value'] # 远程更新exe文件地址self.http_address_edit.setText(self.http_address_edit_value)self.exe_name_edit_value = self.conf['exe_name_edit_value'] # exe文件名self.exe_name_edit.setText(self.exe_name_edit_value)self.version_num_edit_value = self.conf['version_num_edit_value'] # 版本号self.version_num_edit.setText(self.version_num_edit_value)self.log_edit.setText('{},{}当前版本号{}'.format(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),self.exe_name_edit_value,self.version_num_edit_value),)def get_conf_ini(self):# 读取conf.iniself.conf = {}with open(r'conf.ini', 'r', encoding='utf-8') as f:self.conf = json.loads(f.read())def connect_signals(self):# 绑定触发事件self.btn_save.clicked.connect(self.btn_save_clicked)self.btn_update.clicked.connect(self.btn_update_clicked)# 输入框输入完成事件self.http_address_edit.editingFinished.connect(self.http_address_edit_changed)self.exe_name_edit.editingFinished.connect(self.exe_name_edit_changed)self.version_num_edit.editingFinished.connect(self.version_num_edit_changed)self.update_thread._signal_update.connect(self.update_threading_slot)def