'Retrieving data from Firebase and display it on the PyQt5

i am new in python and i have a project to making GUI for monitoring and i am using pyqt5 and pyrebase to retrieving data from firebase real time database. One thing that made me confuse is, how to return the value from data retrieving thread class and display it to the gui label when i click the start button. Here is the code:

from PyQt5.QtCore import QRunnable, QThreadPool, QTimer
from PyQt5.QtWidgets import QApplication, QMainWindow
from gui import UiMainWindow
import pyrebase
import time
import sys


config = {
    "apiKey": "...",
    "authDomain": "...",
    "databaseURL": "...",
    "projectId": "...",
    "storageBucket": "...",
    "messagingSenderId": "...",
}

firebase = pyrebase.initialize_app(config)
db = firebase.database()
# stream_data = db.child("test").child("LDR").get().val()


class Runnable(QRunnable):
    def __init__(self):
        super().__init__()

    def run(self):
        while True:
            global stream_data
            stream_data = db.child("test").child("LDR").get().val()
            print(stream_data)
            time.sleep(1)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    pool = QThreadPool.globalInstance()
    main = QMainWindow()
    ui = UiMainWindow(main)
    runnable = Runnable()
    pool.start(runnable)
    main.show()

    sys.exit(app.exec())

GUI Class

from PyQt5 import QtCore, QtWidgets  
from PyQt5.QtWidgets import QMainWindow 


class UiMainWindow(QMainWindow):
    def __init__(self, mainwin):
        super(UiMainWindow, self).__init__()

        mainwin.setObjectName("MainWindow")
        mainwin.resize(460, 330)
        mainwin.setWindowTitle("AGV MONITOR")

        self.centralwidget = QtWidgets.QWidget(mainwin)
        self.centralwidget.setObjectName("centralwidget")
        mainwin.setCentralWidget(self.centralwidget)

        self.ldr_val = QtWidgets.QLabel(self.centralwidget)
        self.ldr_val.setGeometry(QtCore.QRect(10, 10, 71, 21))
        self.ldr_val.setObjectName("ldr_val")
        self.ldr_val.setText("0")

        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(10, 40, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.pushButton.setText("Start")
        self.pushButton.clicked.connect(self.start_button)

        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(10, 70, 75, 23))
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_2.setText("Stop")
        self.pushButton_2.clicked.connect(self.stop_button)

        self.menubar = QtWidgets.QMenuBar(mainwin)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 460, 22))
        self.menubar.setObjectName("menubar")
        mainwin.setMenuBar(self.menubar)

        self.statusbar = QtWidgets.QStatusBar(mainwin)
        self.statusbar.setObjectName("statusbar")
        mainwin.setStatusBar(self.statusbar)


    def start_button(self):
        self.ldr_val.setText()

    def stop_button(self):
        self.ldr_val.setText("0")

so, what parameter that i have to write on self.ldr_val.setText() in start_button() method?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source