PyQt5 Access GUI elements from QThread

Question:

Im trying to access GUI Elements from a QThread, but without success so far. im using this example because i want to find an easy and clean way to access the elements, help?

class UI(QMainWindow):
    def __init__(self):
        super(UI, self).__init__()


        uic.loadUi("GUI.ui", self)
        self.thread = {}

        self.listbox_devices = self.findChild(QListWidget, "listbox_devices")

        self.btn_start = self.findChild(QPushButton, "btn_start")
        self.btn_start.clicked.connect(self.Worker_Registration)

   def Worker_Registration(self):
    self.thread = ThreadClass(parent=None)
    self.thread.start()


class ThreadClass(QtCore.QThread):

    any_signal = QtCore.pyqtSignal(int)

    def __init__(self,parent=None):
        super(ThreadClass, self).__init__(parent)

        self.is_running = True
    def run(self):
        devices = UI.listbox_devices.count()
        if not devices > 0: UI.textbox_log.append("No Device Connected"); return

app = QApplication(sys.argv)
UIWindow = UI()
UIWindow.show()
app.exec_()

I get this error:

    py", line 149, in run
    devices = UI.listbox_devices.count()
AttributeError: type object 'UI' has no attribute 'listbox_devices'
Asked By: Xox Xox

||

Answers:

using signals to communicate with the main thread

class UI(QMainWindow):
    def __init__(self):
        super(UI, self).__init__()


        uic.loadUi("GUI.ui", self)
        self.thread = {}

        self.listbox_devices = self.findChild(QListWidget, "listbox_devices")

        self.btn_start = self.findChild(QPushButton, "btn_start")
        self.btn_start.clicked.connect(self.Worker_Registration)

   def Worker_Registration(self):
       self.thread = ThreadClass(parent=None)
       number_devices = self.listbox_devices.count()
       self.thread.any_signal.connect(number_devices, self.append_log)
       self.thread.start()
    
   def append_log(self, value):
       if value == 0: 
           self.textbox_log.append("No Device Connected")


class ThreadClass(QtCore.QThread):

    any_signal = QtCore.pyqtSignal(int)

    def __init__(self, number_devices,  parent=None):
        super(ThreadClass, self).__init__(parent)
        self.number_devices = number_devices
        self.is_running = True
    def run(self):
        if not self.number_devices > 0: self.any_signal.emit(0)
Answered By: Lucas M. Uriarte
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.