如何实现matplotlib在tkinter上点击刷新k线呢?
输入股票代码后点击查询就能生成新的k线图
```python
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
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 = '600763.SH'
self.create_matplotlib()
self.createWidget()
def creat_page(self):
# 市场数据页面
self.all_datas_frame = tkinter.Frame(self.root)
#分析页面
self.analyse_frame = tkinter.Frame(self.root)
# 关于页面
self.about_frame = tkinter.Frame(self.root)
mesg = "xxxxxx"
tkinter.Label(self.about_frame, text=mesg, font=('宋体', 20)).pack()
menubar = tkinter.Menu(self.root)
menubar.add_command(label='个股k线',command=self.show_analyse)
self.root['menu'] = menubar
def show_about(self):
self.about_frame.pack()
self.all_datas_frame.pack_forget()
self.analyse_frame.pack_forget()
def show_analyse(self):
self.analyse_frame.pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
self.all_datas_frame.pack_forget()
self.about_frame.pack_forget()
def show_all_datas(self):
self.all_datas_frame.pack()
self.about_frame.pack_forget()
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').grid(row=0, column=3)
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)
return self.code
def create_matplotlib(self):
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False
pro = ts.pro_api('b31e0ac207a5a45e0f7503aff25bf6bd929b88fe1d017a034ee0d530')
df = pro.daily(ts_code=self.code, start_date='20220801')
df = df.loc[:, ['trade_date', 'open', 'high', 'low', 'close', 'vol']]
df.rename(columns={'trade_date': 'Date', 'open': 'Open','high': 'High', 'low': 'Low','close': 'Close', 'vol': 'Volume'},inplace=True)
df['Date'] = pd.to_datetime(df['Date'])
df.set_index(['Date'], inplace=True)
df = df.sort_index()
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(df, style=my_style, type='candle', mav=(5, 10, 20), volume=True,show_nontrading=False,returnfig=True)
if __name__ == '__main__':
root = tkinter.Tk()
Home(root)
root.mainloop()
```