我用qt designer写了一些GUI。这个脚本是用qt designer生成的,我编写了类mythread:
from PyQt4 import QtCore, QtGui
import time
import socket
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class mythread(QtCore.QThread):
def __init__(self):
QtCore.QThread.__init__(self)
def run(self):
while True:
TCP_IP =
TCP_PORT =
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send('GLST\r\n')
glst = s.recv(1024)
if glst:
s.send('GLSC\r\n')
glsc = s.recv(1024)
print glst,glsc
s.close()
time.sleep(0.1)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 600)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(40, 40, 221, 31))
self.label.setStyleSheet(_fromUtf8("background-color:rgb(48, 48, 48)"))
self.label.setObjectName(_fromUtf8("label"))
self.label_2 = QtGui.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(290, 40, 221, 31))
self.label_2.setStyleSheet(_fromUtf8("background-color:rgb(48, 48, 48)"))
self.label_2.setObjectName(_fromUtf8("label_2"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QObject.connect(self.label, QtCore.SIGNAL(_fromUtf8("textChanged(QString)")), self.mythread)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("MainWindow", "<html><head/><body><p><span style=\" font-weight:600; color:#ffffff;\">co:</span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
self.label_2.setText(QtGui.QApplication.translate("MainWindow", "<html><head/><body><p><span style=\" font-weight:600; color:#ffffff;\">co:</span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
在类mythread中我有函数运行,它与服务器建立连接,然后在它们上发送两个命令。这个函数接收一些数据(glst,glsc),现在我想在GUI中的textlabel中用while循环动态看到这些数据(glst,glsc)。
但我不知道如何将线程连接到gui-textlabel。
感谢您的任何想法。约翰尼拉。
这是带有来自coments的补充的脚本
from PyQt4 import QtCore, QtGui
import time
import socket
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class mythread(QtCore.QThread):
def __init__(self):
QtCore.QThread.__init__(self)
def run(self):
while True:
TCP_IP =
TCP_PORT =
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send('GLSC\r\n')
glsc = s.recv(1024)
print glsc[:9]
self.setLog(glsc[:9])
if glsc:
s.send('GLST\r\n')
glst = s.recv(1024)
print glst
self.setLog(glst)
s.close()
time.sleep(0.1)
def setLog (self, text):
self.emit(SIGNAL("log(QString)"), QtCore.QString(text))
class Ui_MainWindow(object):
def setupUi(self,MainWindow):
self.mythread = mythread()
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 600)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(40, 40, 221, 31))
self.label.setStyleSheet(_fromUtf8("background-color:rgb(48, 48, 48)"))
self.label.setObjectName(_fromUtf8("label"))
self.label_2 = QtGui.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(290, 40, 221, 31))
self.label_2.setStyleSheet(_fromUtf8("background-color:rgb(48, 48, 48)"))
self.label_2.setObjectName(_fromUtf8("label_2"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QObject.connect(self.mythread, QtCore.SIGNAL("log(QString)"), self.label.setText)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("MainWindow", "<html><head/><body><p><span style=\" font-weight:600; color:#ffffff;\"></span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
self.label_2.setText(QtGui.QApplication.translate("MainWindow", "<html><head/><body><p><span style=\" font-weight:600; color:#ffffff;\"></span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
答案 0 :(得分:1)
最后编辑:2014年12月8日9:01重构代码
我建议您通过继承类QtGui.QMainWindow
实现,请参阅我的重构代码;
from PyQt4 import QtCore, QtGui
import sys
import time
import socket
class QMyThread (QtCore.QThread):
def __init__(self, parent = None):
super(QtCore.QThread, self).__init__(parent)
def run(self):
while True:
TCP_IP = '127.0.0.1'
TCP_PORT = 23
mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mySocket.connect((TCP_IP, TCP_PORT))
mySocket.send('GLSC\r\n')
glsc = mySocket.recv(1024)
print glsc[:9]
self.setLog1(glsc[:9])
if glsc:
mySocket.send('GLST\r\n')
glst = mySocket.recv(1024)
print glst
self.setLog2(glst)
mySocket.close()
time.sleep(0.1)
def setLog1 (self, text):
self.emit(QtCore.SIGNAL('log1(QString)'), QtCore.QString(text))
def setLog2 (self, text):
self.emit(QtCore.SIGNAL('log2(QString)'), QtCore.QString(text))
class QMyMainWindow (QtGui.QMainWindow):
def __init__(self, parent = None):
super(QtGui.QMainWindow, self).__init__(parent)
self.myInit()
def myInit (self):
self.mythread = QMyThread(self)
self.setObjectName('MainWindow')
self.resize(800, 600)
self.centralwidget = QtGui.QWidget(self)
self.centralwidget.setObjectName('centralwidget')
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(40, 40, 221, 31))
self.label.setStyleSheet('color:rgb(255, 255, 255); background-color:rgb(48, 48, 48);')
self.label.setObjectName('label')
self.label_2 = QtGui.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(290, 40, 221, 31))
self.label_2.setStyleSheet('color:rgb(255, 255, 255); background-color:rgb(48, 48, 48);')
self.label_2.setObjectName('label_2')
self.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(self)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName('menubar')
self.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(self)
self.statusbar.setObjectName('statusbar')
self.setStatusBar(self.statusbar)
self.setWindowTitle('Main Windows')
self.connect(self.mythread, QtCore.SIGNAL('log1(QString)'), self.label.setText)
self.connect(self.mythread, QtCore.SIGNAL('log2(QString)'), self.label_2.setText)
self.mythread.start()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
MainWindow = QMyMainWindow()
MainWindow.show()
sys.exit(app.exec_())
上次编辑:2014年12月8日7:34修正错误'未找到连接'
我对QThread的主要小部件有同样的想法,将它与SIGNAL
连接回主要小部件;
主要小部件
.
.
def setupUi(self, MainWindow):
.
.
self.mythread = mythread()
.
.
QtCore.QObject.connect(self.mythread, QtCore.SIGNAL("log(QString)"), self.label.setText)
.
.
<强>的QThread 强>
class mythread(QtCore.QThread):
def __init__(self):
QtCore.QThread.__init__(self)
def run(self):
while True:
TCP_IP =
TCP_PORT =
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send('GLST\r\n')
glst = s.recv(1024)
self.setLog(glst)
if glst:
s.send('GLSC\r\n')
glsc = s.recv(1024)
self.setLog(glsc)
print glst,glsc
s.close()
time.sleep(0.1)
def setLog (self, text):
self.emit(QtCore.SIGNAL("log(QString)"), QtCore.QString(text))
此致