import time
import tkinter.messagebox
import tkinter.filedialog
import tkinter
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.pylab import mpl
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
import tushare as ts
import mplfinance as mpf
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False
class Home(object):
def __init__(self, master: tkinter.Tk):
self.root = master
self.root.geometry("%dx%d" % (1500,1000))
self.root.title('主界面')
self.creat_page()
self.name = tkinter.Frame(self.analyse_frame)
self.plot = tkinter.Frame(self.analyse_frame)
self.name.pack()
self.plot.pack( side=tkinter.TOP,fill=tkinter.BOTH, expand=1)
self.figure = plt.figure(num=2, figsize=(7, 4), dpi=80, facecolor="gold", edgecolor='green', frameon=True)
self.code=tkinter.StringVar()
self.data()
self.create_matplotlib()
self.createWidget()
def creat_page(self):
#分析页面
self.analyse_frame = tkinter.Frame(self.root)
menubar = tkinter.Menu(self.root)
menubar.add_command(label='个股k线',command=self.show_analyse)
self.root['menu'] = menubar
def show_about(self):
self.analyse_frame.pack_forget()
def show_analyse(self):
self.analyse_frame.pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
def show_all_datas(self):
self.analyse_frame.pack_forget()
def createWidget(self):
"""创建组件"""
tkinter.Label(self.name, text='k线(请输入股票代码,带后缀)',font=('宋体', 20), width=30).grid(row=0, column=0)
tkinter.Entry(self.name, textvariable=self.code, width=20, bd=5,font=('宋体', 20)).grid(row=0, column=1)
tkinter.Button(self.name, text='查询', font=('宋体', 20), width=10,anchor='center',command=self.cx).grid(row=0, column=3)
def create_matplotlib(self):
my_color = mpf.make_marketcolors(up="red", down="green", edge="black",
volume={'up': 'red', 'down': 'green'}, wick="black")
my_style = mpf.make_mpf_style(base_mpf_style='sas', marketcolors=my_color, gridaxis='both', gridstyle='-.',
y_on_right=False, rc={'font.family': 'SimHei'})
self.fig, self.axlist = mpf.plot(self.df, style=my_style, type='candle', mav=(5, 10, 20), volume=True,
show_nontrading=False, returnfig=True)
self.canvas = FigureCanvasTkAgg(self.fig, self.plot)
self.canvas.draw()
self.canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
toolbar = NavigationToolbar2Tk(self.canvas, self.plot)
toolbar.update()
self.canvas._tkcanvas.pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
def data(self):
a =self.code.get()
if len(list(a))==0:
pro = ts.pro_api('b31e0ac207a5a45e0f7503aff25bf6bd929b88fe1d017a034ee0d530')
self.df = pro.daily(ts_code='600732.SH', start_date='20220801')
# 然后 将该DataFrame对象处理为适合我们使用的格式
self.df = self.df.loc[:, ['trade_date', 'open', 'high', 'low', 'close', 'vol']]
self.df.rename(
columns={'trade_date': 'Date', 'open': 'Open', 'high': 'High', 'low': 'Low', 'close': 'Close',
'vol': 'Volume'}, inplace=True) # 重定义列名,方便统一规范操作。
self.df['Date'] = pd.to_datetime(self.df['Date']) # 转换日期列的格式,便于作图
self.df.set_index(['Date'], inplace=True) # 将日期列作为行索引
self.df = self.df.sort_index()
else:
pro = ts.pro_api('b31e0ac207a5a45e0f7503aff25bf6bd929b88fe1d017a034ee0d530')
self.df = pro.daily(ts_code=a, start_date='20220801')
# 然后 将该DataFrame对象处理为适合我们使用的格式
self.df = self.df.loc[:, ['trade_date', 'open', 'high', 'low', 'close', 'vol']]
self.df.rename(
columns={'trade_date': 'Date', 'open': 'Open', 'high': 'High', 'low': 'Low', 'close': 'Close',
'vol': 'Volume'}, inplace=True) # 重定义列名,方便统一规范操作。
self.df['Date'] = pd.to_datetime(self.df['Date']) # 转换日期列的格式,便于作图
self.df.set_index(['Date'], inplace=True) # 将日期列作为行索引
self.df = self.df.sort_index()
def cx(self):
self.canvas.get_tk_widget().delete("all")
self.create_matplotlib()
# for i in ['601899.SH', '600519.SH', '600536.SH', '601012.SH', '600732.SH', '600383.SH', '600056.SH', '600048.SH', '600438.SH']
if __name__ == '__main__':
root = tkinter.Tk()
Home(root)
root.mainloop()