在 Tkinter 框架中绘制图形,在 Tkinter 中更新信息



所以我目前正在开发一个基本的股票程序,我已经能够在我的 tkinter 窗口中获取我的图表(上个月的股票数据(,任何关于如何主动更新我的 tkinter 窗口的提示都会很棒!(仅供参考,我对编程很陌生,这是我的第一年,所以请尝试用基本术语解释!这是我的代码:

import numpy as np
import datetime as dt
import yahoo_finance as yf
import matplotlib.pyplot as plt
from Tkinter import *
import quandl
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

root=Tk()
root.geometry('1400x875')
root.title("Stock Information")

fmain=Frame(root, width=1400, height=900, bg='orange',bd=5)
fmain.place(x=100, y=0)
today=dt.date.today()
thirty_day_graph_frame=Frame(fmain, width=645, height=400,bg='green4',bd=5)
thirty_day_graph_frame.place(x=0, y=444)
thirty_days=dt.timedelta(days=43)   
thirty_days_ago=today-thirty_days

five_yrs_graph_frame=Frame(fmain, width=645, height=400, bg='yellow2',bd=5)
five_yrs_graph_frame.place(x=655, y=444)

five_years=dt.timedelta(days=1825)
five_years_ago=today-five_years

def stock_info(stock_name):

    stock=yf.Share(stock_name)
    stock_price=stock.get_price()
    name_price_label=Label(fmain, text=(stock_name,':', stock_price),font=("Times New Roman",23))
    name_price_label.place(x=400, y=10)
    day_high=quandl.get("WIKI/"+str(stock_name)+".2",start_date=str(today),end_date=str(today))
    high_price_label=Label(fmain, text=(str(day_high)), font=("Times New Roman",20))
    high_price_label.place(x=400, y=100)

    thirty_day_data = quandl.get("WIKI/"+str(stock_name), start_date=str(thirty_days_ago), end_date=str(today),column_index=4) #So quandl.get gives a lot of info, so the column_index=4 is just getting closing prices
    five_year_data = quandl.get("WIKI/"+str(stock_name),start_date=str(five_years_ago), end_date=str(today), column_index=4)

    thirty_day_fig = plt.figure(figsize=(8,4)) 
    plt.plot(thirty_day_data)
    canvas = FigureCanvasTkAgg(thirty_day_fig, master=thirty_day_graph_frame)
    plot_widget = canvas.get_tk_widget()
    plot_widget.place(x=0,y=0)

    five_year_fig=plt.figure(figsize=(8,4))
    plt.plot(five_year_data)
    canvas1=FigureCanvasTkAgg(five_year_fig, master=five_yrs_graph_frame)
    plot_widget1=canvas1.get_tk_widget()
    plot_widget1.place(x=1,y=0)
    root.after(5000, stock_info, stock_name)
apple_button=Button(root,text='AAPL', command=lambda:stock_info('AAPL'))
tesla_button=Button(root,text='TSLA', command=lambda:stock_info('TSLA'))
google_button=Button(root,text='GOOG', command=lambda:stock_info('GOOG'))

apple_button.place(x=10, y=15)
tesla_button.place(x=10, y=45)
google_button.place(x=10,y=75)
root.mainloop()

从一开始就绘制图形的原因是您将命令分配给按钮的方式。解决此问题的一种方法是将命令分配为 lambda 表达式:

apple_button = Button(root, text='AAPL', command=lambda:stock_info('AAPL'))

要让您的 GUI 随着时间的推移自行更新,您可以使用 root.after() 方法创建一个循环:

# Define the figure and canvas outside the loop
fig = plt.Figure()
a = fig.add_subplot(111)
canvas = FigureCanvasTkAgg(fig, master=f1)
canvas.get_tk_widget().grid()
def stock_info(stock_name):
    # Get stock data and plot it on the GUI
    ...
    a.cla()
    a.plot(data)
    canvas.draw()
    # Schedule the function to call itself again after 5 seconds
    root.after(5000, stock_info, stock_name)

最新更新