V变量—建站日记 - price
type: channel
link: http://vvars.com/tag/price/
items: PY的vps成本管理器简单代码

PY的vps成本管理器简单代码
type: item
date: 2024-12-15T20:18:48+08:00
description: 一直以来，本人有个痛点，就是手持太多vps。有月付的，年付的，半年的，季付的。 各种结算货币。完全无统一标准。导致财务管理困难。所以就诞生了这个工具简单易用，只需要把如下代码保存到任意.py文件并且在cmd运行即可。python xx.py程序会自动在相同目录创建vps_cost_config.json文件用作数据存储。感谢：chatgptimport tkinter as tk from tkinter import messagebox, ttk # 引入 ttk 组件，界面更美观 import json # =============== 配置文件相关 =============== CONFIG_FILE_PATH = "vps_cost_config.json" default_exchange_rates = {'USD': 1, 'CNY': 7.1, 'EUR': 0.85} def load_config(): try: with open(CONFIG_FILE_PATH, 'r') as file: return json.load(file) except FileNotFoundError: return {'exchange_rates': default_exchange_rates, 'vps_list': []} def save_config(config): with open(CONFIG_FILE_PATH, 'w') as file: json.dump(config, file, indent=4) # =============== 功能函数 =============== def update_exchange_rates(): try: exchange_rates = {currency: float(var.get()) for currency, var in currency_rate_vars.items()} config['exchange_rates'] = exchange_rates save_config(config) update_vps_list() update_monthly_costs() messagebox.showinfo("成功", "汇率已更新并保存。") except ValueError: messagebox.showerror("错误", "请输入有效的数字汇率。") def add_vps(): try: name = vps_name_var.get() amount = float(amount_var.get()) vps = { 'name': name, 'payment_cycle': payment_cycle_var.get(), 'amount': amount, 'currency': currency_var.get() } config['vps_list'].append(vps) save_config(config) update_vps_list() clear_vps_fields() update_monthly_costs() except ValueError: messagebox.showerror("错误", "请输入有效的费用。") def delete_vps(): selected = vps_listbox.curselection() if not selected: messagebox.showerror("错误", "请选择一个 VPS 项目删除。") return selected_name = vps_listbox.get(selected).split(' ')[0] config['vps_list'] = [v for v in config['vps_list'] if v['name'] != selected_name] save_config(config) update_vps_list() update_monthly_costs() def update_vps_list(): vps_listbox.delete(0, tk.END) for vps in config['vps_list']: vps_listbox.insert(tk.END, f"{vps['name']} ({vps['payment_cycle']}) - {vps['amount']} {vps['currency']}") def update_monthly_costs(): monthly_costs, total_cost = calculate_…