我有一段正在运行的代码,两个线程之间的通信和状态。我遇到了如何根据发射值更新QLineEdit字段的问题。到目前为止,它与评论的行很好地工作,但它不是我想要的...如何修改show_process函数来完成这项工作......一些帮助将不仅仅是受欢迎的?
import sys, time
from PyQt4 import QtGui, QtCore
class Window(QtGui.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(50, 50, 500, 200)
self.setWindowTitle("TEST APP!")
self.home()
def Label(self, name, width, length, fontSize) :
label_name = QtGui.QLabel(name, self)
label_name.move(width,length)
label_font = label_name.font()
label_font.setPointSize(fontSize)
label_name.setFont(label_font)
def Field(self, TrueFalse, width, length, startText) :
field_name = QtGui.QLineEdit(self)
field_name.setDisabled(TrueFalse)
field_name.move(width, length)
field_name.setAlignment(QtCore.Qt.AlignCenter)
field_name.setText(startText)
def home(self):
self.Label('TEST1', 10, 60, 10)
self.Label('TEST2', 10, 120, 10)
self.Field(True, 130, 60, 'Ready') # first one
self.Field(True, 130, 120, 'Ready') # second one
self.start = QtGui.QPushButton("start", self)
self.start.clicked.connect(self.startPressed)
self.start.move(260, 20)
self.stop = QtGui.QPushButton("Stop", self)
self.stop.clicked.connect(self.stopPressed)
self.stop.move(380, 20)
self.show()
def startPressed(self):
self.get_thread_start = Start_Process('239.200.10.1', 50010)
self.stop.clicked.connect(self.get_thread_start.terminate)
self.start.setDisabled(True)
self.get_thread_start.updated.connect(self.show_process)
self.get_thread_start.start()
def stopPressed(self):
self.start.setDisabled(False)
self.get_thread_start.running = False
def show_process(self, data):
if str(data) == '1' :
#self.textbox1.setText(str(data))
pass
elif str(data) == '0' :
#self.textbox2.setText(str(data))
pass
class Start_Process(QtCore.QThread):
updated = QtCore.pyqtSignal(int)
running = True
def __init__(self, mcstaddr, mcstport):
QtCore.QThread.__init__(self)
self.counter = 0
self.array = [1,0,1,0,1,0,1,0,1]
def run(self):
while self.running:
for i in self.array :
self.updated.emit(i)
time.sleep(0.5)
def main():
app = QtGui.QApplication(sys.argv)
GUI = Window()
GUI.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
答案 0 :(得分:1)
访问窗口小部件的一种简单方法是创建容器,在这种情况下,我们选择一个字典:
self.field_dict = {}
然后我们添加Field()
方法,我们将QLineEdits
添加到字典中。
def Field(self, TrueFalse, width, length, startText, key) :
field_name = QtGui.QLineEdit(self)
...
self.field_dict[key] = field_name
然后我们可以通过密钥获取QLineEdit
。
def show_process(self, key, data):
self.field_dict[key].setText(data)
完成示例:
import sys, time
from PyQt4 import QtGui, QtCore
class Window(QtGui.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(50, 50, 500, 200)
self.setWindowTitle("TEST APP!")
self.field_dict = {}
self.home()
def Label(self, name, width, length, fontSize) :
label_name = QtGui.QLabel(name, self)
label_name.move(width,length)
label_font = label_name.font()
label_font.setPointSize(fontSize)
label_name.setFont(label_font)
def Field(self, TrueFalse, width, length, startText, key) :
field_name = QtGui.QLineEdit(self)
field_name.setDisabled(TrueFalse)
field_name.move(width, length)
field_name.setAlignment(QtCore.Qt.AlignCenter)
field_name.setText(startText)
self.field_dict[key] = field_name
def home(self):
self.Label('TEST1', 10, 60, 10)
self.Label('TEST2', 10, 120, 10)
self.Field(True, 130, 60, 'Ready', 0) # first one
self.Field(True, 130, 120, 'Ready', 1) # second one
self.start = QtGui.QPushButton("start", self)
self.start.clicked.connect(self.startPressed)
self.start.move(260, 20)
self.stop = QtGui.QPushButton("Stop", self)
self.stop.clicked.connect(self.stopPressed)
self.stop.move(380, 20)
self.show()
def startPressed(self):
self.get_thread_start = Start_Process('239.200.10.1', 50010)
self.stop.clicked.connect(self.get_thread_start.terminate)
self.start.setDisabled(True)
self.get_thread_start.updated.connect(self.show_process)
self.get_thread_start.start()
def stopPressed(self):
self.start.setDisabled(False)
self.get_thread_start.running = False
def show_process(self, key, data):
self.field_dict[key].setText(data)
class Start_Process(QtCore.QThread):
updated = QtCore.pyqtSignal(int, str)
running = True
def __init__(self, mcstaddr, mcstport):
QtCore.QThread.__init__(self)
self.counter = 0
self.array = [1,0,1,0,1,0,1,0,1]
def run(self):
while self.running:
for i in self.array :
self.updated.emit(i, str(self.counter))
time.sleep(0.5)
self.counter += 1
def main():
app = QtGui.QApplication(sys.argv)
GUI = Window()
GUI.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()