[英]Introduce a text in a lineEdit of PyQt from a thread
How can I introduce a text in a lineEdit from a thread that are getting the data whithout colapse the program? 如何从线程中获取数据而不折叠程序的线程中的lineEdit中引入文本? The important line is in the class "fil" where it shows Principal.self.aplicacio.actual_lineEdit.setText(self.temp)
重要的一行在“ fil”类中,它显示Principal.self.aplicacio.actual_lineEdit.setText(self.temp)
# !/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import serial
import threading
from time import sleep
from PyQt4 import QtCore, QtGui
from temperaturaUI import Ui_Form
class Principal(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.aplicacio = Ui_Form()
self.aplicacio.setupUi(self)
self.aplicacio.sortir_Button.clicked.connect(exit)
self.aplicacio.connectar_Button.clicked.connect(self.connectar)
def connectar(self):
try:
arduino = serial.Serial('/dev/ttyACM0', 9600)
print "Connectat amb èxit"
temperatura = fil(0, arduino, self.aplicacio.actual_lineEdit)
temperatura.start()
except:
print "Impossible connectar a l'Arduino"
class fil(threading.Thread):
def __init__(self, temp, serie, line):
threading.Thread.__init__(self)
self.temp = temp
self.serie = serie
self.line = line
def run(self):
try:
while 1:
self.temp = self.serie.readline()
if self.temp != 0:
**Principal.self.aplicacio.actual_lineEdit.setText(self.temp)**
sleep(0.2)
except:
print "Error al llegir de l'Arduino"
def main():
app = QtGui.QApplication(sys.argv)
aplicacio = Principal()
aplicacio.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
You can use signals
. 您可以使用
signals
。 You would add a signal to the fil
class that emits the new text: 您将向
fil
类添加一个发出新文本的信号:
class fil(threading.Thread):
update_line_edit = pyqtSignal(str)
def __init__(self, temp, serie, line):
...
def run(self):
try:
while True:
self.temp = self.serie.readline()
if not self.temp:
update_line_edit.emit(self.temp)
...
Then, simply connect that signal to a slot
function in your Principal
class: 然后,只需将该信号连接到
Principal
类中的slot
函数:
class Principal(QtGui.QWidget):
def __init__(self):
...
def connectar(self):
try:
arduino = serial.Serial('/dev/ttyACM0', 9600)
print "Connectat amb èxit"
temperatura = fil(0, arduino, self.aplicacio.actual_lineEdit)
temperatura.change_line_edit.connect(self.update_line_edit)
...
def update_line_edit(self, text):
self.aplicacio.actual_lineEdit.setText(text)
There are a few ways to do this correctly. 有几种方法可以正确执行此操作。
The first is to use a QThread instead of a python thread. 第一种是使用QThread而不是python线程。 You can then use Qt signals to pass a message back from the
fil
thread to the Qt MainThread and append the message to the QLineEdit
there. 然后,您可以使用Qt信号将消息从
fil
线程传递回Qt MainThread,并将消息附加到此处的QLineEdit
。 Another similar approach is to continue using a Python thread, but place your message in a Python Queue.Queue()
object. 另一
Queue.Queue()
似的方法是继续使用Python线程,但是将消息放置在Python Queue.Queue()
对象中。 This Queue
is then read by a secondary QThread, whose sole purpose is to read messages out of the Queue
and emit a signal back to the MainThread. 然后,第二个QThread读取此
Queue
,其唯一目的是从Queue
读取消息并将信号发送回MainThread。
The common feature of these two methods is that you only access Qt GUI objects from the MainThread and use signals/slots to communicate between threads. 这两种方法的共同特点是,您只能从MainThread访问Qt GUI对象,并使用信号/插槽在线程之间进行通信。 Here are some other questions where I've answered similar questions (you should be able to adapt them to your program):
以下是我回答过类似问题的其他一些问题(您应该可以将它们适应您的程序):
However, since answering those questions, my colleagues and I have created a project that helps simplify writing multi-threaded Qt applications. 但是,由于回答了这些问题,我和我的同事们创建了一个项目,可帮助简化编写多线程Qt应用程序的过程。 The project is called qtutils and is on PyPi so it can be installed with pip or easy_install (just run
pip install qtutils
or easy_install qtutils
from a commandline/terminal window). 该项目称为qtutils ,位于PyPi上,因此可以通过pip或easy_install进行安装(只需从命令行/终端窗口运行
pip install qtutils
或easy_install qtutils
)。
This library has (among others) some functions inmain
and inmain_later
which will run a specified method in the Qt MainThread (regardless of the thread the call is made from) synchronously or asynchronously. 该库(除其他外)具有
inmain
和inmain_later
函数,这些函数将同步或异步运行Qt MainThread中的指定方法(无论从哪个线程进行调用)。 Documentation on how to use these methods is here . 有关如何使用这些方法的文档在此处 。 I've modified your example code to use my inmain method and put the code here: http://pastebin.com/QM1Y6zBx -- obviously you need to install qtutils for it to work!
我已经修改了示例代码以使用我的inmain方法,并将代码放在此处: http : //pastebin.com/QM1Y6zBx -显然,您需要安装qtutils才能使其工作!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.