线程之间交换变量

时间:2018-12-10 12:57:01

标签: python pyqt4 qthread

我需要将值从Test()类传递给Tread_400类。告诉我如何正确实施。以及我的想法是否正确。这是因为每个窗口都应在单独的线程中运行,并且每个流都必须显示来自单个进程的矩阵数据。

#--编码:utf-8--

from PyQt4 import QtGui, QtCore, uic
import sys
import pyqtgraph as pg
import numpy as np
import data
import process
class Main(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(Main, self).__init__(parent)
        self.k = 0
        self.winnd = uic.loadUi('input_window.ui')

        # main button
        self.addButton = QtGui.QPushButton('add'.decode('utf-8'))
        self.addButton.clicked.connect(self.addWidget)
        self.winnd.pushButton.clicked.connect(self.add_spectrum)
        self.winnd.pushButton_2.clicked.connect(self.close_wind)
        # scroll area widget contents - layout
        self.scrollLayout = QtGui.QGridLayout()

        # scroll area widget contents
        self.scrollWidget = QtGui.QWidget()
        self.scrollWidget.setLayout(self.scrollLayout)

        # scroll area
        self.scrollArea = QtGui.QScrollArea()
        self.scrollArea.setWidgetResizable(True)
        self.scrollArea.setWidget(self.scrollWidget)

        # main layout
        self.mainLayout = QtGui.QGridLayout()

        # add all main to the main vLayout
        self.mainLayout.addWidget(self.addButton)
        self.mainLayout.addWidget(self.scrollArea)

        # central widget
        self.centralWidget = QtGui.QWidget()
        self.centralWidget.setLayout(self.mainLayout)

        # set central widget
        self.setCentralWidget(self.centralWidget)
        self.i = 0
        self.y = 0

    def addWidget(self):

        self.winnd.show()

    def add_spectrum(self):
        self.band_list = []
        self.freq_list = []
        self.start_freq = float(self.winnd.lineEdit.text())*1e6
        self.stop_freq = float(self.winnd.lineEdit_2.text())*1e6
        self.band = self.stop_freq - self.start_freq
        x_axis=np.arange(self.start_freq,self.stop_freq,self.band/4097)
        while True:

            if self.band < 25e6:
                if self.band == 0:
                    break
                self.band_list.append(self.band)

                break

            else:
                self.band = np.abs(self.band - 25e6)

                self.band_list.append(25.0e6)

        for i in self.band_list:
            self.start_freq = self.start_freq + i

            self.freq_list.append(self.start_freq)
        for j in range(len(self.band_list)):
            self.freq_list[j] = self.freq_list[j] - self.band_list[j] / 2
        data.list_band.append(self.band_list)
        data.list_freq.append(self.freq_list)
        self.scrollLayout.addWidget(Test(self.k,x_axis), self.y, self.i)
        self.i += 1
        if self.i == 3:
            self.y += 1
            self.i = 0
        self.k += 1
        data.list.append(self.k)
        self.winnd.close()

    def close_wind(self):
        self.winnd.close()


class Test(QtGui.QWidget):
    def __init__(self, nember,band):
        super(Test, self).__init__()
        self.flag=False
        self.win = pg.PlotWidget()
        self.win.setLimits(xMin=min(band),xMax=max(band))
        self.pushButton = QtGui.QCheckBox('1')
        self.pushButton1 = QtGui.QCheckBox('2')
        self.pushButton2 = QtGui.QCheckBox('3')
        self.thread = Tread_400(self.win, nember,band)
        self.emit(QtCore.SIGNAL('mysignal(QString)'),True)
        button_layout=QtGui.QHBoxLayout()
        button_layout.addWidget(self.pushButton)
        button_layout.addWidget(self.pushButton1)
        button_layout.addWidget(self.pushButton2)
        layout = QtGui.QVBoxLayout()
        layout.addItem(button_layout)
        layout.addWidget(self.win)
        self.setLayout(layout)
        self.thread.start()
        self.pushButton.stateChanged.connect(self.state_changed)
    def state_changed(self,int):
        if self.pushButton.isChecked():
            print("CHECKED!")
            self.flag=True
        else:
            print("UNCHECKED!")
            self.status1 = False


class Tread_400(QtCore.QThread):
    def __init__(self, wind, number,band):
        self.x_axis=band
        self.wind = wind
        self.number = number
        QtCore.QThread.__init__(self)
        self.timer1 = QtCore.QTimer()


    def run(self):
        self.timer1.timeout.connect(self.plot_data)
        self.timer1.start(0.001)

    def plot_data(self):
        app.processEvents()
        # print(data.mat_data[:,self.number])
        self.wind.plot(self.x_axis,data.mat_data[:, self.number], clear=True)


app = QtGui.QApplication(sys.argv)
myWidget = Main()
myWidget.show()
app.exec_()

0 个答案:

没有答案