PyQt5从QThread访问GUI元素



我试图从QThread访问GUI元素,但到目前为止没有成功。我使用这个例子是因为我想找到一种简单干净的方式来访问元素,对吗?

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

我得到这个错误:

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

使用信号与主线程通信

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)

最新更新