这可能是我自己某个地方的愚蠢错误;当我尝试实现此线程代码以更新PyQt5 gui的进度条级别时,出现以下错误:
Traceback (most recent call last):
File "threadtest.py", line 62, in <module>
app=MainWindow()
File "threadtest.py", line 35, in __init__
self.threadclass = ThreadClass()
File "threadtest.py", line 49, in __init__
self.getLevels()
File "threadtest.py", line 53, in getLevels
self.battery.emit()
AttributeError: 'int' object has no attribute 'emit'
[1:1:0100/000000.677780:ERROR:broker_posix.cc(43)] Invalid node channel message
代码是:
class MainWindow(QtWidgets.QMainWindow, gs.Ui_MainWindow):
def __init__(self, parent = None):
super(MainWindow, self).__init__(parent = parent)
self.setupUi(self)
self.threadclass = ThreadClass()
self.threadclass.start()
self.threadclass.battery.connect(self.updateLevels)
def updateLevels(self, val):
self.battery1bar.setValue(val)
class ThreadClass(QtCore.QThread):
battery = pyqtSignal(int)
def __init__(self, parent = None):
super(ThreadClass, self).__init__(parent = parent)
self.getLevels()
def getLevels(self):
self.battery=7
self.battery.emit()
gs.Ui_MainWindow.setupUi是由qt设计器自动生成的ui。 getLevels最终将动态检索电池电量。
答案 0 :(得分:2)
您正在隐藏信号,因为您正在为其分配值:
self.battery = 7
您必须做的是将该值作为参数传递给emit()
:
def getLevels(self):
self.battery.emit(7)