图形未正确显示在tkinter中

时间:2017-05-21 13:13:47

标签: python-2.7 matplotlib 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('800x800')
root.title("Stock Information")

f1=Frame(root, width=100, height=100)

f1.pack()


today=dt.date.today()
thirty_days=dt.timedelta(days=43)   

thirty_days_ago=today-thirty_days



def stock_info(stock_name):


    stock=yf.Share(stock_name)
    stock_price=stock.get_price()

    name_price_label=Label(f1, text=(stock_name,':', stock_price),font=("Times New Roman",23))
    name_price_label.grid(row=1, column=3)





    data = quandl.get("WIKI/"+str(stock_name), start_date=str(thirty_days_ago), end_date=str(today),column_index=4)

    fig = plt.figure(1)
    t = np.arange(0.0,3.0,0.01)
    s = np.sin(np.pi*t)
    plt.plot(data)

    canvas = FigureCanvasTkAgg(fig, master=f1)
    plot_widget = canvas.get_tk_widget()
    plot_widget.grid()


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

tesla_button=Button(root,text='TSLA', command=stock_info('TSLA'))

google_button=Button(root,text='GOOG', command=stock_info('GOOG'))


apple_button.pack(anchor='w')
tesla_button.pack(anchor='w')
google_button.pack(anchor='w')




root.mainloop()

1 个答案:

答案 0 :(得分:0)

用;

替换你的按钮创作
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'))

这可确保在单击按钮时调用该函数,而不是在创建函数时调用该函数。

然而,无论何时单击按钮而不替换旧图,都会不断添加新图。在添加新绘图之前,您需要重写代码以删除旧绘图。作为匿名用户,我超过了我每天的Quandl呼叫限制,因此我无法完成此部分。

<强>更新

试试这个:

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('800x800')
root.title("Stock Information")

## Create a main frame
fmain=Frame(root, width=100, height=100)
fmain.pack()

#*** Option 2 ***#
# Not needed if you use Original Option in stock_info function
## Create a secondary frame that will be destroy when the next button is clicked
f1=Frame(fmain, width=100, height=100)
f1.pack()

today=dt.date.today()
thirty_days=dt.timedelta(days=43)   

thirty_days_ago=today-thirty_days



def stock_info(stock_name):
    global f1

    #*** Original Option ***#
    ## Destroy the secondary frame if it exists
    # try: 
    #     f1.destroy()
    # except:
    #     pass

    #*** Option 2 ***#
    ## Destroy the secondary frame
    f1.destroy()

    ## Create a secondary frame that will be destroy when the next button is clicked
    f1=Frame(fmain, width=100, height=100)
    f1.pack()

    stock=yf.Share(stock_name)
    stock_price=stock.get_price()

    name_price_label=Label(f1, text=(stock_name,':', stock_price),font=("Times New Roman",23))
    name_price_label.grid(row=0, column=2)


    data = quandl.get("WIKI/"+str(stock_name), start_date=str(thirty_days_ago), end_date=str(today),column_index=4)

    fig = plt.figure(figsize=(10,5)) #Change graph size here
    t = np.arange(0.0,3.0,0.01)
    s = np.sin(np.pi*t)
    plt.plot(data)


    canvas = FigureCanvasTkAgg(fig, master=f1)
    plot_widget = canvas.get_tk_widget()

    #Change graph placement here
    #Any widget you grid in row less than 5 will be above this plot
    #Any widget you grid in column less than 2 (0 or 1) will to the left
    #You can chage this row and column number to whatever is appropriate
    plot_widget.grid(row=5, column=2) 


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.pack(anchor='w')
tesla_button.pack(anchor='w')
google_button.pack(anchor='w')

root.mainloop()