Hello when I put the stock symbol and hit the ok button it works fine but when hit the ok button again to see another stock data the previous chart window doesn’t delete but the chart becomes two of them and the newly generated chart is below the previous chart. I want the previous chart to be deleted when ok button is pressed and generate the new chart.
stock = input()
# label
ttk.Label(left_frame, text='From: ').grid(column=0, row=3)
ttk.Label(left_frame, text='To: ').grid(column=0, row=4)
# from
from_cal = DateEntry(left_frame, selectmode='day', year=2020, month=1, day=1)
from_cal.grid(column=1, row=3)
# to
to_cal = DateEntry(left_frame, selectmode='day', year=today.year, month=today.month, day=today.day)
to_cal.grid(column=1, row=4)
def plot_candlestick(ax, df):
try:
df_reset = df.reset_index()
df_reset['Date'] = df_reset['Date'].map(mdates.date2num)
candlestick_ohlc(ax, df_reset.values, width=0.6, colorup='r', colordown='b')
ax.xaxis_date()
ax.grid(True)
except Exception as e:
msg.showerror('test', 'Error: ' + str(e))
def plot_line(ax, df):
try:
for column in df.columns:
if column != 'Date':
ax.plot(df.index, df[column], label=column)
ax.legend()
ax.grid(True)
except Exception as e:
msg.showinfo('test', 'Error: ' + str(e))
def plot_data(symbol, start_date, end_date):
try:
# reading data
df = fdr.DataReader(symbol, start_date, end_date)
fig_candlestick = plt.figure(figsize=(8, 6))
ax_candlestick = fig_candlestick.add_subplot(111)
plot_candlestick(ax_candlestick, df)
fig_line = plt.figure(figsize=(8, 6))
ax_line = fig_line.add_subplot(111)
plot_line(ax_line, df)
# Embedding candlestick chart
canvas_candlestick = FigureCanvasTkAgg(fig_candlestick, master=frame_candlestick)
canvas_candlestick.draw()
canvas_candlestick.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
# Adding navigation toolbar for candlestick chart
toolbar_candlestick = NavigationToolbar2Tk(canvas_candlestick, frame_toolbar_candlestick)
toolbar_candlestick.update()
canvas_candlestick.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
win.update_idletasks()
win.geometry(f"{win.winfo_reqwidth()+50}x{win.winfo_reqheight()+50}")
except Exception as e:
msg.showerror('test', 'Error: ' + str(e))
def left_frame_ok_func():
global stock
if stock != '':
plot_data(stock, start_date=from_cal.get_date(), end_date=to_cal.get_date())
else:
msg.showerror('test', 'Please imput stock')
# Frame for candlestick chart and toolbar
frame_candlestick = ttk.Frame(win)
frame_candlestick.grid(row=2, column=0, padx=10, pady=10, sticky="ew")
frame_toolbar_candlestick = ttk.Frame(win)
frame_toolbar_candlestick.grid(row=3, column=0, padx=10, pady=10, sticky="ew")
# left frame ok button
left_frame_ok = ttk.Button(left_frame, text='OK', command=left_frame_ok_func)
left_frame_ok.grid(column=0, row=6)