Python Forum
Printing Variable into the label - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: Printing Variable into the label (/thread-25006.html)



Printing Variable into the label - ardvci - Mar-14-2020

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()



RE: Printing Variable into the label - joe_momma - Mar-18-2020

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))



RE: Printing Variable into the label - ardvci - Mar-18-2020

(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()



RE: Printing Variable into the label - joe_momma - Mar-18-2020

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


RE: Printing Variable into the label - ardvci - Mar-19-2020

(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.


RE: Printing Variable into the label - joe_momma - Mar-19-2020

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