当前位置: 代码网 > it编程>前端脚本>Python > Python+PyQt5开发一个Windows EXE程序在线更新工具

Python+PyQt5开发一个Windows EXE程序在线更新工具

2026年01月04日 Python 我要评论
一、前言在使用 pyqt5 + pyinstaller 开发 windows 桌面工具时,一个非常现实的问题是:程序如何自动检测新版本,并完成在线升级?本文基于一个真实可用、已落地的更新方案,实现了:

一、前言

在使用 pyqt5 + pyinstaller 开发 windows 桌面工具时,一个非常现实的问题是:

程序如何自动检测新版本,并完成在线升级?

本文基于一个真实可用、已落地的更新方案,实现了:

  • 远程版本号检测
  • 语义化版本对比
  • 更新提示弹窗
  • 子线程下载更新包
  • 实时下载进度条
  • zip 解压
  • exe 自动替换
  • 旧版本自动备份
  • 更新完成后安全退出

非常适合 工具类 / 内部系统 / 单机 exe 程序

二、运行效果说明

整体更新流程如下:

  • 点击「获取最新版本」
  • 请求远程 version.txt
  • 发现新版本 → 弹窗提示
  • 用户确认 → 下载 update.zip
  • 显示下载进度条
  • 解压更新文件
  • 备份旧 exe
  • 替换新 exe
  • 提示升级完成并退出程序

三、环境与依赖说明

python 版本

python 3.8+

pip 依赖安装

pip install pyqt5
pip install requests
pip install packaging

(如需打包 exe)

pip install pyinstaller

四、服务器端准备

版本号文件(version.txt)

1.2.0

更新包(update.zip)

update.zip
├── update.exe
├── logo.png(可选)

五、完整代码

softvesion.py
def current_version():
    """
    returns the version of the package
    """
    return "1.1.0"
def latest_version():
    """
    returns the latest version of the package
    """
    return "1.1.0"
def upgrade_date():
    """
    returns the time of the upgrade
    """
    return "2024年1月19日"
def current_title():
    """
    returns the title of the package
    """
    return "excel常用小工具"
def remote_version_url():
    """
    returns the url of the latest version
    """
    return "http://82.157.62.197:97/version.txt"
def update_url():
    """
    returns the url of the version info
    """
    return "http://192.168.31.219:8080/update.zip"

updateapp.py

import os
import zipfile
import requests
from packaging import version
from pyqt5.qtcore import qthread, pyqtsignal, qt
from pyqt5.qtwidgets import qapplication,qdialog, qvboxlayout,qmainwindow, qpushbutton, qmessagebox, qprogressdialog,qlabel,qaction,qdesktopwidget
import shutil
from .softvesion import current_version,latest_version,remote_version_url,update_url,current_title
from pyqt5.qtgui import qstandarditemmodel, qstandarditem,qicon
import sys
import time
import subprocess
class downloadthread(qthread):
    progresschanged = pyqtsignal(int)
    downloadfinished = pyqtsignal()
    unabletofetchupdatelink = pyqtsignal()

    def __init__(self, url, save_path):
        super().__init__()
        self.url = url
        self.save_path = save_path

    def run(self):
        try:
            response = requests.get(self.url, stream=true)
            if response.status_code == 200:
                total_size = int(response.headers.get('content-length', 0))

                downloaded_size = 0
                with open(self.save_path, 'wb') as file:
                    for data in response.iter_content(chunk_size=8192):
                        file.write(data)
                        downloaded_size += len(data)
                        progress = int((downloaded_size / total_size) * 100)
                        self.progresschanged.emit(progress)

                self.downloadfinished.emit()
            else:
                print("error fetching update link:")
                self.unabletofetchupdatelink.emit()
        except exception as e:
            print("error fetching update link:", e)
            self.unabletofetchupdatelink.emit()

class updateapp(qdialog):
    def __init__(self,parent=none):
        super().__init__(parent)
        self.current_version = current_version()  # 当前应用程序版本号
        self.latest_version = latest_version()
        self.progress_dialog = none
        self.download_thread = none
        self.remote_version_url= remote_version_url()
        self.update_url = update_url()
        self.init_ui()
        #self.show_update_notification()
    def move_to_center(self, widget):
        # 将 widget 移动到屏幕中央
        desktop_center = qdesktopwidget().availablegeometry().center()
        widget_frame = widget.framegeometry()
        widget_frame.movecenter(desktop_center)
        widget.move(widget_frame.topleft())
    def init_ui(self):
        self.setfixedsize(300, 200)
        self.setwindowtitle("检查更新")
        icon = qicon("icon.png")
        self.setwindowicon(icon)

        self.version_label = qlabel("当前版本号: " + self.current_version, self)
        self.version_label.setalignment(qt.aligncenter) 
        self.version_label.setgeometry(100, 10, 110, 20)

        self.update_button = qpushbutton("获取最新版本", self)
        self.update_button.setgeometry(100, 60, 110, 40)
        self.update_button.clicked.connect(self.show_update_notification)

        if self.download_thread:
            self.download_thread.unabletofetchupdatelink.connect(self.show_unable_to_fetch_update_link)

    def show_unable_to_fetch_update_link(self):
        qmessagebox.critical(self, "无法获取更新链接", "无法获取更新链接,请检查网络连接。")

    def update_app(self):
        try:
            remote_version = self.get_remote_version()
            if version.parse(remote_version) > version.parse(self.current_version):
                if self.has_network_connection():
                    self.perform_upgrade()
                else:
                    qmessagebox.information(self, "无法获取版本号", "无法获取远程版本号,请检查网络连接。")
            else:
                qmessagebox.information(self, "无需升级", "当前应用程序已经是最新版本。")
        except:
            qmessagebox.information(self, "无法获取版本号", "无法获取远程版本号,请检查网络连接。")

    def get_remote_version(self):
        try:
            response = requests.get(self.remote_version_url)
            if response.status_code == 200:
                return response.text.strip()
            return self.latest_version
        except:
            return self.latest_version

    def perform_upgrade(self):
        update_url = self.update_url
        temp_zip_path = "temp_update.zip"

        self.download_thread = downloadthread(update_url, temp_zip_path)
        self.download_thread.progresschanged.connect(self.update_progress_bar)
        self.download_thread.downloadfinished.connect(self.handle_download_finished)

        self.progress_dialog = qprogressdialog("正在下载更新...", none, 0, 100, self)
        self.progress_dialog.setwindowmodality(qt.windowmodal)
        self.progress_dialog.setautoclose(false)
        self.progress_dialog.setautoreset(false)
        self.progress_dialog.setwindowtitle("下载进度")
        self.move_to_center(self.progress_dialog)
        self.progress_dialog.canceled.connect(self.download_thread.quit)

        self.download_thread.start()

    def update_progress_bar(self, progress):
        self.progress_dialog.setvalue(progress)

    def handle_download_finished(self):
        self.progress_dialog.hide()
        if os.path.exists("temp_update.zip"):
            self.process_download("temp_update.zip")
            os.remove("temp_update.zip")
        sys.exit()

    def has_network_connection(self):
        try:
            requests.get(remote_version_url(), timeout=5)
            return true
        except:
            return false

    def show_update_notification(self):
        try:
            remote_version = self.get_remote_version()
            if version.parse(remote_version) > version.parse(self.current_version):
                reply = qmessagebox.question(
                    self, "版本更新提示",
                    f"有新版本可用:{remote_version}\n您当前的版本:{self.current_version}\n是否要更新?",
                    qmessagebox.yes | qmessagebox.no
                )
                if reply == qmessagebox.yes:
                    self.update_app()
            else:
                qmessagebox.information(self, "版本更新提示", "您当前的版本是最新版本。")
        except:
            pass

if __name__ == "__main__":
    app = qapplication([])
    window = updateapp()
    window.show()
    app.exec_()

六、关键设计说明

使用 qthread 防止界面卡死

使用 packaging.version 进行版本比较

zip 更新包支持扩展文件

exe 覆盖前自动备份

更新完成后安全退出

七、适用场景

pyqt5 工具软件

内部办公系统

单机 exe 程序

不依赖第三方更新框架

八、总结

本文提供的是一个 可直接落地、真实可用 的 pyqt5 自动更新方案,无需复杂服务器逻辑,维护成本极低。

到此这篇关于python+pyqt5开发一个windows exe程序在线更新工具的文章就介绍到这了,更多相关python exe程序更新内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com