How do I iterate a graph forward in a python tkinter program?

Viewed 50

I'm trying to make program that allows one to pretend they're playing the stock market from old stock records. I have been able to get the records, display the information, and actually do some simple trading, but I haven't been able to get the program to iterate forward.

Right now, it opens up a window with a graph on the left. In the right it has several text input boxes and a button that to complete a round of trading. It all works, but when the user presses the button, I can't get the program to iterate forward. It's like it's all stuck on the same day.

I've tried using loops, but it didn't work. The new window didn't replace the old one; it tried and failed to cover it up. The new window was down and to the right of the old one. The graph wouldn't accept new data, it just extended the x-axis past it.

import tkinter as tk
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

#import data from .csv files
AAPL = pd.read_csv('AAPL.csv', index_col=0)
NOK = pd.read_csv('NOK.csv', index_col=0)
AMD = pd.read_csv('AMD.csv', index_col=0)
GE = pd.read_csv('GE.csv', index_col=0)
MSFT = pd.read_csv('MSFT.csv', index_col=0)

stocks= pd.DataFrame()
stocks['AAPL']=AAPL['Close']
stocks['NOK']=NOK['Close']
stocks['AMD']=AMD['Close']
stocks['GE']=GE['Close']
stocks['MSFT']=MSFT['Close']

root=tk.Tk()
#starting conditions
past=1
present=30
wallet=100

def buttonpress():
    #number of each stock bought
    x1 = float(entry1.get())
    x2 = float(entry2.get())
    x3 = float(entry3.get())
    #money change after transaction
    stock1 = float(stocks['AAPL'][present+6:present+7]) -float(stocks['AAPL'][present-1:present])
    stock2 = float(stocks['NOK'][present+6:present+7]) -float(stocks['NOK'][present-1:present])
    stock3 = float(stocks['AMD'][present+6:present+7]) -float(stocks['AMD'][present-1:present])

    #output, the money gained and lost
    label1 = tk.Label(root, text=stock1*x1)
    label2 = tk.Label(root, text=stock2*x2)
    label3 = tk.Label(root, text=stock3*x3)
    #output placement
    canvas1.create_window(300, 100, window=label1)
    canvas1.create_window(300, 140, window=label2)
    canvas1.create_window(300, 180, window=label3)

#create graph when window opens
df1 = stocks[past:present]
figure1 = plt.Figure(figsize=(7, 6), dpi=100)
ax1 = figure1.add_subplot(111)
bar1 = FigureCanvasTkAgg(figure1, root)
bar1.get_tk_widget().pack(side=tk.LEFT, fill=tk.BOTH)
df1.plot(kind='line', legend=True, ax=ax1)
legend = ax1.legend(loc='upper left')
ax1.xaxis.set_ticks(np.arange(past, present, (present-past)/5))
ax1.set_title('stocks')

canvas1 = tk.Canvas(root, width=400, height=300)
canvas1.pack()

# text inputs for the user
entry1 = tk.Entry(root)
entry2 = tk.Entry(root)
entry3 = tk.Entry(root)

canvas1.create_window(100, 100, window=entry1)
canvas1.create_window(100, 140, window=entry2)
canvas1.create_window(100, 180, window=entry3)
#labels
canvas1.create_text(200, 100, text='Apple')
canvas1.create_text(200, 140, text='Nokia')
canvas1.create_text(200, 180, text='AMD')



button1 = tk.Button(text='Get the Square Root', command=buttonpress)
canvas1.create_window(200, 240, window=button1)

root.mainloop()
0 Answers
Related