在 python 中调用另一个 Class 的变量
Call a variable from another Class in python
我正在尝试从 MainWindow 的 Class 调用一个变量到 Dialog 的另一个 class,我知道这个问题之前有人问过,但我尝试了所有给定的答案,但我不能'让它工作,因为我仍然是 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_())
您可以将 parent
发送到对话框,然后您可以访问主 window - self.parent.Value
但是如果你想在对话框中使用它,你应该在 __init__
中创建一些默认值的变量 __init__
顺便说一句:我们为 类 使用 CamelCase
名称 - 即。 QPushButton
和 DataAcquisition
、SaveDialog
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_())
我正在尝试从 MainWindow 的 Class 调用一个变量到 Dialog 的另一个 class,我知道这个问题之前有人问过,但我尝试了所有给定的答案,但我不能'让它工作,因为我仍然是 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_())
您可以将 parent
发送到对话框,然后您可以访问主 window - self.parent.Value
但是如果你想在对话框中使用它,你应该在 __init__
中创建一些默认值的变量 __init__
顺便说一句:我们为 类 使用 CamelCase
名称 - 即。 QPushButton
和 DataAcquisition
、SaveDialog
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_())