python实现数据展示:设计并实现简洁的 GUI 展示各只股票的股指,要求可以选择显示 单只或多只股票的单个或多个指标
时间: 2024-03-27 08:36:11 浏览: 50
要实现这个需求,可以使用Python中的Tkinter库来创建GUI界面并使用Matplotlib库来绘制股票指标图表。
首先,需要安装Matplotlib库,可以通过以下命令进行安装:
```
pip install matplotlib
```
接下来,可以使用以下代码创建一个简单的GUI界面,并添加一些控件来选择需要显示的股票和指标:
```python
import tkinter as tk
from tkinter import ttk
import matplotlib.pyplot as plt
# 创建GUI界面
root = tk.Tk()
root.title("股票指标展示")
# 添加股票选择下拉框
stock_label = ttk.Label(root, text="选择股票:")
stock_label.grid(row=0, column=0, padx=5, pady=5, sticky="W")
stocks = ["股票1", "股票2", "股票3"]
stock_var = tk.StringVar()
stock_combobox = ttk.Combobox(root, textvariable=stock_var, values=stocks, state="readonly")
stock_combobox.grid(row=0, column=1, padx=5, pady=5)
# 添加指标选择复选框
indicator_label = ttk.Label(root, text="选择指标:")
indicator_label.grid(row=1, column=0, padx=5, pady=5, sticky="W")
indicators = ["指标1", "指标2", "指标3"]
indicator_vars = []
for i in range(len(indicators)):
var = tk.BooleanVar()
indicator_vars.append(var)
checkbox = ttk.Checkbutton(root, text=indicators[i], variable=var)
checkbox.grid(row=i+2, column=1, padx=5, pady=5, sticky="W")
# 添加展示按钮
def show_chart():
selected_indicators = [indicators[i] for i in range(len(indicators)) if indicator_vars[i].get()]
if selected_indicators:
# 绘制股票指标图表
plt.plot([1, 2, 3, 4])
plt.title(f"{stock_var.get()}股票指标展示")
plt.legend(selected_indicators)
plt.show()
show_button = ttk.Button(root, text="展示", command=show_chart)
show_button.grid(row=len(indicators)+2, column=1, padx=5, pady=5)
root.mainloop()
```
在以上代码中,创建了一个包含股票选择下拉框和指标选择复选框的GUI界面,并添加了一个展示按钮。当用户点击展示按钮时,会根据用户选择的股票和指标绘制相应的股票指标图表。
需要注意的是,在实际使用时需要替换掉示例数据,并根据实际需求添加更多的控件和功能。
阅读全文
相关推荐


















