m0_56279032 2022-11-10 15:53 采纳率: 50%
浏览 29

如何实现matplotlib在tkinter上点击刷新k线

如何实现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()

```

  • 写回答

2条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-11-10 18:34
    关注
    评论

报告相同问题?

问题事件

  • 创建了问题 11月10日

悬赏问题

  • ¥15 上传图片时提交的存储类型
  • ¥15 Ubuntu开机显示器只显示kernel,是没操作系统(相关搜索:显卡驱动)
  • ¥15 VB.NET如何绘制倾斜的椭圆
  • ¥15 在rhel8中安装qemu-kvm时遇到“cannot initialize crypto:unable to initialize gcrypt“报错”
  • ¥15 arbotix没有/cmd_vel话题
  • ¥15 paddle库安装时报错提示需要安装common、dual等库,安装了上面的库以后还是显示报错未安装,要怎么办呀?
  • ¥20 找能定制Python脚本的
  • ¥15 odoo17的分包重新供应路线如何设置?可从销售订单中实时直接触发采购订单或相关单据
  • ¥15 用C语言怎么判断字符串的输入是否符合设定?
  • ¥15 通信专业本科生论文选这两个哪个方向好研究呀