PyQT螺纹冻结

时间:2017-03-21 21:25:06

标签: multithreading python-3.x udp multiprocessing pyqt5

我希望我的PyQT小部件界面可以在后台线程中更改,我希望在仍然选中时通过udp选中复选框时从我的编辑框发送值。但是,当我尝试这样做时,我的GUI是冻结的,因为它似乎陷入了while循环(我认为)读取值并在整个时间检查复选框。我创建了一个Thread和队列来将数据从主线程传递到我的后台线程,认为它会处理这个,而我仍然可以在主GUI线程上的值,但它不起作用。有人可以帮忙吗?有没有办法在我的后台线程中访问我的所有主要线程小部件,所以我可以在后台循环发送数据?

import sys, os , queue, multiprocessing, threading
import socket
import numpy as np
from PyQt5.QtWidgets import QMainWindow,QHBoxLayout,QLabel,QRadioButton, QTextEdit, \
    QApplication, QWidget, QVBoxLayout, QFormLayout,\
    QMessageBox

from PyQt5.QtGui import QIcon, QFont, QDoubleValidator
from PyQt5.QtCore import pyqtSlot, QThread, QObject , pyqtSignal
from PyQt5 import QtCore, QtWidgets, Qt

class ThreadProcessor(QObject):

    # network code goes here
    completed = pyqtSignal()
    begin = pyqtSignal()
    finish = pyqtSignal()

    def weBegin(self):
        print("er begin")

    def ShutDown(self):
        self.finish.emit()

    def doSend(self):
        number = None
        print("hey")
        # print(self.list)
        if self.conSend :
            print("hellow")
        self.begin.emit()

        # todo code here

        # after a whole lot of code
        # self.completed.emit()


class App(QMainWindow):
    def __init__(self):
        super().__init__()
        '''self.title = 'INTERFACE GUI'
        self.left = 0
        self.top = 0
        self.width = 900
        self.height = 700

        self.setGeometry(self.left, self.top, self.width, self.height)
        self.setWindowIcon(QIcon("am.png"))'''
        self.setWindowTitle("Title")
        # self.setWindowIcon(QIcon('am.png'))
        self.resize(579, 353)
        self.table_widget = MyTableWidget(self)
        self.setCentralWidget(self.table_widget)
        self.show()

class MyTableWidget(QWidget):
    def __init__(self, parent):
        super(QWidget, self).__init__(parent)
        self.initUI()
        self.setupThread()

    @pyqtSlot()
    def completed(self):
        self.continous_send.setEnabled(False)
        self.pushButton1.setEnabled(True)


    @pyqtSlot()
    def begin(self):
        self.pushButton1.setEnabled(True)

    def setupThread(self):
        self.thread = QThread()
        self.worker = ThreadProcessor()
        self.worker.moveToThread(self.thread)

        self.thread.started.connect(self.worker.weBegin)
        # self.thread.terminate.connect(self.worker.ShutDown)
        self.worker.completed.connect(self.completed)
        self.worker.begin.connect(self.begin)
        # todo send the list form the line edits here somehow
        list = [self.textInput.text(), self.val2.text()]
        self.pushButton1.clicked.connect(self.worker.doSend)

        self.thread.start()




    def initUI(self):
        # Networking Stuff
        # self.udpSocket = QtNetwork.QUdpSocket()
        # self.udpSocket.connectToHost(self.appr, self.port)

        # layout
        self.layout = QVBoxLayout(self)
        self.label = QLabel("testLabel")
        self.layout.addWidget(self.label)
        # Initialize tab screen
        self.tabs = QtWidgets.QTabWidget()
        self.tab1 = QtWidgets.QWidget()
        self.tab2 = QtWidgets.QWidget()
        self.tab3 = QtWidgets.QWidget()
        self.tabs.setGeometry(0, 0, 200, 200)
        self.resize(200, 200)
        # Add tabs
        self.tabs.addTab(self.tab1, "tab1")

        # Create first tab
        # self.tab1.layout = QHBoxLayout(self)
        self.pushButton1 = QtWidgets.QPushButton(self.tab1)
        self.pushButton1.setText("send udp")
        self.pbleft = 0
        self.pbtop = 40

        self.pushButton1.setGeometry(QtCore.QRect(0, 0, 150, 25))
        # self.tab1.setLayout(self.tab1.layout)
        # creating radiobutton to continuosly send udp
        self.continous_send = QtWidgets.QRadioButton(self.tab1)
        self.continous_send.setText("Send Forever")
        self.continous_send.setChecked(False)
        # self.continous_send.setGeometry(QtCore.QRect(160,100,40,40))
        self.continous_send.move(160, 100)
        # creating a integer text input
        self.textInput = QtWidgets.QLineEdit(self.tab1)
        self.textInput.setPlaceholderText("enter value")
        self.textInput.setValidator(QDoubleValidator())
        self.textInput.setGeometry(QtCore.QRect(160, 0, 150, 25))
        # second integer box
        self.val2 = QtWidgets.QLineEdit(self.tab1)
        self.val2.setPlaceholderText("enter value")
        self.val2.setValidator(QDoubleValidator())
        self.val2.setGeometry(QtCore.QRect(160, 50, 150, 25))
        # create the Second Tab

        # Add tabs to widget
        self.layout.addWidget(self.tabs)
        self.setLayout(self.layout)

    def click(self):
        print('began Thread')

    def changed(self):
        if self.thread.isRunning():
            self.thread.terminate()
        print('Killed Thread')




'''
    def click(self):

        # self.checkSend = self.continous_send.isChecked()
        try :
            self.thread.start()
            self.pushButton1.setEnabled(False)
        except KeyboardInterrupt:
            self.finishQueueProcess()

    def finishQueueProcess(self):
        self.inq.put(None)
        self.inq.join()
        self.tdPro.shutThread()
        self.tdPro.join()
        os._exit(-1)
'''

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

0 个答案:

没有答案