从一个线程在 PyQt 的 lineEdit 中引入一个文本 [英] Introduce a text in a lineEdit of PyQt from a thread

查看:53
本文介绍了从一个线程在 PyQt 的 lineEdit 中引入一个文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不折叠程序的情况下从获取数据的线程中引入 lineEdit 中的文本?重要的一行在fil"类中,它显示了 Principal.self.aplicacio.actual_lineEdit.setText(self.temp)

How can I introduce a text in a lineEdit from a thread that are getting the data whithout colapse the program? The important line is in the class "fil" where it shows 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()

推荐答案

有几种方法可以正确执行此操作.

There are a few ways to do this correctly.

第一个是使用 QThread 而不是 python 线程.然后,您可以使用 Qt 信号将消息从 fil 线程传回 Qt MainThread,并将消息附加到那里的 QLineEdit.另一种类似的方法是继续使用 Python 线程,但将您的消息放在 Python Queue.Queue() 对象中.这个 Queue 然后被一个辅助 QThread 读取,它的唯一目的是从 Queue 中读取消息并将信号发送回 MainThread.

The first is to use a QThread instead of a python thread. 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. Another similar approach is to continue using a Python thread, but place your message in a Python Queue.Queue() object. 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.

这两种方法的共同特点是只能从 MainThread 访问 Qt GUI 对象,并使用信号/插槽在线程之间进行通信.以下是我回答过类似问题的其他一些问题(您应该能够根据您的计划进行调整):

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. Here are some other questions where I've answered similar questions (you should be able to adapt them to your program):

然而,自从回答了这些问题后,我和我的同事创建了一个项目,帮助简化编写多线程 Qt 应用程序.该项目名为qtutils,位于PyPi 所以它可以用 pip 或 easy_install 安装(只需运行 pip install qtutilseasy_install qtutils 从命令行/终端窗口).

However, since answering those questions, my colleagues and I have created a project that helps simplify writing multi-threaded Qt applications. 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).

该库具有(除其他外)一些函数 inmaininmain_later ,它们将在 Qt MainThread 中运行指定的方法(无论调用来自哪个线程)同步或异步.关于如何使用这些方法的文档位于此处.我已经修改了您的示例代码以使用我的 inmain 方法并将代码放在此处:http://pastebin.com/QM1Y6zBx -- 显然你需要安装 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. 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!

这篇关于从一个线程在 PyQt 的 lineEdit 中引入一个文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆