Python+Tkinter实现软件自动更新与提醒
作者:小锋学长生活大爆炸
这篇文章主要为大家详细介绍了Python如何利用Tkinter编写一个软件自动更新与提醒小程序,文中的示例代码简洁易懂,感兴趣的小伙伴可以动手尝试一下
示例演示
参考代码
import os import _thread import shutil import subprocess import sys import requests from tkinter import Tk, Label, IntVar, DoubleVar, HORIZONTAL, messagebox from tkinter.ttk import Progressbar import pyautogui class SoftUpdateDialog: def __init__(self, soft_version): self.soft_update_url = 'http://xfxuezhang.cn/web/share/version/update_demo' self.soft_version = soft_version def init_ui(self, root): self.root = root self.root.title('软件更新') self.root.geometry('300x100') self.progress_var = IntVar() self.label_var = DoubleVar() self.label_var.set(0.0) label = Label(self.root, text='正在更新软件,请等待···') label.pack(pady=5) self.progress_bar = Progressbar(self.root, length=200, mode='indeterminate') self.progress_bar.pack(pady=5) def tip_show(self, msg): """消息提示""" messagebox.showwarning('提示', msg) def soft_download_with_thread(self): '''开线程下载''' # 下载txt文件 url = self.soft_update_url try: req = requests.get(url, stream=True, verify=False) except Exception: self.tip_show('请检查网络设置,无法检测到网络连接') return if req is None or str(req.status_code) != '200': self.tip_show('软件更新失败,请检查网络设置,无法检测到网络连接') return json_result = req.json() # 版本比对 latest_version = json_result['soft_version'] latest_soft_url = json_result['soft_url'] if self.compare_version(latest_version): # 下载进度工具条 self.progress_var.set(0) self.progress_bar.start(60) # self._soft_download(latest_soft_url, latest_version) # 软件下载线程 _thread.start_new_thread(self._soft_download, (latest_soft_url, latest_version)) def check_version(self): url = self.soft_update_url try: req = requests.get(url, stream=True, verify=False) except Exception: self.tip_show('请检查网络设置,无法检测到网络连接') return if req is None or str(req.status_code) != '200': self.tip_show('软件更新失败,请检查网络设置,无法检测到网络连接') return json_result = req.json() # 版本比对 latest_version = json_result['soft_version'] return self.compare_version(latest_version) def _update_progressbar(self): '''更新进度条''' self.progress_bar['value'] = self.label_var.get() def _end_update(self): '''结束更新''' self.progress_bar.stop() self.root.destroy() def get_remote_file_size(self, url, proxy=None): """通过content-length头获取远程文件大小""" try: req = requests.head(url, proxies={'http': proxy, 'https': proxy} if proxy else None, verify=False) fileSize = int(req.headers.get('Content-Length', 0)) return fileSize except Exception: return 0 def _soft_download(self, latest_url, latest_version): '''软件下载''' self.label_var.set(0.0) lastest_name = os.path.basename(latest_url) lastest_size = self.get_remote_file_size(latest_url) start_size = 0 with requests.get(latest_url, stream=True, verify=False) as r: with open(lastest_name, "wb") as f: for chunk in r.iter_content(chunk_size=1024): if not chunk: continue start_size += len(chunk) f.write(chunk) f.flush() self.label_var.set(100.0 * start_size / lastest_size) print('>> ok') self._end_update() def compare_version(self, latest_version): return int(self.soft_version.replace('.', '')) < int(latest_version.replace('.', '')) if __name__ == '__main__': app = Tk() app.geometry('0x0') soft_dialog = SoftUpdateDialog(soft_version='0.0.5') if soft_dialog.check_version() and pyautogui.confirm(text='有新版本,是否更新', title='通知', buttons=['OK', 'Cancel'])=='OK': soft_dialog.init_ui(root=app) soft_dialog.soft_download_with_thread() app.mainloop() pyautogui.alert(text='下载完成!', title='通知', button='好的👌🏻')
远程服务器的文件格式:
{ "soft_version": "1.0.0", "soft_url": "http://xfxuezhang.cn/web/share/version/update_demo.exe" }
到此这篇关于Python+Tkinter实现软件自动更新与提醒的文章就介绍到这了,更多相关Python Tkinter软件自动更新提醒内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!