Python Forum
Printing Variable into the label
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Printing Variable into the label
#1
Hi everyone , I'm trying to make an app that can show the price of a stock and also the graphics of the stock. The part of Graph is working but I could not pass the DATA variable into the Label that shows on the screen(You can see the results in terminal).So could you help me on this issue any contrubutions will help :)
import datetime as dt
import matplotlib.pyplot as plt
import pandas_datareader.data as web
import tkinter as tk
from PIL import ImageTk, Image
import requests
import pandas as pd 
from alpha_vantage.techindicators import TechIndicators
from alpha_vantage.timeseries import TimeSeries

root=tk.Tk()
inputValue=tk.StringVar()
DATA=tk.StringVar()
def format_response(DATA):
	try:
		openn = DATA['open']
		high = DATA['high']
		low = DATA['low']

		final_str = 'open: %s \nhigh: %s \nLow: %s' % (openn,high,low)
	except:
		final_str = 'There was a problem retrieving that information'

	return final_str
def get_name():
    inputValue=Entry.get()
    ts=TimeSeries(key='Q5PZV4ADMZIUI2VT',output_format='pandas')
    DATA, meta_data=ts.get_daily(symbol=inputValue)
    print(DATA)
def open_graph():
	inputValue=Entry.get()
	start=dt.datetime(2020,2,14)
	end=dt.datetime(2020,3,14)
	df=web.DataReader(inputValue,'yahoo',start,end)
	df['Open'].plot()
	df['Close'].plot(color='r')
	plt.show()
	
	
	



tk.Canvas()
photo = ImageTk.PhotoImage(Image.open("D:\\Profile\\Desktop\\Photos\\stockmrkt.jpg"))
logo = tk.Label(root, image=photo)
logo.pack()
Frame=tk.Frame(root,bg="#80c1ff",bd=5)
Frame.place(relx=0.5,rely=0.1,relwidth=0.75,relheight=0.1,anchor='n')

Entry=tk.Entry(Frame,font=40,bd=4)
Entry.place(relwidth=0.3,relheight=1)

Button=tk.Button(Frame,text="Stock Values",bg="Grey",font=40,command=lambda:get_name())
Button.place(relx=0.48,rely=0.1,anchor='n',relwidth=0.32)

Buttongraph=tk.Button(Frame,text="Stock Graphs",bg="Grey",font=40,command=lambda:open_graph())
Buttongraph.place(relx=0.83,rely=0.1,anchor='n',relwidth=0.32)

lower_frame=tk.Entry(root,bg="#80c1ff",bd=5)
lower_frame.place(relx=0.5,rely=0.25,relwidth=0.75,relheight=0.6,anchor='n')



label=tk.Label(lower_frame,font=('courier',18),anchor='nw',justify='left',bd=4)
label.place(relwidth=1,relheight=1)


tk.mainloop()
Reply
#2
I'm missing some modules to run your script but from your code it looks like the
Quote:print(DATA)
is the information you want to create labels with.
If you could post a snip it of it you might get a better answer. I would make the data into a list or dictionary and use a for loop to create labels. When you use a StringVar() it's for only one label and that label text is changing. https://effbot.org/tkinterbook/variable.htm
What is your data type?
print(type(DATA))
Reply
#3
(Mar-18-2020, 02:18 AM)joe_momma Wrote: I'm missing some modules to run your script but from your code it looks like the
Quote:print(DATA)
is the information you want to create labels with.
If you could post a snip it of it you might get a better answer. I would make the data into a list or dictionary and use a for loop to create labels. When you use a StringVar() it's for only one label and that label text is changing. https://effbot.org/tkinterbook/variable.htm
What is your data type?
print(type(DATA))
Well actually I found a solution to do that. The solution is I create a new window in the function to do that so that I can use this variable into the label that I Create here is the code
def get_name():
    inputValue=Entry.get()
    ts=TimeSeries(key='-----------',output_format='pandas')
    root=tk.Toplevel()
    tk.Canvas()
    Label=tk.Label()
    photo = ImageTk.PhotoImage(Image.open("D:\\Profile\\Desktop\\Photos\\stockmrkt.jpg"))
    logo = tk.Label(root, image=photo)
    logo.pack()
    lower_frame=tk.Entry(root,bg="#80c1ff",bd=5)
    lower_frame.place(relx=0.5,rely=0.25,relwidth=0.75,relheight=0.6,anchor='n')
    label=tk.Label(lower_frame,text=DATA,font=('courier',10),anchor='nw',justify='left',bd=4)
    label.place(relwidth=1,relheight=1)
    tk.mainloop()
Reply
#4
I finally got the modules I created a Text widget and dumped the info into it like this-
import datetime as dt
import matplotlib.pyplot as plt
import pandas_datareader.data as web
import tkinter as tk
from PIL import ImageTk, Image
import requests
import pandas as pd 
from alpha_vantage.techindicators import TechIndicators
from alpha_vantage.timeseries import TimeSeries
 
root=tk.Tk()
inputValue=tk.StringVar()
DATA=tk.StringVar()
def format_response(DATA):
    try:
        openn = DATA['open']
        high = DATA['high']
        low = DATA['low']
 
        final_str = 'open: %s \nhigh: %s \nLow: %s' % (openn,high,low)
    except:
        final_str = 'There was a problem retrieving that information'
 
    return final_str
def get_name():
    inputValue=Entry.get()
    ts=TimeSeries(key='Q5PZV4ADMZIUI2VT',output_format='pandas')
    DATA, meta_data=ts.get_daily(symbol=inputValue)
    my_text.delete('1.0','end')
    my_text.insert('end',DATA)
    #print(DATA)
    #print(meta_data)
    #print(type(DATA))
def open_graph():
    inputValue=Entry.get()
    start=dt.datetime(2020,2,14)
    end=dt.datetime(2020,3,14)
    df=web.DataReader(inputValue,'yahoo',start,end)
    df['Open'].plot()
    df['Close'].plot(color='r')
    plt.show()
     
     
     
 
 
 
tk.Canvas()
photo = ImageTk.PhotoImage(Image.open("/path/to/my/pictures.jpg"))#use you own path
logo = tk.Label(root, image=photo)
logo.pack()
Frame=tk.Frame(root,bg="#80c1ff",bd=5)
Frame.place(relx=0.5,rely=0.1,relwidth=0.75,relheight=0.1,anchor='n')
 
Entry=tk.Entry(Frame,font=40,bd=4)
Entry.place(relwidth=0.3,relheight=1)
 
Button=tk.Button(Frame,text="Stock Values",bg="Grey",font=40,command=lambda:get_name())
Button.place(relx=0.48,rely=0.1,anchor='n',relwidth=0.32)
 
Buttongraph=tk.Button(Frame,text="Stock Graphs",bg="Grey",font=40,command=lambda:open_graph())
Buttongraph.place(relx=0.83,rely=0.1,anchor='n',relwidth=0.32)
 
lower_frame=tk.Frame(root,bg="white",bd=5)
lower_frame.pack(side='bottom')
 
 
 
my_text=tk.Text(lower_frame,font=('courier',18),relief='sunken', wrap='word')
my_text.pack(side='bottom')
 
 
tk.mainloop()
I used pack it was quick and easy it made the Entry larger
Reply
#5
(Mar-18-2020, 11:06 PM)joe_momma Wrote: I finally got the modules I created a Text widget and dumped the info into it like this-
import datetime as dt
import matplotlib.pyplot as plt
import pandas_datareader.data as web
import tkinter as tk
from PIL import ImageTk, Image
import requests
import pandas as pd 
from alpha_vantage.techindicators import TechIndicators
from alpha_vantage.timeseries import TimeSeries
 
root=tk.Tk()
inputValue=tk.StringVar()
DATA=tk.StringVar()
def format_response(DATA):
    try:
        openn = DATA['open']
        high = DATA['high']
        low = DATA['low']
 
        final_str = 'open: %s \nhigh: %s \nLow: %s' % (openn,high,low)
    except:
        final_str = 'There was a problem retrieving that information'
 
    return final_str
def get_name():
    inputValue=Entry.get()
    ts=TimeSeries(key='Q5PZV4ADMZIUI2VT',output_format='pandas')
    DATA, meta_data=ts.get_daily(symbol=inputValue)
    my_text.delete('1.0','end')
    my_text.insert('end',DATA)
    #print(DATA)
    #print(meta_data)
    #print(type(DATA))
def open_graph():
    inputValue=Entry.get()
    start=dt.datetime(2020,2,14)
    end=dt.datetime(2020,3,14)
    df=web.DataReader(inputValue,'yahoo',start,end)
    df['Open'].plot()
    df['Close'].plot(color='r')
    plt.show()
     
     
     
 
 
 
tk.Canvas()
photo = ImageTk.PhotoImage(Image.open("/path/to/my/pictures.jpg"))#use you own path
logo = tk.Label(root, image=photo)
logo.pack()
Frame=tk.Frame(root,bg="#80c1ff",bd=5)
Frame.place(relx=0.5,rely=0.1,relwidth=0.75,relheight=0.1,anchor='n')
 
Entry=tk.Entry(Frame,font=40,bd=4)
Entry.place(relwidth=0.3,relheight=1)
 
Button=tk.Button(Frame,text="Stock Values",bg="Grey",font=40,command=lambda:get_name())
Button.place(relx=0.48,rely=0.1,anchor='n',relwidth=0.32)
 
Buttongraph=tk.Button(Frame,text="Stock Graphs",bg="Grey",font=40,command=lambda:open_graph())
Buttongraph.place(relx=0.83,rely=0.1,anchor='n',relwidth=0.32)
 
lower_frame=tk.Frame(root,bg="white",bd=5)
lower_frame.pack(side='bottom')
 
 
 
my_text=tk.Text(lower_frame,font=('courier',18),relief='sunken', wrap='word')
my_text.pack(side='bottom')
 
 
tk.mainloop()
I used pack it was quick and easy it made the Entry larger
Wow ! I did not think that this was possible ,thanks for your respond I learned a new way of doing that.
Reply
#6
no worries, a couple things to think about-
Quote:Buttongraph=tk.Button(Frame,text="Stock Graphs",bg="Grey",font=40,command=lambda:open_graph()
lambda is for someone who's lazy and they don't want to write a function, you wrote one so you could do-
Buttongraph=tk.Button(Frame,text="Stock Graphs",bg="Grey",font=40,command= open_graph
and skip the lambda
You could make a label for your Entry widget describing the parameters to be expected entered- for example 'Stock Symbol lookup'
Your homework is to make your script into a class... nice start keep going
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  update text variable on label with keypress knoxvilles_joker 3 4,835 Apr-17-2021, 11:21 PM
Last Post: knoxvilles_joker
  [Tkinter] Trying to display a variable in a label Jordansonatina 2 17,669 Oct-31-2019, 06:28 PM
Last Post: Jordansonatina
  [Tkinter] HOW TO: Set [Label].config() by variable name reference? gazoxtapod 4 4,877 Apr-17-2019, 09:57 PM
Last Post: gazoxtapod
  [Tkinter] can't update label to include variable foxtreat 2 3,605 Dec-01-2018, 07:16 AM
Last Post: jfong
  printing option menu variable in label in Tkinter SmokerX 1 6,590 Jan-18-2018, 07:36 PM
Last Post: SmokerX

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020