【发布时间】:2017-06-02 18:57:55
【问题描述】:
首先对篇幅表示抱歉。我想尽可能地解释我的问题。我对 Python 很陌生,并试图使用嵌入在 PyQt4 中的 PyQtGraph 制作一个绘图应用程序。几天前,我的plotting problem 得到了一个非常好的答案,我的下一步是让两个 PyQtGraphs 绘图小部件同时在同一个 PyQt4 的 CentralWidget 中绘图。通过与描述的链接相同的方法,两个图都可以正常工作,但是 GUI 没有响应。为了克服这个问题,我的目标是使用QThread,为此,我需要在不同的类中使用我的绘图功能。但是我弄乱了我的变量,看不到如何:
from PyQt4 import QtCore, QtGui
import pyqtgraph as pg
import random
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.central_widget = QtGui.QStackedWidget()
self.setCentralWidget(self.central_widget)
self.login_widget = LoginWidget(self)
self.login_widget.button.clicked.connect(Plots.plotter)
self.central_widget.addWidget(self.login_widget)
class LoginWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(LoginWidget, self).__init__(parent)
layout = QtGui.QHBoxLayout()
self.button = QtGui.QPushButton('Start Plotting')
layout.addWidget(self.button)
self.plot = pg.PlotWidget()
layout.addWidget(self.plot)
self.setLayout(layout)
class Plots(MainWindow):
def print(self):
print('hello World')
def plotter(self):
self.data = [0]
self.curve = self.login_widget.plot.getPlotItem().plot()
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.updater)
self.timer.start(0)
def updater(self):
self.data.append(self.data[-1]+0.2*(0.5-random.random()))
self.curve.setData(self.data)
if __name__ == '__main__':
app = QtGui.QApplication([])
window = MainWindow()
window.show()
app.exec_()
这给了我一个 Plots.plotter 错误:
self.data = [0]
AttributeError: 'bool' object has no attribute 'data'
如果在 MainWindow 类中我将“button.connect”参数替换为 Plots.print,它可以正常工作。所以我可以看到我在 MainWindow 中创建了一个 LoginWidget 对象,然后 Plots 从 MainWindow 继承,再次调用相同的 LoginWidget 对象这一事实有问题。
我尝试了一个建议的解决方案,其中父亲(MainWindow)不访问孩子(Plots)的方法。但是,如果从 Plots 中,我想调用我打算放置线程的类,我会再次遇到同样的错误....
import sys
from PyQt4 import QtCore, QtGui
import pyqtgraph as pg
import random
class LoginWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(LoginWidget, self).__init__(parent)
layout = QtGui.QHBoxLayout()
self.button = QtGui.QPushButton('Start Plotting')
layout.addWidget(self.button)
self.plot = pg.PlotWidget()
layout.addWidget(self.plot)
self.setLayout(layout)
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.central_widget = QtGui.QStackedWidget()
self.setCentralWidget(self.central_widget)
self.login_widget = LoginWidget(self)
self.central_widget.addWidget(self.login_widget)
class Plots(MainWindow):
def __init__(self, parent=None):
super(Plots, self).__init__(parent=parent)
self.login_widget.button.clicked.connect(MyThread.plotter)
class MyThread(MainWindow):
def __init__(self, parent=None):
super(Aname, self).__init__(parent=parent)
def print(self):
print('hello World')
def plotter(self):
self.data = [0]
self.curve = self.login_widget.plot.getPlotItem().plot()
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.updater)
self.timer.start(0)
def updater(self):
self.data.append(self.data[-1]+0.2*(0.5-random.random()))
self.curve.setData(self.data)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
w = Plots()
w.show()
sys.exit(app.exec_())
【问题讨论】:
-
您的代码给了我一个不同的错误。
self.login_widget.button.clicked.connect(Plots.plotter)AttributeError: 'function' 对象没有属性 'pyqtSignature' -
当您在 self.login_widget.button.clicked.connect(Plots.plotter) 中将 Plots.plotter 替换为 Plots.print 时是否会出现相同的错误?
标签: python pyqt pyqt4 qthread pyqtgraph