The objective is to have the label named 'sandp_value_label' show live data which is provided by the web scraper function live_indices() which is a parameter of the set_sandp_value() function -
def live_indices():
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36'}
all_indices_values = {}
symbols = ['GSPC', 'DJI', 'IXIC', 'FTSE', 'NSEI', 'FCHI', 'N225', 'GDAXI', 'IMOEX.ME', '000001.SS']
for ticker in symbols:
url = f'https://uk.finance.yahoo.com/lookup/all?s={ticker}'
tmp_res = requests.get(url, headers=headers)
tmp_res.raise_for_status()
soup = bs4.BeautifulSoup(tmp_res.text, 'html.parser')
indices_price_value = soup.select('#Main tbody>tr td')[2].text
all_indices_values[ticker] = indices_price_value
return all_indices_values
The function to display the label is as follows:
def set_sandp_value(live_indices):
self.sandp_value_label.setGeometry(QtCore.QRect(10, 58, 70, 16))
self.sandp_value_label.setFont(QFont('Arial', 12))
self.sandp_value_label.setText("$" + live_indices['GSPC'])
This is the code to initialise the QTimer which calls the set_sandp_value() function, where the error is raised -
# Functionality for stock price live updates
self.qTimer = QtCore.QTimer(self.centralwidget)
self.qTimer.setInterval(1000)
self.qTimer.timeout.connect(lambda: self.set_sandp_value(live_indices()))
self.qTimer.start()
However I receive the following self.qTimer.timeout.connect(lambda: self.set_sandp_value(live_indices())) AttributeError: 'Ui_MainWindow' object has no attribute 'set_sandp_value' Abort trap: 6.
I'm not sure how to resolve this error and whether that will result in the application providing live data.
Reproducible example:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QTimer, QDateTime, QBasicTimer
from PyQt5.QtGui import QFont
from imagewindow import Ui_external_image_window # External window to display graphs
import bs4
import requests
import threading
class Ui_MainWindow(object):
def openWindow(self):
"""Initialises external window QFrame (which holds the chart (.png) image.)"""
self.window = QtWidgets.QFrame()
self.ui = Ui_external_image_window()
self.ui.setupUi(self.window)
self.window.show()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(900, 650)
MainWindow.setTabShape(QtWidgets.QTabWidget.Rounded)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
# Functionality for stock price live updates
# self.qTimer = QtCore.QTimer(self.centralwidget)
# self.qTimer.setInterval(1000)
# self.qTimer.timeout.connect(lambda: self.set_sandp_value(live_indices()))
# self.qTimer.start()
self.label_30 = QtWidgets.QLabel(self.centralwidget)
self.label_30.setGeometry(QtCore.QRect(12, 40, 25, 20))
self.label_30.setText("")
self.label_30.setPixmap(QtGui.QPixmap("../gui/../icons/flags/United-States.png"))
self.label_30.setScaledContents(True)
self.label_30.setObjectName("label_30")
self.sandp_value_label = QtWidgets.QLabel(self.centralwidget)
self.sandp_value_label.setGeometry(QtCore.QRect(10, 58, 60, 16))
self.sandp_value_label.setText("")
self.sandp_value_label.setObjectName("sandp_value_label")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 900, 37))
self.menubar.setObjectName("menubar")
self.menuNew = QtWidgets.QMenu(self.menubar)
self.menuNew.setObjectName("menuNew")
self.menuEdit = QtWidgets.QMenu(self.menubar)
self.menuEdit.setObjectName("menuEdit")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionChart = QtWidgets.QAction(MainWindow)
self.actionChart.setObjectName("actionChart")
self.actionRSS_Feed = QtWidgets.QAction(MainWindow)
self.actionRSS_Feed.setObjectName("actionRSS_Feed")
self.menuNew.addAction(self.actionChart)
self.menuNew.addAction(self.actionRSS_Feed)
self.menubar.addAction(self.menuNew.menuAction())
self.menubar.addAction(self.menuEdit.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def live_indices():
"""Acquire stock value from Yahoo Finance using stock symbol as key. Then assign the relevant variable to the respective value.
ie. 'GSPC' equates to the value keyed to 'GSPC' stock indices_price_value.
"""
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36'}
all_indices_values = {}
symbols = ['GSPC', 'DJI', 'IXIC', 'FTSE', 'NSEI', 'FCHI', 'N225', 'GDAXI', 'IMOEX.ME', '000001.SS']
for ticker in symbols:
url = f'https://uk.finance.yahoo.com/lookup/all?s={ticker}'
tmp_res = requests.get(url, headers=headers)
tmp_res.raise_for_status()
soup = bs4.BeautifulSoup(tmp_res.text, 'html.parser')
indices_price_value = soup.select('#Main tbody>tr td')[2].text
all_indices_values[ticker] = indices_price_value
return all_indices_values
def set_sandp_value(live_indices):
"""Acquire real-time stock indices data then apply to respective stock value"""
self.sandp_value_label.setGeometry(QtCore.QRect(10, 58, 70, 16))
self.sandp_value_label.setFont(QFont('Arial', 12))
self.sandp_value_label.setText("$" + live_indices['GSPC'])
display_stock_price_thread_1 = threading.Thread(target=set_sandp_value, args=[live_indices()])
display_stock_price_thread_1.start()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Stock Fund Tracker App"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())