使用Python和Tkinter的开发股票交易计算器

PieroPc 2024-12-21 10:47:48编程技术
426

在当今的金融市场中,股票交易是一项复杂且需要精确计算的活动。为了帮助投资者更好地管理他们的交易成本和收益,本文介绍了一个使用Python和Tkinter库开发的股票交易计算器。通过这个计算器,用户可以轻松输入股票交易的相关数据,获得准确的交易成本、收益、利润率以及目标卖出价等关键信息。本文将详细解读该计算器的开发过程和功能特点。

python版

完整代码

import tkinter as tk
from tkinter import ttk, messagebox
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from datetime import datetime
 
class StockDesigner:
    def __init__(self, root):
        self.root = root
        self.root.title("股票交易计算器")
        self.root.geometry("1000x700")
        
        # 设置主题样式
        style = ttk.Style()
        style.theme_use('clam')  # 使用 clam 主题
        
        # 自定义样式
        style.configure('TLabel', font=('微软雅黑', 10))
        style.configure('TButton', font=('微软雅黑', 10))
        style.configure('TLabelframe', font=('微软雅黑', 10))
        style.configure('TLabelframe.Label', font=('微软雅黑', 10, 'bold'))
        style.configure('Custom.TButton', padding=10, font=('微软雅黑', 10, 'bold'))
        
        # 设置默认手续费率
        self.commission_rate = 0.00025
        self.stamp_duty = 0.001
        self.min_commission = 5
        
        # 创建主框架
        self.main_frame = ttk.Frame(root, padding="20")
        self.main_frame.grid(row=0, column=0, sticky="nsew")
        
        self.create_widgets()
        
        # 配置根窗口的网格权重
        root.grid_rowconfigure(0, weight=1)
        root.grid_columnconfigure(0, weight=1)
        
    def create_widgets(self):
        # 创建标题
        title_label = ttk.Label(self.main_frame, text="股票交易计算器", 
                              font=('微软雅黑', 16, 'bold'))
        title_label.grid(row=0, column=0, columnspan=2, pady=(0, 20))
        
        # 创建左侧输入框架
        input_frame = ttk.LabelFrame(self.main_frame, text="交易数据输入", padding="20")
        input_frame.grid(row=1, column=0, padx=(0, 10), sticky="nsew")
        
        # 修改输入框布局
        labels = ["股票代码:", "买入价格:", "买入数量:", "目标卖出价:", "目标盈利金额:"]
        entries = ["code_entry", "buy_price_entry", "volume_entry", "sell_price_entry", "target_profit_entry"]
        
        for i, (label, entry) in enumerate(zip(labels, entries)):
            ttk.Label(input_frame, text=label).grid(row=i, column=0, pady=10, padx=(0, 10), sticky="e")
            entry_widget = ttk.Entry(input_frame, width=25, font=('微软雅黑', 10))
            entry_widget.grid(row=i, column=1, pady=10, sticky="w")
            setattr(self, entry, entry_widget)
        
        # 添加说明文本
        hint_text = "注:手续费率为万分之2.5,印花税为千分之1"
        ttk.Label(input_frame, text=hint_text, font=('微软雅黑', 9), foreground='gray')\
            .grid(row=len(labels), column=0, columnspan=2, pady=(10, 0))
        
        # 添加两个计算按钮
        calc_button = ttk.Button(input_frame, text="计算交易成本和收益", 
                               command=self.calculate_profit, style='Custom.TButton')
        calc_button.grid(row=len(labels)+1, column=0, columnspan=2, pady=(20,5))
        
        reverse_calc_button = ttk.Button(input_frame, text="计算目标卖出价", 
                                       command=self.calculate_target_price, style='Custom.TButton')
        reverse_calc_button.grid(row=len(labels)+2, column=0, columnspan=2, pady=(5,20))
        
        # 创建右侧显示框架
        display_frame = ttk.LabelFrame(self.main_frame, text="计算结果", padding="20")
        display_frame.grid(row=1, column=1, sticky="nsew")
        
        # 创建结果显示区
        self.result_text = tk.Text(display_frame, height=20, width=45, 
                                 font=('Consolas', 10), bg='#F8F8F8')
        self.result_text.grid(row=0, column=0, sticky="nsew")
        
        # 添���滚动条
        scrollbar = ttk.Scrollbar(display_frame, orient="vertical", 
                                command=self.result_text.yview)
        scrollbar.grid(row=0, column=1, sticky="ns")
        self.result_text.configure(yscrollcommand=scrollbar.set)
        
        # 设置网格权重
        self.main_frame.grid_columnconfigure(1, weight=1)
        self.main_frame.grid_rowconfigure(1, weight=1)
        display_frame.grid_columnconfigure(0, weight=1)
        display_frame.grid_rowconfigure(0, weight=1)
        
    def calculate_profit(self):
        try:
            buy_price = float(self.buy_price_entry.get())
            volume = int(self.volume_entry.get())
            sell_price = float(self.sell_price_entry.get())
            
            if not all([buy_price > 0, volume > 0, sell_price > 0]):
                messagebox.showerror("错误", "请输入有效的数据!")
                return
            
            # 计算买入成本
            buy_amount = buy_price * volume
            buy_commission = max(buy_amount * self.commission_rate, self.min_commission)
            
            # 计算卖出收入
            sell_amount = sell_price * volume
            sell_commission = max(sell_amount * self.commission_rate, self.min_commission)
            stamp_duty = sell_amount * self.stamp_duty
            
            # 计算总成本和收益
            total_cost = buy_amount + buy_commission + sell_commission + stamp_duty
            total_income = sell_amount
            net_profit = total_income - total_cost
            profit_rate = (net_profit / total_cost) * 100
            
            # 显示结果
            result = f"""┌{'─'*40}┐
│            交易明细                    │
└{'─'*40}┘
 买入价格: {buy_price:.3f}元
 买入数量: {volume:,}股
 买入金额: {buy_amount:,.2f}元
 买入手续费: {buy_commission:.2f}元
 卖出价格: {sell_price:.3f}元
 卖出金额: {sell_amount:,.2f}元
 卖出手续费: {sell_commission:.2f}元
 印花税: {stamp_duty:.2f}元
┌{'─'*40}┐
│            盈亏分析                    │
└{'─'*40}┘
 总成本: {total_cost:,.2f}元
 总收入: {total_income:,.2f}元
 净利润: {net_profit:,.2f}元
 收益率: {profit_rate:.2f}%
┌{'─'*40}┐
│            盈亏平衡点                  │
└{'─'*40}┘
 最低卖出价格: {(total_cost/volume):.3f}元
"""
            self.result_text.delete(1.0, tk.END)
            self.result_text.insert(tk.END, result)
            
        except ValueError:
            messagebox.showerror("错误", "请输入正确的数值!")
 
    def calculate_target_price(self):
        try:
            buy_price = float(self.buy_price_entry.get())
            volume = int(self.volume_entry.get())
            target_profit = float(self.target_profit_entry.get())
            
            if not all([buy_price > 0, volume > 0, target_profit > 0]):
                messagebox.showerror("错误", "请输入有效的数据!")
                return
            
            # 计算买入成本
            buy_amount = buy_price * volume
            buy_commission = max(buy_amount * self.commission_rate, self.min_commission)
            
            # 通过二分法查找目标卖出价
            left, right = buy_price, buy_price * 2
            target_price = 0
            
            while left <= right:
                mid = (left + right) / 2
                sell_amount = mid * volume
                sell_commission = max(sell_amount * self.commission_rate, self.min_commission)
                stamp_duty = sell_amount * self.stamp_duty
                
                total_cost = buy_amount + buy_commission + sell_commission + stamp_duty
                net_profit = sell_amount - total_cost
                
                if abs(net_profit - target_profit) < 0.01:
                    target_price = mid
                    break
                elif net_profit < target_profit:
                    left = mid + 0.0001
                else:
                    right = mid - 0.0001
            
            # 计算详细成本
            buy_amount = buy_price * volume
            buy_commission = max(buy_amount * self.commission_rate, self.min_commission)
            
            sell_amount = target_price * volume
            sell_commission = max(sell_amount * self.commission_rate, self.min_commission)
            stamp_duty = sell_amount * self.stamp_duty
            
            total_cost = buy_amount + buy_commission + sell_commission + stamp_duty
            
            # 修改显示结果,添加成本明细
            result = f"""┌{'─'*40}┐
│            反向计算结果                │
└{'─'*40}┘
 买入价格: {buy_price:.3f}元
 买入数量: {volume:,}股
 买入金额: {buy_amount:,.2f}元
 买入手续费: {buy_commission:.2f}元
 目标盈利: {target_profit:.2f}元
 需要卖出价格: {target_price:.3f}元
 卖出金额: {sell_amount:,.2f}元
 卖出手续费: {sell_commission:.2f}元
 印花税: {stamp_duty:.2f}元
┌{'─'*40}┐
│            成本与收益                  │
└{'─'*40}┘
 总成本: {total_cost:,.2f}元
 总收入: {sell_amount:,.2f}元
 净利润: {target_profit:.2f}元
 上涨幅度: {((target_price/buy_price - 1) * 100):.2f}%
"""
            self.result_text.delete(1.0, tk.END)
            self.result_text.insert(tk.END, result)
            
            # 自动填充卖出价格输入框
            self.sell_price_entry.delete(0, tk.END)
            self.sell_price_entry.insert(0, f"{target_price:.3f}")
            
        except ValueError:
            messagebox.showerror("错误", "请输入正确的数值!")
 
if __name__ == "__main__":
    root = tk.Tk()
    app = StockDesigner(root)
    root.mainloop()

效果图

使用Python和Tkinter的开发股票交易计算器

Html 版

完整代码

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>股票交易计算器</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 0 auto;
            padding: 20px;
        }
        .calculator {
            border: 1px solid #ccc;
            padding: 20px;
            border-radius: 5px;
        }
        .input-group {
            margin-bottom: 15px;
        }
        label {
            display: inline-block;
            width: 120px;
        }
        input {
            width: 150px;
            padding: 5px;
        }
        button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        button:hover {
            background-color: #45a049;
        }
        .result {
            margin-top: 20px;
            padding: 10px;
            background-color: #f9f9f9;
        }
    </style>
</head>
<body>
    <div class="calculator">
        <h2>股票交易计算器</h2>
        <div class="input-group">
            <label>买入价格:</label>
            <input type="number" id="buyPrice" step="0.01">
        </div>
        <div class="input-group">
            <label>卖出价格:</label>
            <input type="number" id="sellPrice" step="0.01">
        </div>
        <div class="input-group">
            <label>交易数量:</label>
            <input type="number" id="quantity">
        </div>
        <div class="input-group">
            <label>手续费率:</label>
            <input type="number" id="feeRate" value="0.00025" step="0.0001">
        </div>
        <button onclick="calculate()">计算</button>
        <div class="result" id="result"></div>
    </div>
 
    <script>
        function calculate() {
            // 获取输入值
            const buyPrice = parseFloat(document.getElementById('buyPrice').value);
            const sellPrice = parseFloat(document.getElementById('sellPrice').value);
            const quantity = parseInt(document.getElementById('quantity').value);
            const feeRate = parseFloat(document.getElementById('feeRate').value);
 
            // 验证输入
            if (!buyPrice || !sellPrice || !quantity || !feeRate) {
                alert('请填写所有必要信息!');
                return;
            }
 
            // 计算交易费用
            const buyTotal = buyPrice * quantity;
            const sellTotal = sellPrice * quantity;
            const buyFee = Math.max(buyTotal * feeRate, 5); // 最低手续费5元
            const sellFee = Math.max(sellTotal * feeRate, 5);
 
            // 计算盈亏
            const profit = sellTotal - buyTotal - buyFee - sellFee;
            const profitRate = (profit / buyTotal * 100).toFixed(2);
 
            // 显示结果
            const resultHTML = `
                <h3>交易明细:</h3>
                <p>买入总额:¥${buyTotal.toFixed(2)}</p>
                <p>买入手续费:¥${buyFee.toFixed(2)}</p>
                <p>卖出总额:¥${sellTotal.toFixed(2)}</p>
                <p>卖出手续费:¥${sellFee.toFixed(2)}</p>
                <p>净盈亏:¥${profit.toFixed(2)}</p>
                <p>收益率:${profitRate}%</p>
            `;
            
            document.getElementById('result').innerHTML = resultHTML;
        }
    </script>
</body>
</html>

效果图

使用Python和Tkinter的开发股票交易计算器

总结

本文介绍了如何使用Python和Tkinter库开发一个功能强大的股票交易计算器。该计算器不仅能够计算交易成本和收益,还能够根据用户的目标盈利金额计算出所需的目标卖出价。通过图形用户界面,用户可以方便地输入相关数据,并获得实时的计算结果。此外,计算器还具备错误处理功能,确保输入数据的有效性和准确性。这一工具对于股票投资者来说是一个非常实用的辅助工具,能够帮助他们更好地管理交易风险和提高投资回报率。

Python Tkinter 股票 计算器
THE END
蜜芽
故事不长,也不难讲,四字概括,毫无意义。

相关推荐

Python yield 用法大全:轻松掌握生成器与迭代器设计
在Python中,yield关键字是构建生成器的核心工具,它通过状态保存机制实现了高效的内存管理和惰性计算。与传统的迭代器实现相比,yield能将迭代器设计从复杂的类定义简化为直...
2025-09-15 编程技术
547

基于Python的旅游数据分析可视化系统【2026最新】
本研究成功开发了基于Python+Django+Vue+MySQL的旅游数据分析可视化系统,实现了从数据采集到可视化展示的全流程管理。系统采用前后端分离架构,前端通过Vue框架构建响应式界...
2025-09-13 编程技术
571

手把手教你用Python读取txt文件:从基础到实战的完整教程
Python作为数据处理的利器,文件读写是其基础核心功能。掌握txt文件读取不仅能处理日志、配置文件等常见场景,更是理解Python文件I/O的基石。本文ZHANID工具网将从基础语法到...
2025-09-12 编程技术
543

Python Flask 入门指南:从零开始搭建你的第一个 Web 应用
Flask作为 Python 中最轻量级且灵活的 Web 框架之一,特别适合初学者快速上手 Web 应用开发。本文将带你一步步了解如何在本地环境中安装 Flask、创建一个简单的 Web 应用,并...
2025-09-11 编程技术
532

Python 如何调用 MediaPipe?详细安装与使用指南
MediaPipe 是 Google 开发的跨平台机器学习框架,支持实时处理视觉、音频和文本数据。本文脚本之家将系统讲解 Python 环境下 MediaPipe 的安装、配置及核心功能调用方法,涵盖...
2025-09-10 编程技术
575

基于Python开发一个利率计算器的思路及示例代码
利率计算是金融领域的基础需求,涵盖贷款利息、存款收益、投资回报等场景。传统计算依赖手工公式或Excel表格,存在效率低、易出错等问题。Python凭借其简洁的语法和强大的数学...
2025-09-09 编程技术
515