从python

时间:2017-01-22 21:03:57

标签: python qt class pyqt class-variables

我试图将一个MainWindow类中的变量调用到另一个Dialog类,我知道之前已经问过这个问题,但是我尝试了所有给定的答案而且我无法使它像我一样工作还是初学者用python。

我的代码如下所示(我想调用的变量是self.Value,考虑到我需要它在同一个While循环中):

from PyQt4 import QtCore, QtGui
import sys
import time
from threading import Event, Thread

from mainwindow import Ui_MainWindow
from savedialog import Ui_saveDialog
from plot import Plot

from PyQt4.Qt import QString, QFileDialog


from pylab import *

import threading

#DH11 tempSensor Library
import Adafruit_DHT

#Library to find FFT and FFTshift 
from scipy.fftpack import fft, fftshift

from numpy import linspace

#Library to find Max and Min 
from operator import itemgetter

class dataAcquisition(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):

        super(dataAcquisition, self).__init__(parent)
        #threading.Thread.__init__(self)
        self.setupUi(self)

        self.startButton.clicked.connect(self.startButton_clicked)
        self.th = Thread(target = self.runValue)
        self.popSave = saveDialog()

        self.status = False
    def runValue(self):
        Fs = 8000
        N = 256
        f = linspace(-Fs/2, Fs/2, N)

        X = []
        Y = []
        MaxMin = []
        self.i = 0.0

        ax1 = subplot(2, 1, 1)
        Line1 = plot(0,0,'r-')[0]

        ax2 = subplot(2, 1, 2)
        Line2 = plot(f,0*f,'r-')[0]

        self.status = True
        while (self.status):

            self.Value = Adafruit_DHT.read_retry(11, 4)[1]        

            X.append(self.i)
            Y.append(self.Value)

            FFT = log10(abs(fft(Y, N)))
            FFT = fftshift(FFT)

            Line1.set_xdata(X)
            Line1.set_ydata(Y)

            ax1.relim()
            ax1.autoscale_view()

            Line2.set_ydata(FFT)
            ax2.relim()
            ax2.autoscale_view()

            for k in Y:
                for MM in range(int(k)):
                    MaxMin.append(MM)

            self.MaxValue = max(MaxMin)
            self.MinValue = min(MaxMin)

            self.i += 0.10
            pause(0.01)

            self.lcdNumber_value.display(self.Value)
            self.lcdNumber_MaxValue.display(self.MaxValue)
            self.lcdNumber_MinValue.display(self.MinValue)

    def startButton_clicked(self):
        self.th.start()      
        self.popSave.show()

class saveDialog(QtGui.QDialog, Ui_saveDialog):
    def __init__(self, parent=None):

        super(saveDialog, self).__init__(parent)

        flags = QtCore.Qt.Drawer | QtCore.Qt.WindowStaysOnTopHint
        self.setWindowFlags(flags)
        self.setupUi(self)

        self.checkBox.stateChanged.connect(self.Unlimited_NOVs)

        self.saveOkButton.clicked.connect(self.acceptOKButtonClicked)

        self.NOVs = 0.0

        self.spinBox.valueChanged.connect(self.spinBox_value_changed)

    def Unlimited_NOVs (self, state):
        if state == QtCore.Qt.Checked:
            self.NOVs = 5

    def spinBox_value_changed (self):
        self.NOVs = self.spinBox.value()

    def acceptOKButtonClicked(self):

        #Here I want to call self.Value

        i = 1
        while True:
            Name = QtGui.QFileDialog.getSaveFileName(self, 'Save File', 'sessionData.txt')
            self.fName = open(Name %i ,"w")

            i += 1
            c = 1
            while c <= self.NOVs:
                c += 1

                self.fName.write(str(self.Value))
                self.fName.write("\n")

            else:
                self.fName.close()



if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    MainWindow = dataAcquisition()
    MainWindow.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

您可以将parent发送到对话框,然后您就可以访问主窗口了 - self.parent.Value

但是如果你想在对话框__init__

中使用它,你应该在__init__创建一个带有默认值的变量

BTW:我们为类使用CamelCase名称 - 即。 QPushButtonDataAcquisitionSaveDialog

from PyQt4 import QtCore, QtGui
from PyQt4.Qt import QString, QFileDialog
from threading import Event, Thread
import sys
import time

class DataAcquisition(QtGui.QMainWindow):

    def __init__(self, parent=None):
        super(DataAcquisition, self).__init__(parent)

        self.startButton = QtGui.QPushButton(self, text="Show Dialog")
        self.startButton.clicked.connect(self.startButton_clicked)

        self.th = Thread(target = self.runValue)

        # create variable with default value
        self.Value = "Hello World!"

        # send parent (self) to Dialog
        self.popSave = SaveDialog(self)

    def runValue(self):
        self.Value = 0
        while True:
            self.Value += 1
            time.sleep(1)

    def startButton_clicked(self):
        self.th.start()      
        self.popSave.show()

class SaveDialog(QtGui.QDialog):

    def __init__(self, parent=None):
        super(SaveDialog, self).__init__(parent)

        vbox = QtGui.QVBoxLayout()

        # this value is get when Dialog is create, not when is executed `show()`
        # so it gets default value "Hello World!"
        self.label = QtGui.QLabel(self, text=self.parent().Value)
        vbox.addWidget(self.label)

        self.button = QtGui.QPushButton(self, text="Update label")
        self.button.clicked.connect(self.button_clicked)
        vbox.addWidget(self.button)

        self.setLayout(vbox)

    def button_clicked(self):
        # get current value from parent
        self.label.setText(str(self.parent().Value))

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    MainWindow = DataAcquisition()
    MainWindow.show()
    sys.exit(app.exec_())