使用Python构建 GUI 应用程序以获取实时股票价格
股票价格是某人愿意为股票支付的最高金额。在本文中,我们将编写获取每家公司实时股价的代码,并将其与 GUI 应用程序绑定。
需要的模块
Yahoo_fin:该模块用于抓取历史股价数据,以及提供有关市值、股息收益率以及主要交易所的股票的当前信息。要安装此模块,请在终端中键入以下命令。
pip install yahoo_fin
下面是 GUI 的样子:-
让我们编写代码来获取股票数据。
导入 yahoo_fin 模块。
Python3
from yahoo_fin import stock_info
Python3
stock_info.get_live_price("AMZN")
Python3
from yahoo_fin import stock_info
from tkinter import *
def stock_price():
price = stock_info.get_live_price(e1.get())
Current_stock.set(price)
master = Tk()
Current_stock = StringVar()
Label(master, text="Company Symbol : ").grid(row=0, sticky=W)
Label(master, text="Stock Result:").grid(row=3, sticky=W)
result2 = Label(master, text="", textvariable=Current_stock,
).grid(row=3, column=1, sticky=W)
e1 = Entry(master)
e1.grid(row=0, column=1)
b = Button(master, text="Show", command=stock_price)
b.grid(row=0, column=2, columnspan=2, rowspan=2, padx=5, pady=5)
mainloop()
使用stock_info.get_live_price()方法获取实时股票价格。
蟒蛇3
stock_info.get_live_price("AMZN")
输出:
3198.93994140625
注意:请访问此站点以获取公司符号,例如 Amazon 是 AMZN,Reliance 是 RELFF。
使用 Tkinter 的股票价格应用程序
该脚本将上述实现实现到 GUI 中。
蟒蛇3
from yahoo_fin import stock_info
from tkinter import *
def stock_price():
price = stock_info.get_live_price(e1.get())
Current_stock.set(price)
master = Tk()
Current_stock = StringVar()
Label(master, text="Company Symbol : ").grid(row=0, sticky=W)
Label(master, text="Stock Result:").grid(row=3, sticky=W)
result2 = Label(master, text="", textvariable=Current_stock,
).grid(row=3, column=1, sticky=W)
e1 = Entry(master)
e1.grid(row=0, column=1)
b = Button(master, text="Show", command=stock_price)
b.grid(row=0, column=2, columnspan=2, rowspan=2, padx=5, pady=5)
mainloop()
输出: