Search code examples
pythontkinteryahoo-finance

I am working with Tk inter and yahoo_finance


I want to write a program such that I can enter in a pop-up entry box the stock symbol of my choice:

I know how to create an entry box in TKinter generally, but I don't know how to employ the .get() method here:

import datetime
import tkinter
from tkinter import *
import matplotlib.finance as finance

root = Tk()
E1 = Entry(root, bd=8)
E1.pack(fill=Y)
root.mainloop()
startdate = datetime.date(2013,1,1)
today = enddate = datetime.date.today()
ticker = 'I WANT THIS TO COME FROM THE ENTRY BOX '?
fh = finance.fetch_historical_yahoo(ticker, startdate, enddate)

Solution

  • You could use StringVar. For example

    import datetime
    
    import tkinter
    
    from tkinter import *
    
    import matplotlib.finance as finance
    
    root = Tk()    
    
    # create a StrringVar
    ticker_entry_var = StringVar()
    
    E1 = Entry(root, bd=8, textvariable=ticker_entry_var)
    
    E1.pack(fill=Y)
    
    root.mainloop()
    
    startdate = datetime.date(2013,1,1) 
    today = enddate = datetime.date.today()
    
    # get value from the entry box 
    ticker = ticker_entry_var.get()
    print(ticker)
    

    Now, when you close the tk window, ticker will have the value from the stringvar.